Design a Contact-Centric Email Inbox
Accept incoming and outgoing email, deliver every message to each recipient's mailbox, including large distribution lists, and show each user their mail grouped by the person they exchanged it with.
Problem statement
For this exercise, assume an email service in which each user's mail is organized around the people they correspond with. Mail arrives all the time, both from other mail providers and from the service's own users, and every message has to land in the mailbox of each of its recipients.
When users open their mailbox they see their contacts, and selecting a contact shows every message they have exchanged with that person.
Also assume an existing inbound mail gateway already handles the internet mail protocol, filters spam and viruses, and hands each message from another provider to your service together with the list of your users it is addressed to. Mail your users send to other providers is passed to an existing outbound relay.
Your service owns accepting messages, delivering them into mailboxes, and the per-contact views.
Assume a message belongs to the conversation with each person on the other side of it: a message a user receives appears under its sender, and a message a user sends appears under each address it was sent to, with a distribution list address counted as one contact.
Messages are ordered by the time your service accepted them, newest first. A message may be addressed to many people directly or to a distribution list, and it must reach the mailbox of every recipient, including every member of the list.
Assume that once your service has told the gateway or the sending user that a message is accepted, it must never be lost, and no recipient may see the same message twice, even though the gateway sometimes hands over the same message again when an acknowledgment is lost.
Search, folders and rules, read and unread state, deleting or moving messages, grouping by subject, and the design of the push channel to open clients are out of scope.
What to build
- The service accepts messages handed over by the inbound gateway and messages its own users send, and tells the gateway or the sender when a message has been accepted.
- Every accepted message reaches the mailbox of each of its recipients on the service, including every member of a distribution list it was sent to, and appears in the sender's own mailbox when the sender is a user of the service.
- A user sees their contacts ordered by the most recent message exchanged with each one, with that message's subject and time.
- Selecting a contact shows every message exchanged with that contact, both sent and received, newest first; the user can page back through the whole history and open any message.
At scale
- For this exercise, assume about 100 million mailboxes and about 4 billion deliveries a day, where a delivery is one message landing in one mailbox: about 46,000 a second on average and up to about 150,000 a second at peak. Contact list and contact view reads peak at about 20,000 a second.
- For this exercise, assume about 1 billion distinct messages a day, averaging about 50 KB with attachments and at most 25 MB. Most messages have one to three recipients; a message may name up to 500 addresses directly, and a distribution list may have up to 50,000 members.
- For this exercise, assume these targets: a message to a few recipients is visible in each recipient's contact list and contact view within 5 seconds of acceptance at the 99th percentile, even while large list messages are being delivered; a message to a 50,000-member list reaches every member within 2 minutes; and the contact list or the first page of a contact view loads within 200 ms at the 99th percentile.
- For this exercise, assume a heavy mailbox has up to 20,000 contacts and 2 million messages, and one contact, such as an automated sender, may have up to 100,000 messages with one user.