System design · Durable acceptance, recipient delivery and mailbox sync

How to design an email service like Gmail

Accept each message durably, fan out recipient deliveries through a queue, and keep attachments outside mailbox reads. The Dropbox sync challenge covers file metadata and bytes, not mail routing or spam controls.

Updated · 4 min read

A teaching design of a system like Google, not Google's real architecture.

Durable message acceptance fans out through a queue to delivery workers and recipient servers.

Email Service like Gmail architecture diagram

Durable message acceptance fans out through a queue to delivery workers and recipient servers.

The short version

  • Acceptance means the message and its delivery intent are durably stored, not delivered. Commit both in one transaction with an outbox.
  • Store the message once and a recipient row per mailbox. Each recipient has its own delivery result.
  • Exactly-once delivery to a remote server is impossible when its acknowledgement can be lost. Make internal steps idempotent instead.
  • Attachments go to object storage. The mailbox database holds references, size, type and checksum.
  • Search indexes lag the mailbox. Mailbox state stays authoritative.

Clarifying questions to ask first

  • Consumer mail, business mail, or both? (Assume personal inboxes, sent mail, attachments and basic search.)
  • Does it send to external providers? (Assume yes. We can't control their response time or spam decisions.)
  • How fast must a message arrive? (Assume the API accepts durably and quickly, while delivery may take longer.)
  • What does deletion mean? (Retention and legal deletion are explicit product decisions.)
  • How is abuse handled? (Assume rate limits, sender authentication, reports and review before high-volume sending.)

Numbers that shape the design

These are assumptions, not Gmail traffic. One million users sending two messages a day is about 23 submissions/s, or 230/s at a ten-times busy period. At 75 KB each, excluding large attachments, that's 17 MB/s. If 20% go to three recipients and the rest to one, fan-out averages 1.4: 322 delivery tasks/s at peak. If 5% carry a 4 MB attachment, peak attachment writes are 46 MB/s.

Metadata, fan-out and attachment bytes need separate estimates. Size real queues and storage from measured workload.

API

POST /messages
Idempotency-Key: <client-generated-key>
{ to, cc, subject, body, attachmentIds }

GET /mailboxes/{mailboxId}/messages?cursor=...
GET /messages/{messageId}
GET /attachments/{attachmentId}
POST /messages/{messageId}/send

Return a message ID, acceptance time and queued state. Scope the idempotency key to the sending account, keep it long enough for realistic retries, and reject reuse with a different payload. Check authorisation on every mailbox, attachment and send call.

Data model

A message record holds sender, subject, body reference, creation time and an immutable version. A recipient record holds mailbox, message, folder, read state, labels and delivery state, so outcomes differ per recipient without copying bodies.

  • (accountId, idempotencyKey) is unique for accepted submissions.
  • Each recipient task has a stable key, so queue replay doesn't create a new delivery.
  • Accepted, delivered, bounced, deferred and deleted are timestamped events.

High-level design

The mail API checks account limits and idempotency, then writes the message, recipient rows and an outbox event in one transaction. A publisher sends recipient tasks to a durable queue (message queue). Workers apply policy, contact recipient servers and record results. A mailbox API reads the recipient view and returns a sync cursor.

Delivery, retries and bounces

A remote server accepts, defers or rejects. Retry temporary failures with bounded exponential backoff and jitter, stop at a maximum age or attempt count, and expose the final state. A worker can crash after remote acceptance but before recording success, so a retry may send again: no local success record doesn't prove remote failure.

Move poison messages out of the hot queue and alert on them. Track queue age as well as depth. Give each account a quota and isolate high-volume senders, so one recipient's outage delays only that recipient's work.

Search and sync

Mailbox reads use a per-recipient index, never a scan of bodies. A separate text index consumes mailbox events and can lag, so a new message may show in the inbox before search finds it. Return an index watermark so clients can tell "no results" from "still indexing". Check mailbox permissions even when results come from another store. Multi-device sync uses versioned change cursors; an expired cursor needs a fresh snapshot.

Spam and sender trust

Limit accounts and recipients, verify sender identity, watch complaint and bounce rates, and offer an appeal path. Google's public overview says Gmail filters use IP and domain characteristics, sender authentication and user feedback. That describes published signals, not this design or Gmail's full internals. Treat attachment names and message text as untrusted input in the web client.

Common traps

  • Returning success before durable acceptance. A crash then loses a message the sender thinks went through.
  • Giving one message with many recipients one delivery result.
  • Storing attachments in the mailbox database. Replication, backups and reads all inflate.
  • Promising exactly-once delivery when a remote acknowledgement can be lost.
  • Putting user-specific labels or read state in a shared cached message body.

Follow-up questions interviewers ask

  • "When is it safe to acknowledge a send?" After the message and its delivery intent are durably committed.
  • "A worker times out after remote acceptance." A retry may duplicate delivery. Keep this separate from internal idempotency.
  • "Can search be immediately consistent?" Yes, at higher write cost, or asynchronously with an explicit lag and a recovery path.

Frequently asked questions

Should the API wait until a message is delivered?

+

Usually no. Confirm durable acceptance, then expose delivery state asynchronously, because recipient servers have their own availability and rate limits.

Can email delivery be exactly once?

+

Not to an independent remote server when an acknowledgement can be lost. Internal submission and queue handling can be idempotent, but retrying an uncertain remote attempt may duplicate delivery.

Why keep attachments outside the mailbox database?

+

They are much larger than metadata. Object storage keeps mailbox indexes and ordinary reads small, while checksums and access controls protect the references.

Does this describe Gmail's architecture?

+

No. It is a teaching design for email. The linked Google Workspace article supports only the stated description of spam-filter signals.

Now fix it yourself.

Run the design under real traffic and see what breaks first. No signup.