Preparing your design workspace

Loading the problem and your latest saved attempt…

Design a Multi-Business Invoice System

Ingest transactions from many small businesses, create each invoice on demand for a customer and time range, return the stored invoice for repeat requests, and make its PDF downloadable.

Problem statement

Design a system that manages transactions and generates invoices for many small businesses. A business sends transaction details to the system for future billing and asks for an invoice for transactions it already sent, and invoices can be searched by customer or by invoice ID. One invoice is made of many transactions.

Only the invoice request names a time range: the invoice is created on demand the first time it is requested and then stored, so a repeated request for the same customer and the same time range returns the stored invoice. Explain when the downloadable invoice becomes available.

For this exercise, assume the businesses are the system's users: each business calls the API for its own end customers, and the searches are made by the business.

Each transaction records the business, the customer ID the business uses, an amount in the business's billing currency, the time it happened, a description and the business's own transaction ID.

Transactions may be sent one at a time or in batches of up to 500, requests from one business can arrive in parallel and out of order, and resending a transaction with the same business transaction ID never records it twice.

A transaction's details and amount never change once accepted; a refund or correction is a new transaction with a negative amount.

A transaction must reach the system within 24 hours of the time it happened, and an invoice can be requested only for a time range that ended at least 25 hours ago, so the range is complete before it is billed.

For this exercise, also assume each transaction appears on at most one invoice: an invoice lists the customer's transactions that happened in its range and are not already on another invoice, with their total, and it never changes once created.

Each invoice has a downloadable PDF document, and the business can see whether it is ready before downloading it. Search means looking up an invoice by its ID or listing one customer's invoices, newest first. A business can see only its own transactions and invoices.

Payment collection, taxes, currency conversion, recurring billing schedules and a portal for the businesses' own customers are outside scope.

What to build

  1. A business can send transactions for future billing; for this exercise, resending a transaction it already sent never records it twice.
  2. A business can request an invoice for one customer and one ended time range; the first request creates and stores it, and every later request for the same customer and range returns that same invoice.
  3. A business can download an invoice's document; for this exercise, it can also see whether the document is ready.
  4. A business can search its invoices by invoice ID or by customer.

At scale

  • For this exercise, assume 200,000 businesses with 20 million customers in total, 30 million transactions a day with peaks of 3,000 per second, and bursts of up to 200 per second from a single business.
  • For this exercise, assume 1 million invoice requests a day; an invoice may include up to 20,000 transactions, and the invoice total and line count come back within 2 seconds at p95 for invoices of up to 10,000 transactions.
  • For this exercise, assume 99 percent of PDF documents are ready within 60 seconds of the invoice being created, and lookups by invoice ID or customer return within 300 milliseconds at p95.
  • For this exercise, assume transactions and invoices are kept for seven years and an acknowledged transaction or invoice survives the loss of one availability zone.