Preparing your design workspace

Loading the problem and your latest saved attempt…

Design an Asynchronous Report Generation System

Build reports of more than 100,000 rows from several databases and services in the background, notify the user when the file is ready, and keep every report complete and exact when workers crash or a service goes down.

Problem statement

Design a system that builds large reports in the background. A user asks for a report whose rows come from several databases and services, and one report can have more than 100,000 rows, too many to build while the user waits. The system tells the user when the file is ready and lets them download it.

What a report is. - For this exercise, assume the user picks a report type and a date range, and the report is a CSV file with one row per record of the user's organization in that range, for example one row per order. - Assume the records come from a main database. Each row also needs columns from a second database and from two internal services, such as a customer service and a payments service. Each service can look up to 500 IDs in one call. - Assume a report includes the records that are in the range when the user asks. Records added or changed while it is being built may or may not appear, but every record appears at most once, and none that existed when the user asked and still exists may be missing. - Assume a report is never delivered with missing rows or blank columns. If a source stays down, the build waits and retries for up to 1 hour, then fails, and the user is told why.

What the user sees. - Assume only the user who asked can see and download a report. The user sees a list of their reports with each one's status. Sending the same request again, for example after a double click or a client retry, does not start a second build. - Assume that when the report is ready, the user gets an email and an in-app notification that both link to it, and that if the build fails, the user is told why. The file can be downloaded for 7 days; after that the user must ask again.

Out of scope: scheduled or recurring reports, choosing report types and their columns, file formats other than CSV, how users sign in, and changing the source data.

What to build

  1. Let a user ask for a report by type and date range and get a report ID at once; sending the same request again does not start a second build.
  2. Build the complete CSV file in the background from the main database, the second database and the two services, with no record twice and no record missing that was in the range when the user asked and still exists.
  3. Tell the user by email and in the app when the report is ready, or when it has failed and why.
  4. Let the user list their reports with their status and download a ready report for 7 days.

At scale

  • For this exercise, assume about 10,000 reports a day, and up to 2,000 in the busiest hour, such as the first morning of a month.
  • Assume most reports have 100,000 to 200,000 rows of about 50 bytes each, so 5 to 10 MB as CSV. The largest have 1,000,000 rows, about 50 MB.
  • Assume that while the sources are healthy, 95% of reports are ready within 5 minutes of the request, and the largest within 30 minutes.
  • Assume the two services also serve the product's live users, so the report system may send each of them at most 200 calls per second in total. Each database has a read replica, a read-only copy kept up to date from the main copy, that the report system may use.