System Design AI

Design a local delivery service like Gopuff: the junior, senior and staff answer

A rapid delivery service runs thousands of small warehouses, often called distribution centres or DCs. A customer opens the app and sees what can reach them within the hour; they order several items; a courier brings them.

It is a good interview problem because its two main paths pull in opposite directions. Reads are enormous and can be a little stale. Writes are small but must be exact.

Requirements to pin down

  1. Availability by location: the items that can reach this address within an hour, with quantities. That is the combined stock of every DC that can reach it in time.
  2. Orders of several items: all lines or none, and never selling a unit twice.
  3. Stock that matches the shelf: deliveries, picks, damage and stock counts from each DC.

The worked design's targets: availability at p99 under 100 ms, stock never below zero, and a scale of 10,000 DCs, 100,000 catalogue items and 10 million orders a day.

The numbers

  • Orders: 10 million a day is about 116 a second on average (10,000,000 / 86,400), say 350 at the evening peak.
  • Availability reads: shoppers browse far more than they buy. At about 200 availability calls per order, that is 2 billion calls a day, about 23,000 a second, and around 60,000 at peak.
  • Stock rows: 10,000 DCs x about 5,000 items each = 50 million rows. At about 100 bytes a row, that is 5 GB.

The order rate is small for a relational database; the read rate is not. That asymmetry decides most of the design.

The data model

Stock is a count per (DC, item), not a row per physical unit. An order has lines; each line takes a quantity of one item from one DC. An idempotency key on the order makes a retried checkout safe.

The junior answer

  • Nearby DCs by drive time. Straight-line distance lies: a river or a motorway can make a close DC unreachable within the hour.
  • Pages served from a read replica, so browsing does not compete with orders.
  • An order is one transaction of conditional decrements. For each line: decrement the quantity where the DC and item match and the quantity is at least what the line needs. If any line updates zero rows, it is short, and the whole transaction rolls back.
  • Lock order. Lines are updated sorted by DC and item, so two baskets that share items lock rows in the same order and queue behind each other instead of deadlocking.
  • Then the read arithmetic that breaks it, said without being asked: tens of thousands of availability reads a second is more than a primary with a replica or two should be asked to serve.

Why the conditional decrement cannot oversell: when two transactions update the same row, the second waits for the first's lock. When the first commits, the second's condition is checked against the new value. If the first took the last unit, the second's condition is now false, and it updates nothing.

What a senior answer adds

  • Geography computed ahead of time. Drive-time areas for each DC, refreshed periodically and cut into hexagonal map cells, so "which DCs reach this address" becomes a lookup instead of a routing query.
  • Stock cached per DC, fed by change data capture. The cache is keyed by DC, because that is what an availability read combines, and it is updated from the database's change stream rather than by every writer remembering to do it.
  • When stock is taken. A short hold taken at checkout and released after about ten minutes, so a basket does not lose items while the customer pays, and abandoned checkouts give them back.
  • Partitioning that keeps every order in one database. Markets are grouped onto clusters, so an order's DCs always live together and the transaction never spans databases.
  • Substitutions, and what a stale page costs: a customer who sees an item that sold a second ago.

What a staff answer adds

  • The promise includes couriers. "Within the hour" depends on stock, roads, pickers and couriers, so courier capacity feeds the ETA, and the design is sized for the busiest night rather than the average.
  • Reconciling with the shelf. A ledger of stock movements with sequence numbers, so the system's numbers can be checked against the warehouse's and corrected.
  • Containing failures. Cells by market across two regions, so a failure touches one slice of the country.
  • Configuration as a control plane, separate from the services that serve traffic, so a bad change to delivery areas cannot take checkout down.

Mistakes that sink this question

  • Reading stock, checking it in application code, then writing it back: two orders both see one unit left.
  • Straight-line distance for "can reach me".
  • Serving every availability read from the primary.
  • Updating basket lines in arbitrary order, then wondering about deadlocks.
  • One giant database for the whole country, with orders spanning shards.

Practise it

Read the free worked design, Design a Local Delivery Service like Gopuff: start with the board for your level. Before you open it, write the order transaction yourself and explain why it cannot oversell.

With Pro ($10/month) you can draw your answer on the practice canvas and get an AI review against the rubric.

Questions people ask

Could the stock live in a key-value store instead of a relational database?

Yes. A key-value store with multi-item transactions and a condition such as available >= n per line can take an order atomically too. A relational database is a good fit here because planning a basket across DCs, holds that expire and pick lists are relational queries, and the order rate is small.

Should stock be reserved when an item goes into the basket?

Usually not. Many baskets are never checked out, so reserving on add would hide stock from real buyers. Taking a short hold at checkout, which expires if payment does not complete, keeps the page honest without locking stock up for browsers.

Why cache stock per DC rather than per location?

An address is served by several DCs, and each DC serves many addresses. Caching per DC means one stock change updates one key, and an availability read combines a handful of DC entries. Caching per location would turn one stock change into updates for every address that DC reaches.