Gmail
Email Service
Design a web email service that receives mail from the whole internet over SMTP, filters spam, stores years of mail per user, and lets people search, label and sync it on every device without losing a message.
Overview
An email service has two very different halves. The transport half speaks SMTP with every other mail server on the internet: it receives mail addressed to its users and sends their mail out. The mailbox half is an application: it stores each user's mail, groups it into conversations, searches it and keeps phones and browsers in sync.
What makes it hard is that email is federated and old. Anyone can connect and hand you a message, so the service must decide who the sender really is and whether the message is spam before a user sees it. Once the service says it has accepted a message, the SMTP rules make it responsible for delivering it or reporting failure, so it can never quietly lose one. And users keep mail for years and expect to find any message in a second.
Questions to ask first
- Users and volume? — 1 billion daily active mailboxes; 40 messages received and 10 sent per mailbox a day
- Attachments? — Up to 25 MB per message; bigger files become a link to file storage
- Storage per user? — A quota of about 15 GB, shared with other products
- Clients? — Web, mobile apps, and third-party clients over IMAP
- Search? — Full text within one user's mailbox, with operators like from: and has:attachment
- Focus? — Both halves, with the mailbox side deeper
What you will be able to do
- Explain how SMTP delivery works and exactly when the service becomes responsible for a message.
- Describe SPF, DKIM and DMARC, and a layered spam pipeline built on them.
- Lay out mailbox storage: metadata, bodies and attachments, partitioned by user.
- Model labels and conversation threads, and build per-user search.
- Keep clients in sync with a change log, and send mail out with retries and bounces.
Key concepts
- SMTP and MX records
- Accept, then take responsibility
- SPF, DKIM, DMARC
- Layered spam filtering
- Metadata vs blobs
- Labels, not folders
- Threading headers
- Per-user search index
- History-based sync
- Outbound retry and bounces
Functional requirements
- Receive mail for our users from any other mail server
- Send mail, with attachments up to 25 MB, to any address
- Put spam in a Spam label and warn about unauthenticated senders
- Group messages into conversations; apply labels, stars, archive and trash
- Search a mailbox by words and by operators (from:, has:attachment, after:)
- Sync every change to web, mobile and IMAP clients quickly
Non-functional requirements
- No accepted message is ever lost (durable before the SMTP reply)
- Mail between our own users appears within a few seconds (a target we chose)
- Search returns in under 500 ms at p99 for a mailbox of 1 million messages (a target we chose)
- Reading and writing a mailbox stays available during a data center outage
- Each user sees only their own mail
Interview questions
- How do you make sure no email is ever lost?
- What are SPF, DKIM and DMARC, and why do you need all three?
- How would you store billions of mailboxes?
- How does conversation threading work?
- How do you make search fast across years of mail?
- How do phones and browsers stay in sync?
- What happens when the recipient's mail server is down?
- How do you keep spam out without losing real mail?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio