How to design a local delivery service
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- What can reach me within the hour
- Assigning a courier
- Tracking the courier
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- Meta
- Deliveroo
The problem
A local delivery service shows a customer which groceries and household items can reach their door within the hour, takes the order, hands it to a courier and lets the customer watch that courier drive over. The design rests on one imbalance: people look at what's available about 200 times for each order they place, and a unit sitting on a shelf can go to one buyer. So the browse path runs on center data held in memory, cached stock and read replicas, while the purchase itself is a single transaction on the primary, and the courier side is a separate service that keeps positions in memory and pushes them to the customer.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
What does "deliverable" mean here: inside a fixed radius, or arriving within a time window? | A radius is one formula on coordinates; a time window needs road travel time, which means an external call and a reason to prune centers first. | Within 1 hour of ordering. Distance is not the test on its own, so the design needs travel time somewhere on the browse path. |
How many orders, centers and items are we sizing for? | The ratio of browsing to buying decides whether availability needs its own read path, and the center count decides whether a list of centers fits in memory. | About 10 million orders a day, 10,000 distribution centers, a catalog of 100,000 items, and centers considered out to 60 miles. That works out to about 100 orders and 20,000 availability checks a second. |
Can one order be filled from more than one center? | A multi-center order means two stock transactions and two couriers for one order, which doubles the hard parts. Fixing one center per order keeps the transaction local. | Assume one center fills the whole order. An order that spans centers is a variant you can name and park. |
Who packs the order, and when is the courier chosen: at order time or once the bag is packed? | If the courier is picked at order time they wait at the center; if picked at pack time the order may sit packed with nobody to carry it. The choice shapes the assignment path and the dinner-rush story. | Center staff pack. The courier is chosen when the order is packed, and orders with no free courier wait by center. |
May the availability list be a little stale, as long as the order itself is exact? | If browse must be exact, each check has to read a strongly consistent source, which in this design means the primary. If a minute of staleness is fine, replicas and a cache carry the reads and the transaction catches the rare sold-out item. | Yes. The list may lag by about a minute; the order is strongly consistent and no two customers get the same unit. |
Are payments, catalog search, and cancellations in scope? | Each is a system of its own. Naming them out of scope early keeps the hour on availability, assignment and tracking. | Out of scope. Keep a status field on the order so cancellations could be added later, and move on. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Availability answers in under 100 ms | No synchronous read of the primary on the browse path. The list of centers lives in each availability server's memory, stock comes from a cache or a replica, and the travel-time call goes out for a handful of pruned centers at most, with a timeout and a cached answer per area. |
Strong consistency on orders: two customers never buy the same unit | The stock check and the order insert happen in one transaction on the primary, with the inventory and the orders in the same database. Separate stores would need cross-store coordination instead, a distributed lock or a saga with reservations and compensating steps, which is more to get right in the hour. |
10 million orders a day across 10,000 distribution centers | About 100 orders a second of writes, a modest rate that this design puts on one Postgres primary with a standby rather than sharding the writes. The list of 10,000 centers is small enough to hold in memory on every server. Inventory, up to 1 billion rows in the worst case, is partitioned by region for query locality. |
About 20,000 availability queries a second | About 200 reads per order, so reads and writes get different paths: read replicas and a short-lived Redis cache take the browsing, and the primary sees orders. Travel-time lookups are pruned by radius first so the external service does not see 20,000 calls a second times the number of centers. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Availability checks a second | 10 million orders a day ÷ 100,000 seconds ≈ 100 orders a second. If 5% of visits end in an order, that is 100 ÷ 0.05 = 2,000 visits a second; at 10 pages a visit, 2,000 × 10 = 20,000 checks a second | About 20,000 availability checks a second, roughly 200 for each order | The read path: why availability gets replicas and a cache while just the orders touch the primary. |
Stock that changes during one cache lifetime | 100 orders a second × 60 seconds = 6,000 orders during a 1-minute cache entry's life; spread over 10,000 centers, 6,000 ÷ 10,000 = 0.6 orders per center | Under one order per center per minute on average | How stale a 1-minute cache gets: most centers see no sale during an entry's life, so the transaction on order catches the rare sold-out item. |
Inventory rows and bytes | 10,000 centers × 100,000 items = 1 billion rows if every center stocked the whole catalog; at about 100 bytes a row (ids, count, timestamps) that is 100 GB. If a center stocks a tenth of the catalog, 100 million rows ≈ 10 GB | Between about 10 GB and 100 GB | Whether inventory fits one Postgres primary with replicas: it does. Partitioning by region is for keeping each query inside one slice, not for raw size. |
Deliveries in flight | Assume a courier holds an order about 30 minutes from pack to drop-off: 100 orders a second × 1,800 seconds = 180,000 deliveries in progress at once on average | About 180,000 couriers on the road, and as many customers who could be watching a map | Open tracking connections and the size of the busy-courier set. |
Courier position writes a second | 180,000 active couriers sending a position every 5 seconds = 36,000 writes a second; if each row were kept, 36,000 × 86,400 seconds ≈ 3.1 billion rows a day. Customers polling every 5 seconds would add another 36,000 reads a second | About 36,000 position writes a second, and about 3.1 billion rows a day if they were stored | Why positions stay in memory as a latest-only value, and why pushing beats polling for the customer side. |
Travel-time calls without pruning | 20,000 checks a second × 10,000 centers = 200 million travel-time lookups a second. If the 60-mile cut leaves about 10 centers per check, 20,000 × 10 = 200,000 a second | 200 million a second unpruned, about 200,000 a second after the radius cut | Why the radius cut comes first and why travel times are cached per center and area rather than fetched per check. |
Orders per center | 10 million orders a day ÷ 10,000 centers = 1,000 orders a center a day, about 0.01 a second on average | About 1,000 orders a center a day | The per-center waiting queue and courier pool: small numbers, so a per-center list in Redis is enough. |
Dinner peak | Assume the dinner hour runs at 3 times the daily average: 100 × 3 = 300 orders a second, 20,000 × 3 = 60,000 availability checks a second, 180,000 × 3 = 540,000 deliveries in flight | About 300 orders and 60,000 checks a second at the peak, with about 540,000 tracking sessions open at once | The headroom replicas, cache and WebSocket servers need, and the size of the waiting queues when couriers run out. |
Core entities
| Entity | What it holds |
|---|---|
Item | A kind of product in the catalog: the thing a customer searches for, not a unit on a shelf. |
Inventory | The stock of one item at one center, with the count the order transaction decrements. |
Distribution center | A warehouse with a location, held in memory on each availability server and refreshed every 5 minutes. |
Order | A basket bought from one center, with its address, its courier once assigned, and a status that moves forward. |
Courier | A driver with a status that flips between free and busy, and a latest position kept in memory rather than in this table. |
The API
| Endpoint | Notes |
|---|---|
| The hot path. The server prunes centers by radius, asks travel time for the survivors, reads their stock from cache or a replica and returns the union of items with a count. |
| Picks the center that can fill the whole basket in time, then in one transaction decrements each inventory row (refusing if any count would go below zero) and inserts the order. A failed check returns |
| Called by the courier app every few seconds. It updates the courier's entry in the in-memory position index and their |
| The customer who owns the order opens one connection; the server pushes the courier's position and the arrival time as they change. A client without a persistent connection polls the same path and gets the latest pair. Unknown or foreign order: |
The high-level design
Step 1 of 4
Browse and order against one database
Start with the two things a customer does. An availability service answers GET /availability by reading stock rows from Postgres, and an orders service takes POST /orders, decrements the inventory rows and inserts the order in one transaction on the same primary, so two customers can't both get the last unit. They are separate services because the two paths will scale differently: about 20,000 checks a second against about 100 orders. This step has no idea which centers are close enough to deliver, and each browse hits the primary.
What breaks first
The deep dives
What can reach me within the hour. A customer opens the app at home. How do you produce the list of items that some center can get to their door within 1 hour, in under 100 ms, about 20,000 times a second?
Assigning a courier. An order is packed at the center. How does the system pick one courier for it, and only one, when another order at the same center is packed in the same second?
Tracking the courier. The courier is on the way. How does a position sent from the courier's phone reach the customer's screen within seconds, with an arrival time, for each delivery in flight at dinner?