Library / Blogs

Khan Academy Engineering Blog

The engineering blog of Khan Academy: posts on how it designs, builds and scales its systems.

By Khan Academy engineers · Khan Academy · Added 2026-10-12

Open original source

All content belongs to Khan Academy engineers / Khan Academy. We only link to it and give credit. Report a problem

Learn this article

About 37 min left. Your work is saved as you go.

What you are about to study

Khan Academy wrote this to explain how they thought about a problem. You should know the basics: caching, queues and replication.

Ideas you will meet

Distributed Systems
Consensus, time, failure and the fundamentals of scale-out.

What we already know about this

Our own lessons on these ideas. Read them here before you start, so it makes sense.

  • Real-World Systems · 11 minDesign 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.Read here

    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

    Open this lesson with its quiz and exercise

Go deeper on this

A path through videos, articles, other case studies and code on the same ideas as Khan Academy Engineering Blog.

  1. Warm upGet the vocabulary first.

  2. WatchHow others solve the same problem, on video.

    Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)

  3. ReadEngineers explain it in their own words.

  4. See it in another companyThe same ideas, a different system.

  5. Read the codeSee how it looks when it is real.

Keep learning

Loading community…