CodingNodes Space play · ←→ step · R restart

The problem

An email system has two paths that have almost nothing in common. The send path is a firehose: a message to ten people is ten deliveries, each over a wire that is slow, unreliable, and run by someone else, and the sender's request has to return in a fraction of a second no matter what. The read path is the opposite: one user's inbox, a small and time-ordered set of messages, read a page at a time. Designing the two as if they were the same service is where naive mail systems fail — one is volume and retry, the other is per-user and small.

So the design splits the system along that seam. The send path stores the message once, fans it out into one delivery task per recipient, and hands those tasks to a queue that absorbs the bursts and the failures. The delivery workers then talk to the unreliable wire — which defers, bounces, and retries — and the receiver closes the correctness loop by deduping on the message id, so an at-least-once delivery lands in the inbox exactly once. The inbox itself is an append-only, per-user log with a separate search index and a sidecar for the big attachments. We will build the fan-out, the wire and its retries, the dedupe, the inbox, and the gate that keeps the untrusted wire out of the trusted inbox.

Requirements

Functional — what it must do

  • Send: a sender composes a message and it is accepted and stored, with the request returning quickly, before any delivery happens.
  • Deliver: each recipient copy is delivered over the wire to the far end, retried when the wire defers it, and bounced when it rejects it — without the sender waiting.
  • Receive: an inbound message is validated, rate-checked, and classified, then appended to the recipient's inbox, with the big content kept in a sidecar.
  • Read: a recipient lists their inbox a page at a time and searches it by content, both served from a per-user store and a derived index.

Non-functional — how well it must do it

  • The send request returns in well under a second and does not depend on the wire: delivery is asynchronous and can fail, retry, and be slow without the sender ever knowing.
  • Deliverability is at-least-once into the far end and exactly-once into the inbox: a message is never silently dropped, and it is never shown to the recipient twice.
  • The inbox read is per-user and bounded: listing a page touches only that user's recent messages, and searching is a query against a prebuilt index, not a scan of the log.
  • The system holds up under a flood: a burst of sends, and a flood of inbound mail from the untrusted wire, neither of which can exhaust the queue, the inbox, or the sender's request budget.

Scale — how much it must take

  • On the order of tens of millions of messages sent and received per day, with per-recipient fan-out at compose time and a retry ladder that can hold a deferred delivery for hours before it bounces.

Assumptions — the numbers this design is sized for

A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.

Send volume
tens of millions/day
The firehose. One message to N recipients is N delivery tasks, so the real throughput is higher than the message count.
Send latency
< 1 s to the sender
The sender's request must return before any delivery happens — the send path and the wire are decoupled by a queue.
Fan-out
per recipient
An email to ten people is ten independent deliveries, each with its own retry, its own bounce, its own dedupe.
Retry ladder
hours, then bounce
A deferred delivery is retried with backoff for a bounded window — hours, not minutes — before it is given up and bounced.
Dedupe
on the message id
The receiver keeps a short-lived set of recently seen message ids, so a retried copy is dropped, not appended again.
Inbox read
per user · a page
The inbox is an append-only log, one per user; a read is the tail plus a cursor, never a scan.
Attachments
a sidecar
Content above a small threshold goes to object storage, with a reference left in the message — the inbox log stays small and fast.

Takeaways

Part 1

Two paths, not one service

The send path is a fan-out firehose over an unreliable wire; the read path is a small, per-user inbox. They share a message, but they are not the same system, and building them as one is the root of most mail failures.

  1. 1
    Asymmetric by design

    The two paths have opposite shapes. Sending is high-volume and fan-out: one message becomes one delivery per recipient, each over a wire that is slow, unreliable, and run by someone else, and the whole thing has to survive bursts and failures. Reading is low-volume and per-user: one inbox, a small and time-ordered set, read a page at a time. If you build them as one service, the firehose drowns the reads and the unreliable wire takes down the sender's request. Split them and each can be built for its own shape.

  2. 2
    Deliverability is the hard part

    The one thing this system is judged on is that a message reaches the recipient's inbox exactly once, and that it is never silently dropped. Everything else — latency, throughput, search — is infrastructure in service of that. The difficulty is that the wire between us and the recipient is unreliable and untrusted: it defers, it bounces, it retries, and it can deliver the same message twice. So the design goal is at-least-once over the wire, closed into exactly-once at the inbox by a dedupe the receiver performs.

Part 2

The message and its anchor

A message is headers plus content, and one of the headers — the message id — is globally unique. That id is the anchor: it is what lets the receiver tell a retried copy from a new message, and it is the whole of exactly-once delivery.

  1. 1
    Headers route, content sits

    A message splits cleanly into two halves. The headers are small and routable: who it is from, who it is to, the subject, the date, and a globally unique message id. The content is big and inert: the body, and the attachments, which are referenced rather than inlined. The send path mostly cares about the headers — they drive routing, fan-out, and dedupe — while the content is shipped as a sidecar that the reader pulls when needed.

  2. 2
    The message id is the anchor

    Of all the fields, the message id is the one that carries the correctness of the whole system. It is generated once, at compose time, and it is the same on every retry of the same logical message. A genuinely new message has a different id. So when the receiver sees a copy whose id it has already seen, it knows — for certain — that this is a repeat of a message it already has, and it drops it. At-least-once delivery becomes exactly-once in the inbox purely because of this one id.

Part 3

Compose, then hand off

The send request must return in a fraction of a second and must not depend on the wire. So the API validates and stores the message, fans it out into one delivery task per recipient, and hands the tasks to a queue — and returns. Delivery is someone else's problem, now.

  1. 1
    Decouple the send from the delivery

    The send API does three things and then returns: it validates the message, stores it once, and enqueues a delivery task for each recipient. It does not talk to the wire. That is the whole point — the sender's request is bounded by validate-and-enqueue, which is fast and local, and it is completely independent of whether the far end is up, slow, or rejecting. The queue is the seam: it absorbs the burst of tasks, and it holds them so the delivery workers can take them at their own pace.

  2. 2
    One message, N independent deliveries

    An email to ten recipients is not one message with ten addresses — it is one stored message and ten delivery tasks. Each task is independent: it has its own retry ladder, its own bounce, and its own dedupe at its own far end. That is what makes the fan-out safe. If one recipient's wire is down, the other nine still get their copy; if one is bounced, the rest are unaffected. The message is stored once, but the delivery is a fan-out of N, and each of the N is a small, bounded problem on its own.

Part 4

The unreliable wire

The far end does not simply accept a message. It can accept it, defer it, or reject it, and those three outcomes must be treated very differently. Deferring is a 'not now' that gets retried; rejecting is a 'never' that bounces; accepting is a 'queued by them' that is not the same as 'delivered'.

  1. 1
    Three outcomes, not one

    A delivery to the far end returns one of three things. Accepted means the far end has queued it — they will get to it, but that is their promise, not a delivery. Deferred means the far end is not ready right now — a full mailbox, a transient outage — and it is asking to be tried again later. Rejected means the far end will never take it — a bad address, a closed account — and it must be bounced to the sender. Treating these three the same is the classic mistake: it either spams the far end with retries it cannot serve, or it gives up on a message that would have delivered on the next try.

  2. 2
    The retry ladder

    A deferral is retried on a ladder, not in a loop. The first retry is soon, the next is later, the next later still, each step backing off, for a bounded window that spans hours rather than minutes — because a far end that is down for a network incident is a different situation from one that is down for a week. If the ladder is exhausted and the message has still not been accepted, it is given up and bounced to the sender with the reason. The ladder is the difference between 'we retried until it worked or it was truly gone' and 'we spammed the far end and then dropped the message'.

  3. 3
    At-least-once, then the dedupe closes it

    Because the ladder retries, the far end can be handed the same message twice — once on a delivery that was accepted but whose acknowledgement was lost, and once on the retry. That is at-least-once, and it is acceptable over the wire, because the receiver is the one that closes the correctness loop. The far end keeps a short-lived set of recently seen message ids; when a copy arrives whose id it has already seen, it drops it rather than appending it. So the inbox gets the message exactly once even though the wire delivered it twice. The dedupe is at the receiver, on the id, and it is what turns at-least-once into exactly-once.

Part 5

The inbox is a per-user log

A recipient's inbox is an append-only log, one per user, ordered by arrival. Reading it is reading the tail with a cursor, not a scan. The two heavier things — full-text search and the big attachments — live in derived structures so the log itself stays small and fast.

  1. 1
    Append-only, one per user

    The inbox is a log: messages are appended in arrival order and never rewritten. There is one such log per user, sharded by user id, so a user's reads touch only their own shard. Listing the inbox is reading the tail — the most recent N — with a cursor that marks how far back you have read. There is no scan, no join, no global ordering to maintain. That is why a read of a mailbox with a million messages can still return in a few milliseconds: it reads a page off the tail of one user's log.

  2. 2
    Search is a derived index, not a scan

    Finding a message by content is not something you do by reading the tail of a log. A background process reads each message as it is appended and adds it to a full-text index, keyed by user and term. A search query goes to that index, not to the log. The log is the source of truth and the index is a derived view: if the index is lost, it can be rebuilt by replaying the log. That separation is what keeps the 'find the email about the invoice' query fast without slowing down the append.

  3. 3
    Attachments are a sidecar

    A message with a fifty-megabyte attachment should not slow down every read of the inbox. So content above a small threshold is written to object storage, and the message log keeps only a reference — a pointer to the object. Reading the inbox is fast because it reads small log entries; pulling an attachment is a separate, explicit fetch of the object. The inbox log stays small and the big content is served from a store built for it.

Part 6

The gate between the wire and the inbox

Inbound mail arrives from an untrusted wire, addressed to a real user, and it can be a flood. Before anything is appended to the inbox, it must be validated, rate-checked, and classified. The gate is the boundary that keeps the untrusted wire out of the trusted inbox.

  1. 1
    Validate, rate, classify — in that order

    The gate does three checks, in a deliberate order. It validates first: is the recipient a real account here? A message to a nonexistent address is rejected at the gate and never reaches the inbox. It rate-checks next: is this sender, or this flood, within the allowed rate? A burst that exceeds it is held or dropped before it can fill the inbox. It classifies last: is this message spam or signal? Only a message that has passed all three is appended to the inbox. Ordering matters — validating and rate-checking are cheap and reject the obvious flood, so the expensive classification only runs on what survived.

  2. 2
    Rate-limit the flood

    The wire is untrusted, which means a hostile sender can try to fill an inbox or the outbound queue with a flood. The gate enforces a per-sender rate — how fast one sender can send to this domain — and a per-recipient rate — how fast one recipient's inbox can be filled — plus a global cap on the domain's intake. A flood that exceeds any of these is not appended; it is held, throttled, or dropped. The rate limit is what keeps a flood from being a denial of service on the inbox and the queue.

  3. 3
    Classify, then decide the folder

    The classification step is the decision of where a message lands: the inbox, the spam folder, or the gate's rejection. It is not a binary spam-or-not — a message can be legitimate but unwanted, in which case it goes to the spam folder rather than being dropped. The classification runs at the gate, before the append, so the folder is chosen at the moment of intake, not after the message is already in the log. That keeps the inbox clean and gives the recipient a recoverable place for the things that were close.

★ Finale

End to end

One message's life: composed and stored, fanned out to a queue, delivered over a wire that defers it once and then accepts it, deduped at the far end, and appended to the recipient's inbox exactly once — with the attachment in its sidecar and the index already built.

  1. 1
    The whole system

    Every piece in one frame. A sender composes; the send API stores the message and fans it out into the outbound queue; a delivery worker takes a task and hands it to the wire; the far end defers it once, accepts it on the retry, and dedupes the repeat; and the message lands in the recipient's inbox, with the attachment in the sidecar and the search index already holding it. One message, one inbox entry, no matter how the wire behaved.

  2. 2
    The fan-out, into the queue

    The sender composes a message to three recipients. The send API stores it once and enqueues three delivery tasks — one per recipient — and returns to the sender. The sender's request is done; the three deliveries are the queue's problem now. Each of the three is independent, so a failure on one does not touch the others.

  3. 3
    Deferred once, then accepted — and deduped

    A delivery worker takes one of the tasks and hands the message to the wire. The far end defers it — not ready right now — so the worker puts it back on the ladder and retries. The retry is accepted. Because the first attempt's acknowledgement was lost, the far end may see two copies carrying the same message id; it keeps the first and drops the second. The inbox gets the message exactly once.

  4. 4
    In the inbox, indexed, with its sidecar

    The message passes the gate — the recipient is real, the rate is fine, it is classified as signal — and is appended to the recipient's inbox log. The background indexer adds it to the search index, and the attachment is already sitting in the object-storage sidecar, referenced from the log entry. The recipient lists their inbox and sees exactly one copy of the message, fast, with its attachment one fetch away.

🌐 In the wild

In the wild

Distributed mail is the workhorse of every communication platform. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.

  1. 1
    The pattern, piece by piece

    Each box is a standard building block of a mail platform. Anchoring them to the classes of production systems that run that piece turns the abstract design into the shape behind the inbox that has to deliver every message exactly once, even on the worst day.

  2. 2
    Why the same pieces keep winning

    Independent mail platforms converge on the same pieces because each one removes a real failure: the queue removes the sender's request from the wire, the retry ladder removes the dropped-but-deliverable message, the message-id dedupe removes the double delivery, and the per-user log with sidecars removes the slow inbox and the slow attachment. That convergence across independent systems is the evidence the design is load-bearing.