Skip to main content

๐Ÿ“ง Email System Architecture

Overviewโ€‹

The RABS email system is a multi-service architecture built around IMAP connectivity, background queue processing, and a message cache layer. All state is stored in the admin_mail schema on DB2. The system separates concerns across six backend services, each with a distinct responsibility, coordinated through database-backed queues and a shared message cache.

Backend Servicesโ€‹

Six Node.js services compose the email backend:

ServiceFilePurpose
IMAP Serviceemail-imap.jsCore IMAP operations โ€” connect, fetch, search, stream
Cache Warmeremail-cache-warmer.jsBackground cache warming for configured mailboxes
Mail Ops Workeremail-mail-ops-worker.jsAsync delete, move, and flag operations via queue
Mail Sync Workeremail-mail-sync-worker.jsBackground folder synchronisation
Outgoing Workeremail-outgoing-worker.jsSMTP sending via outgoing mail queue
Mailbox Serviceemail-mailboxes.jsMailbox CRUD, schema validation, health tracking

All services read from and write to tables in the admin_mail schema.

API Endpointsโ€‹

IMAP Routes (backend/routes_v1p/email-imap.js)โ€‹

MethodEndpointDescription
GET/accountsList all configured email accounts
GET/accounts/:id/mailboxesList mailboxes (folders) for an account
GET/accounts/:id/messagesFetch message list for an account/folder
GET/accounts/:id/messages/:uidFetch a single message by UID
DELETE/accounts/:id/messages/:uidDelete a message (?background=true for async)
PATCH/accounts/:id/messages/:uid/flagsUpdate message flags (read, starred, etc.)
POST/accounts/:id/mailbox/:mailbox/resyncForce resync a specific mailbox folder
POST/accounts/:id/sendSend an email via SMTP
GET/cache-statsCache storage and hit-rate statistics
GET/queue-statsMail operations queue statistics
GET/system-mailbox-healthHealth status of all configured mailboxes

Me Email Routes (me-email.js)โ€‹

MethodEndpointDescription
GET/Get current user's email preferences
PUT/Update current user's email preferences

Queue Processing Pipelineโ€‹

The mail operations queue follows a linear state machine:

User action โ†’ API endpoint โ†’ admin_mail.mail_ops (status: pending)
โ†“
Worker polls every 5 seconds
โ†“
status: processing
โ†“
status: done OR status: error

When a user triggers a delete, move, or flag operation with ?background=true, the API inserts a row into admin_mail.mail_ops with status pending. The Mail Ops Worker polls the table at a configurable interval and processes each job sequentially, updating status to processing, then done or error upon completion.

Environment Variablesโ€‹

VariableDefaultDescription
EMAIL_WARMERfalseEnable/disable the background cache warmer
EMAIL_OPS_INTERVAL_MS5000Mail Ops Worker polling interval (ms)
EMAIL_SYNC_INTERVAL_MS5000Mail Sync Worker polling interval (ms)

State Ownership & Database Schemaโ€‹

All tables reside in the admin_mail schema. They are organised into five categories by function.

Core Tablesโ€‹

TablePurposeKey Details
mailboxesConfiguration truth for all email accountsconnection_health: healthy, degraded, failed, auth_failed
mailbox_user_accessUser-to-mailbox relationship mappingsLinks platform users to mailbox accounts
messagesCached message data (not authoritative)has_full_body flag indicates whether full content is cached

Sync Tablesโ€‹

TablePurposeKey Details
mailbox_folder_statePer-folder sync bookmarksTracks last_uid, last_modseq, last_sync_at

Queue Tablesโ€‹

TablePurposeState Machine
mail_opsDelete, move, flag operationspending โ†’ processing โ†’ done | error
mail_sync_jobsBackground sync job trackingManaged by Mail Sync Worker
outgoing_mailSMTP send queuequeued โ†’ sending โ†’ sent | failed

Stats Tables (Derived Views)โ€‹

Table/ViewPurpose
email_stats_folder_currentPer-folder message counts and sync status
email_stats_mailbox_currentPer-mailbox aggregate statistics
email_stats_queue_currentQueue depth and processing rates

Feature Tablesโ€‹

TablePurposeStatus
user_email_rulesUser-defined email filtering rulesActive
reggie_rulesAI-driven email rulesFuture
reggie_draftsAI-generated draft responsesFuture

Legacy Tables (Deprecated)โ€‹

These tables are from the original implementation and should not be used for new features:

Legacy TableReplaced By
user_email_accountsmailboxes + mailbox_user_access
user_email_messagesmessages
user_email_prefsmailbox_user_prefs

Truth Hierarchyโ€‹

Data authority flows top-down. When conflicts arise, the higher-level source wins:

IMAP Server (authoritative)
โ†“
mailboxes (configuration truth)
โ†“
messages (cache โ€” rebuilt from IMAP)
โ†“
Queue tables (transient processing state)
โ†“
Stats tables (derived, read-only)

Analytics Widgetsโ€‹

Eight dashboard widgets provide operational visibility into the email system:

#WidgetData Source
1Mail Operations QueueGET /api/v1/email/imap/queue-stats
2Email Cache StorageGET /api/v1/email/imap/cache-stats
3Email Cache Storage MonitorDerived from cache-stats over time
4Mail Sync Jobsadmin_mail.mail_sync_jobs
5Outgoing Mail Pipelineadmin_mail.outgoing_mail
6Queue Alerts & FailuresFiltered view of failed/error queue entries
7Mailbox Health & CacheGET /api/v1/email/imap/system-mailbox-health
8Queue Throughput (Last Hour)Aggregated ops completed in rolling 60-minute window