Preparing your design workspace

Loading the problem and your latest saved attempt…

Design a Hosted Email Service

Design the back end of a hosted email service: take in mail sent by its users and by outside mail servers, file each message into every hosted recipient's mailbox (group addresses included), hand outside mail to other providers with retries, and list every folder newest first.

Problem statement

For this exercise, assume you are building a hosted email service of the kind people use for personal and work mail. Users read and write mail in a web app and in phone apps. They write to other users of the service and to people at other email providers.

Mail from other providers arrives over SMTP (Simple Mail Transfer Protocol), the standard protocol mail servers use to pass messages to each other.

The service you design has three jobs: taking in mail, both from its users and from other providers' servers; filing each message into each hosted recipient's mailbox; and showing users their folders.

A message can name many recipients, and it can be sent to a group address the service hosts, such as a team list, in which case all of the group's members get it. Mail for people elsewhere is handed to their providers' servers over SMTP.

When such a server is unreachable, the service keeps trying for a while; if it still cannot hand the message over, the sender gets a failure notice in their Inbox.

Also assume: after the service has confirmed a message, either to the user who sent it or to the remote server that passed it in, the message is never lost.

Remote servers sometimes send a message a second time when our confirmation did not reach them, and users sometimes press Send twice on a slow network; even so, each recipient sees each message only once.

A spam and virus filter already exists as a service you can call with a message to get a verdict. Two folders are enough: Inbox and Sent. Search, filters and rules, other folders and labels, contacts, end-to-end encryption, live push updates to open apps, and the apps' screens are out of scope.

What to build

  1. A user sends a message with a subject, a body and optional attachments to up to 100 addresses (To, Cc or Bcc), hosted here or elsewhere. The app learns whether the send was accepted, and the sender keeps a copy in Sent.
  2. The service takes in mail that other providers' servers pass to it for hosted addresses. Mail the filter rejects is refused; the rest is accepted.
  3. Each accepted message gets to all of its recipients. Hosted recipients, and all members of a hosted group address, find it in their Inbox. Recipients elsewhere get it through their providers' servers; if that still fails after 3 days of retries, the sender gets a failure notice.
  4. A user opens Inbox or Sent and sees messages newest first, with sender, subject, a short preview, time and an unread marker. They can load older mail page by page, view any message's full body and attachments, and mark it read or unread.

At scale

  • For this exercise, assume 200 million mailboxes, with around 60 million people active on a typical day. Users send about 300 million messages a day and other providers pass in about 1.2 billion. Counting each hosted recipient separately, that is about 2 billion mailbox deliveries per day: an average of roughly 23,000 per second, peaking near 70,000 per second in the busiest hour.
  • For this exercise, assume messages average about 100 KB including attachments, with a 20 MB cap per message. That is about 150 TB of new mail a day, kept until the user deletes it. Most messages go to one or two people; a hosted group address may hold as many as 20,000 members.
  • For this exercise, assume a message for a hosted recipient shows up in their Inbox within 10 seconds of being accepted in 99% of cases, and a folder's first page loads in under 300 ms. At peak, users open folders about 100,000 times per second.
  • For this exercise, assume confirmed mail must survive the loss of any server or of a whole data center, and that another provider's server may stay unreachable for hours at a time.