System design interview: design an email service like Gmail
An email service receives messages from the internet over SMTP, filters out spam and malware, stores each user's mailbox, lets them search and organize it from many devices, and sends their outgoing mail with retries. The interview tends to focus on storage (small metadata read constantly, large bodies read rarely), on keeping every device in sync, and on the delivery and anti-abuse rules that make email different from chat.
By Evin JohnPublished 8 min read
1. Clarify the requirements
This page designs a mailbox product in the style of Gmail or Outlook.com, without describing how either is built. If the interviewer means a transactional sending service (password resets, receipts), that is closer to the notification system walk-through; confirm which before you start.
- Functional: receive mail for our addresses; send mail to anyone; read, reply, forward; threads (conversations); labels or folders; search the whole mailbox; attachments; spam folder; access from web, mobile apps and standard clients (IMAP).
- Non-functional: never lose an accepted message; new mail shows up on all devices within seconds; opening the inbox is fast; search returns in about a second; storage cost per user stays low because mailboxes only grow.
- Out of scope unless asked: calendar and contacts, encryption beyond transport and at rest, enterprise admin and legal hold.
2. Estimate the scale
| Quantity | Assumption | Result |
|---|---|---|
| Users | 100 million | the base for the rest of the table |
| Inbound mail | 40 messages per user a day, before spam is dropped | 4 billion a day, about 46,000 a second |
| Outbound mail | 5 messages per user a day | 500 million a day, about 5,800 a second |
| Size | about 75 KB average including attachments | about 300 TB of new mail a day before deduplication and compression |
| Mailbox reads | users open the app many times a day; most reads are lists of recent metadata | read traffic dominated by small metadata queries |
Storage is the cost to watch. Most of the 300 TB a day is attachments, many of them identical (the same newsletter image or the same PDF sent to a list), which is why content-addressed attachment storage pays off.
3. Define the API
GET /v1/messages?label=INBOX&pageToken=... -> 200 { messages: [{ id, threadId, from, subject, snippet, labels, date }] }
GET /v1/messages/{id} -> 200 { headers, body, attachments: [{ id, name, size }] }
POST /v1/messages/send { to[], cc[], subject, body, attachmentIds[], idempotencyKey } -> 202 { id }
POST /v1/messages/{id}/modify { addLabels[], removeLabels[] }
GET /v1/search?q=from:alex invoice has:attachment
GET /v1/changes?since=8812 -> 200 { changes: [{ version, type, messageId }], latest: 8840 }4. Sketch the data model
| Data | Store | Key and fields |
|---|---|---|
| Message metadata | Wide-column or sharded relational store | (user_id, message_id): thread, from, to, subject, snippet, date, labels, size, body pointer |
| Label index | Same store, separate table | (user_id, label, date desc, message_id) so "Inbox, newest first" is one range scan |
| Bodies | Blob storage | Compressed MIME body per message, immutable |
| Attachments | Blob storage, content-addressed | Key = hash of the content; a reference count or a periodic sweep removes unused blobs |
| Change log | Per-user append-only table | (user_id, version): what changed, used by device sync |
| Search index | Per-user inverted index segments | Terms from headers and body, built asynchronously |
5. Receiving mail
- AcceptSending servers find our inbound (MX) servers through DNS and connect over SMTP. Our servers check that the recipient exists, apply connection-level limits and reputation checks, and accept the message.
- Authenticate the senderCheck SPF (was this server allowed to send for that domain?), DKIM (does the signature match the domain's published key?) and DMARC (the domain's policy when those fail).
- ScanRun malware scanning on attachments and a spam classifier on content and sender reputation. Spam is labeled, not deleted, so users can recover mistakes.
- Queue, then storeWrite the message to a durable queue and only then acknowledge it with "250 OK". Delivery workers write the body and attachments to blob storage, the metadata row, label index entries and a change-log entry, and trigger indexing and push notifications.
The order matters. Once our server has said "250 OK", the sender considers the message delivered and will not retry, so the message must be durable at that point. Writing to a replicated queue before acknowledging makes that true without waiting for every downstream store.
6. Threads, labels and search
- Threads. Group messages using the In-Reply-To and References headers, falling back to a normalized subject within a time window. Store
thread_idon each message and keep a small thread summary row for the list view. - Labels as rows, not copies. A message with three labels is stored once and appears in three label index entries. Moving a message to a folder is just a label change.
- Per-mailbox search. Each user's index is small, so build it per mailbox: a delivery event adds the new message's terms to an in-memory or recent segment, merged into larger segments later. Search queries never fan out across users; they go to the shard that owns the mailbox. The search engine walk-through explains the index structure at web scale; here the same ideas run at mailbox scale.
- Operators. Parse
from:,has:attachmentand date ranges into metadata filters, and send free text to the index.
7. Sending mail
When a user presses Send, the message is stored in their Sent label, written to an outbound queue and acknowledged to the app. Outbound workers look up each recipient domain's MX servers, sign the message with DKIM, and deliver it over SMTP. Temporary failures (a busy server, greylisting) are retried with growing delays over many hours before the sender gets a bounce message. Our own users on the same service are delivered internally without SMTP.
- Outbound reputation. If compromised accounts send spam, other providers start rejecting all our mail. Rate-limit new accounts, scan outbound mail, and keep separate sending IP pools for different risk levels.
- Idempotent send. The client's idempotency key stops a double tap or a retry on a bad connection from sending twice.
- Large recipient lists. Split per recipient domain so one slow domain does not delay the rest.
8. The architecture
- Internet
- Other mail servers
- DNS (MX, SPF, DKIM, DMARC records)
- Inbound
- SMTP receivers
- Auth checks, malware scan, spam classifier
- Durable inbound queue
- Mailbox
- Delivery workers
- Metadata + label index (by user)
- Blob storage (bodies, deduplicated attachments)
- Per-user search index
- Access
- API servers (web, mobile)
- IMAP front-end
- Push notifications
- Outbound
- Outbound queue
- DKIM signing
- SMTP senders with retry schedule
9. What breaks, and trade-offs to discuss
| Failure | Effect | Mitigation |
|---|---|---|
| Mailbox store slow or partly down | Delivery lags | Inbound keeps accepting into the queue; delivery workers catch up; senders never see errors |
| Spam wave | Inbound load spikes, users annoyed | Connection-level reputation blocks before content scanning; adaptive rate limits per sending IP |
| A device misses changes while offline | Inbox out of date | Sync from its last version; if the change log was trimmed, the server returns a full resync signal |
| Our IPs land on a blocklist | Outbound mail rejected widely | Separate IP pools, outbound abuse detection, monitoring of bounce rates per destination |
| Attachment blob deleted while still referenced | Broken messages | Delete only after a sweep confirms no references, with a grace period |
- Wide-column or relational for metadata. Wide-column stores fit the "all rows for a user, sorted" access pattern and scale writes easily; relational stores give transactions for label changes but need careful sharding by user.
- Index on delivery or lazily. Indexing every message as it arrives costs CPU for mail nobody searches; indexing lazily makes the first search slow. Many designs index promptly but in batches.
- Deduplicate attachments or not. Content-addressed storage saves a lot of space but needs reference tracking and careful deletion, which matters for privacy requests.
- Push or poll for devices. Push gives instant updates and saves battery compared with frequent polling, but needs per-platform push channels and a fallback.
Risky changes such as a new spam model are best rolled out to a small share of mailboxes first, controlled by a feature flag service.
10. How to talk through it in the interview
Fictional example
- Interviewer
- A sender's server hands you a message. When do you acknowledge it?
- Candidate
- After it is in a replicated queue, not before and not after the mailbox write. Once I say 250 OK the sender deletes its copy, so I must not lose it. Waiting for the mailbox, search index and push would make acceptance depend on every downstream system.
- Interviewer
- And if spam scanning is slow?
- Candidate
- Cheap checks such as the recipient, sender reputation and SPF happen before acceptance. The heavier content classifier can run after queueing, and the message goes to Spam or Inbox when it finishes, usually within seconds.
- Draw inbound, mailbox and outbound as three lanes and say email is federated.
- Separate metadata from bodies and justify it with the read pattern.
- Explain device sync with versions and a change log; it is the most common follow-up after storage.
Practicing this design with Natively
An email design has many moving parts, so practice keeping a steady order when the interviewer jumps between them. Set up a mock call and run Natively on your Mac or Windows PC: it transcribes the conversation and can suggest talking points for the question just asked, such as DKIM signing or when to acknowledge inbound mail. Request a diagram of the three lanes if you want something to compare with your board. Then read the saved notes and check whether you ever explained how a phone catches up after a day offline.
Questions people ask
How would you design Gmail in a system design interview?
Split it into three paths. Inbound SMTP servers authenticate, scan and queue mail before acknowledging it; delivery workers store bodies and attachments in blob storage and metadata, labels and a change log in a store partitioned by user; outbound workers sign and deliver sent mail with retries. Devices sync through the change log and search a per-mailbox index.
How is email stored in a large email service?
Metadata such as sender, subject, labels and thread goes in a store partitioned by user and sorted for list views, while bodies and attachments go to blob storage. Attachments are often stored once by content hash and shared across messages, which saves a lot of space because many are identical.
How do email apps stay in sync across devices?
Each mailbox keeps an append-only change log with an increasing version number. A device remembers the last version it saw and asks for changes since then, applying new messages and label changes. If it has been offline so long that the log was trimmed, it does a full resync.
What are SPF, DKIM and DMARC?
They are DNS-published rules that let receivers check mail claiming to come from a domain. SPF lists servers allowed to send for it, DKIM adds a cryptographic signature verified with the domain's public key, and DMARC tells receivers what to do when those checks fail. A sending service signs its outgoing mail so others accept it.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
