Full answer
Requirements
Functional:
- Receive and send mail with attachments using standard protocols (SMTP, and IMAP for third-party clients).
- Folders and labels, threads, search over all mail.
- Spam filtering and notifications for new mail.
Non-functional:
- Accepted mail is never lost.
- The inbox loads quickly; search returns in about a second.
- Storage is the biggest cost.
Capacity estimates
Assumptions: 100M users, 40 incoming emails per user per day, 50 KB average size including attachments.
- Incoming: 100M x 40 / 86,400 = about 46,000 emails per second.
- New storage: 100M x 40 x 50 KB = 200 TB per day, about 73 PB per year before deduplication and compression.
- Attachments sent to many people (a newsletter PDF) are stored once and referenced, which cuts storage a lot.
API
GET /v1/messages?label=INBOX&cursor=.. -> list of headers and snippets
GET /v1/messages/{id} -> full message with attachment links
POST /v1/messages/send {"to": [...], "subject", "body", "attachment_ids": [...]}
GET /v1/search?q=from:registrar has:attachment after:2026/11/01
POST /v1/messages/{id}/labels {"add": ["STARRED"], "remove": ["INBOX"]}
Data model
messages: (user_id, message_id) primary key, thread_id, from, to, subject, snippet, labels, received_at, body_ref, size. Partitioned by user_id so a user's mailbox is on one shard.bodies: content-addressed blobs in object storage (key = hash of the content).- Search index: per-user inverted index over subject, body text, sender and attachment names.
High-level design
mermaid
flowchart LR
EXT[Other mail servers] -- "SMTP" --> MTA[Inbound MTAs]
MTA --> SP[Spam and malware filter]
SP --> ST[Store service]
ST --> MD[(Message metadata DB)]
ST --> OS[(Blob storage)]
ST -- "new mail event" --> IX[Indexer]
IX --> SI[(Search index)]
ST -- "new mail event" --> NT[Push notifications]
C[Web and mobile clients] --> API[Mail API]
API --> MD
API --> SI
API --> OS
API --> OUT[Outbound MTAs] --> EXTMain flows
- Receive. The MTA accepts the message only after it is safely queued on disk (otherwise the sender retries later, which SMTP supports). Checks: SPF, DKIM and DMARC for sender authenticity, spam scoring, virus scanning. The store service writes the body blob and the metadata row, then emits an event for indexing and notifications.
- Read. The inbox list is a range read of the user's partition by label and time.
- Send. The API stores the message in Sent, then the outbound MTA delivers it, retrying with backoff for hours if the receiving server is unavailable.
Bottlenecks and trade-offs
- Storage cost. Deduplicate attachments, compress bodies, and move old mail to cheaper storage classes while keeping metadata hot.
- Search freshness. Indexing is asynchronous, so a just-arrived email may take a few seconds to be searchable.
- Big mailboxes. A user with millions of messages stays on one partition; that is fine for storage but heavy for search, so per-user index segments are merged in the background.
- Spam filtering trades false positives (lost real mail) against false negatives; the spam folder is the safety net.
- Sending reputation. Outbound IPs must be protected from abusive senders with rate limits, or the whole service's mail starts landing in spam elsewhere.
By level
Fresher: Explain that mail arrives at mail servers, is checked for spam, and is saved with its details in a database and its contents in file storage. A fresher should describe how the inbox list is loaded.
Mid: Size the system, partition by user, deduplicate attachments and design async indexing and notifications. Explain retries in SMTP.
Senior: Discuss storage tiering, large mailboxes, sender authentication, spam trade-offs, outbound reputation and deletion of shared blobs. Expect cost-driven reasoning.
Key points
- SMTP in through MTAs that accept only after durable queueing
- Metadata partitioned by user; bodies in blob storage
- Store identical attachments once
- Asynchronous per-user search indexing
- About 46,000 emails per second and 200 TB per day before deduplication
Common mistakes
- Storing full message bodies and attachments in the relational database
- Accepting mail before it is durably stored
- Indexing search synchronously on the receive path
- Ignoring outbound sending reputation
Follow-ups
- How would you implement conversation threading?
- How do you support IMAP clients efficiently?
- How would you delete an attachment shared by many messages?
Tips
- Separate metadata, blobs and the search index
- Do the storage math; it drives most decisions
- Mention SPF, DKIM and DMARC
Evaluation rubric
- Correct receive, store and read paths
- Capacity math and storage strategy
- Search and notification design
- Spam and reputation trade-offs