Classic design · Senior
Distributed email service (Gmail)
Classic interview design. Key points: blob and video delivery, and SMTP/IMAP.
Short answer
Classic interview design. Key points: blob and video delivery, and SMTP/IMAP. An email service receives messages from other domains, stores each in the right mailbox, and lets people read them. Between servers the message travels over SMTP; the client accesses the messages stored on the server over IMAP.
01 · CONTEXT
What an email service needs to do
An email service receives messages from other domains, stores each in the right mailbox, and lets people read them. Between servers the message travels over SMTP; the client accesses the messages stored on the server over IMAP.
This case relies only on public sources: the IETF standards, the Dovecot server documentation, and that of the Apache SpamAssassin filter.
A message rarely goes straight from sender to final destination. Intermediate hosts are normally discovered through the DNS MX record, and a relay server that accepts the task becomes an SMTP client itself and opens the channel to the next server.
02 · DELIVERY OVER THE INTERNET
SMTP, DNS, and retry queues
The critical point of ingestion is the success reply: after it, the standard requires that the server that issued it take responsibility for delivering the message or reporting the failure.
In this reading, ingestion is designed with a message queue between the SMTP server and message processing.
The sender's server queries DNS to find out which servers receive email for the destination domain.
It opens the SMTP connection to the indicated inbound server and transmits the message.
The inbound server hands the message to the inbound queue; the success reply to the sender marks the transfer of responsibility.
A consumer takes the message off the queue and runs it through the anti-spam filter, detailed further on.
The content goes to message storage and the mailbox index records the new message.
A message that cannot be transmitted immediately must be queued and retried periodically, and the queue entry holds the message along with the envelope information. The standard asks that the interval between attempts be at least 30 minutes and that the give-up time be at least 4-5 days. A temporary error in the MX lookup also sends the message to the queue for another attempt.
03 · MAILBOX STORAGE
Mailbox formats and IMAP access
The Dovecot documentation lists mailbox formats that organize messages on disk in quite different ways.
| Formato | Organization | What the documentation highlights |
|---|---|---|
| mbox | A single file contains several messages | Traditional UNIX mailbox format |
| Maildir | One file contains one message | Files are never modified and operations are atomic |
| dbox | Messages in one or more files, each with one or more messages | Dovecot's own high-performance format |
In Maildir, the tmp directory is used during delivery. The documentation also recommends keeping index files on a different disk from the message spool, which gives better performance.
On the client side, IMAP lets you access and manipulate the messages that stay on the server. Messages are referred to by sequence numbers or by unique identifiers, the protocol provides for flags and selective fetching of message parts, and the offline client can resynchronize with the server. IMAP also requires a unique, permanent identification number per message.
Persistent identifiers are what let the client resynchronize the state of a previous session, and any change to them between sessions must be detectable through the UIDVALIDITY mechanism.
The email client opens an IMAP session and accesses the messages stored on the server.
The IMAP server reads the messages' identifiers and flags from the mailbox index.
When a message is opened, the server fetches only the requested parts from storage.
To send, the client hands the message to the submission server, which puts it on the outbound queue.
The SMTP transmitter consumes the outbound queue and retries delivery until it succeeds or gives up.
A design proposal for the interview: store the raw content of messages and attachments in object storage and keep in a document database only each message's metadata, such as folder, flags, and identifier. The IMAP server would answer listings and flag filters from the index and only go to object storage to read content. This separation covers the database choice and the data modeling, and a text search would require a separate search system.
04 · ANTI-SPAM
Authenticate the sender and score the content
The transport protocol does not stop anyone from impersonating another domain: existing protocols do not restrict what a sending host can use as a message's MAIL FROM. Three public mechanisms address this problem. SPF lets a domain explicitly authorize the hosts that may use its name and lets the receiver verify that authorization. In DKIM, responsibility is validated by a cryptographic signature and by directly querying the signer's domain for the public key. DMARC lets the originating organization express validation, disposition, and reporting policies that the receiver can use.
Apache SpamAssassin is a filter for classifying email and blocking spam: it uses a scoring framework and plug-ins that integrate heuristic and statistical tests on headers and body, including a Bayesian filter, DNS-based blocklists, and collaborative databases. According to the project, the wide variety of local and network tests identifies spam signatures, and the filter can be integrated at any point in the email flow.
The incoming message goes from the inbound server to the authentication checker.
The checker verifies the host's authorization through SPF and obtains the signer's public key by querying their domain, in DKIM; DMARC expresses the domain policy that the receiver can use.
The message goes to content scoring, which adds up tests on headers and body, including DNS-based blocklists.
Scoring consults the rules and the statistical filter's data, kept apart from the message flow.
Depending on the classification, the message is stored in the designated folder, such as the inbox or the spam folder.
05 · FULL PICTURE
Putting the pieces together and what to defend
Putting the paths together, the service has an inbound path (DNS, SMTP server, queue, filter, and storage), an access path (IMAP or webmail reading the index and the storage), and an outbound path (submission, queue, and transmitter). All three share the same message storage.
Inbound: external servers discover the destination through DNS, the SMTP server accepts the message and puts it in the inbound queue.
Processing: the filter consumes the queue, classifies the message, and writes it to storage and to the index.
Access: the client reads folders and messages through the IMAP server, which uses the index and the storage.
Outbound: the client sends through the submission server, the message waits in the outbound queue and goes on to external servers.
Frequently asked questions
What is Distributed email service (Gmail)?
Classic interview design. Key points: blob and video delivery, and SMTP/IMAP.
What does the Distributed email service (Gmail) design cover?
It is organized in these sections: What an email service needs to do, SMTP, DNS, and retry queues, Mailbox formats and IMAP access, Authenticate the sender and score the content, and Putting the pieces together and what to defend.
How long is Distributed email service (Gmail) and what level is it?
It takes about 16 minutes to read and is rated Senior level.
Now design it yourself
Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.