Design an email system (Gmail)

How a message travels between providers over SMTP, how spam and spoofing are filtered, and how a mailbox is stored and searched at huge scale.

On the roadmap:Interview Ready · step 5: Real-world designs

Lesson 2 of 18 · 11 min read · quiz · exercise

The problem

An email system receives and sends messages for billions of mailboxes, stores them for years, keeps spam out, and lets users search and organise (labels, threads).

Requirements

  • Reliable delivery: a message is never silently lost.
  • Interoperability with every other mail provider.
  • Spam, phishing and virus filtering.
  • Large attachments, fast search, and strong privacy.

How email moves between providers

Email uses SMTP to send between servers. The key ideas:

  • MX records in DNS tell you which servers accept mail for a domain.
  • Delivery is store-and-forward: a server accepts a message, persists it, and passes it on, retrying if the next hop is down.
  • Clients usually read mail through IMAP (or POP3) or through a provider's web and mobile API.

Figure SDA-RWS-02-1 · Servers talk SMTP to each other; readers fetch with IMAP or POP · © forcsguy.com

A post sorting office: email servers also accept, sort and forward messages between providersImage: Tim Sheerman-Chase · CC BY 2.0 · Source · Wikimedia Commons: Old Post Sorting Office, Portsmouth

Architecture

Figure SDA-RWS-02-2 · Email delivery architecture · © forcsguy.com

Sending

  1. The client sends the message to its provider's submission server (with authentication).
  2. The message is saved and put on an outbound queue, so slow recipients never slow the sender.
  3. A worker finds the recipient domain's MX hosts, connects with SMTP, and delivers. The worker signs the message (DKIM).
  4. Temporary failures are retried with backoff for days; permanent failures generate a bounce message.

Receiving

  1. The incoming server accepts the connection and verifies the sender with SPF, DKIM and DMARC.
  2. Filters score the message for spam, phishing and malware (rules, reputation, and machine learning).
  3. The message is written to the user's mailbox: metadata in a scalable database, bodies and attachments in object storage, and an entry in the search index.
  4. Clients are told (push, IMAP IDLE) that new mail arrived.

Figure SDA-RWS-02-3 · Where a received message is stored and how readers are told · © forcsguy.com

Fighting spam and spoofing

MechanismWhat it does
SPFThe domain lists which servers may send for it
DKIMThe sender signs the message; receivers check the signature
DMARCThe domain says what to do when SPF or DKIM fail, and gets reports
ReputationTrack sending behaviour per IP and domain; rate-limit new senders
Content filtersSpam classifiers, link and attachment scanning

Storage design

  • A mailbox is append-heavy and read by recent items. Partition by user id, so one user's mail sits together.
  • Keep metadata (labels, flags, thread id, snippet) separate from large bodies and attachments.
  • Threads group messages by headers (References, In-Reply-To) and subject.
  • Search uses an inverted index per user, updated as mail arrives.
  • Deduplicate identical large attachments with content hashes.
  • Keep multiple replicas and backups: email is people's records.

Scale and reliability

  • Queues absorb bursts, for example newsletter sends.
  • Rate-limit outgoing mail per account to stop compromised accounts spamming.
  • Spread incoming servers across regions; MX records can list several with priorities.
  • Privacy: encrypt in transit (TLS between servers) and at rest, and control internal access carefully.

Real-world reading

Check your understanding

Question 1 of 3

0 correct so far

How does a sending mail server find where to deliver a message for [email protected]?

Go deeper

  • How email works: RFC 5321 (SMTP) overview by IETF: search for the standard; the first sections are readable

© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.