Skip to content

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
Asked at
Practice this question

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

AskWhy it mattersA 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.

The requirements menu

RequirementScopeWhy

See which items can be delivered to my location within 1 hour

Core

The front door of the product and the path that carries almost all the traffic. It needs travel time, not distance, which is what makes it a design problem.

Order several items at once

Core

A basket means one transaction over several inventory rows, and a basket that one center can fill in full.

Assign a courier to each order

Core

One courier per packed order, and no two orders claiming the same courier at the same moment.

Track the courier in real time until delivery

Core

Positions arrive every few seconds from each courier on the road and have to reach the one customer waiting for that order, with an arrival time.

Hold the items for a few minutes while the customer checks out

Optional

Worth taking if asked: a reservation row with an expiry keeps the basket from vanishing at checkout. Leave it otherwise, because the expiry sweeper and the release path add a second state machine to explain.

Substitute an item when the picker finds the shelf empty

Optional

Real stores miscount stock, so this happens. Take it as a one-line order status if time allows; the full flow needs a customer approval step, which is its own feature.

Notify the customer at each step: packed, on the way, delivered

Optional

Cheap to add once the order has a status field and the courier service emits events. Mention it, draw one arrow to a notification service, and don't spend more time on it.

Fill one order from two centers at once

Out of scope

Two stock transactions for one basket, and either two couriers or one courier making two pickups, so the assignment path gets a second branch. Say you assume one center per order and why, then park it.

Scheduled deliveries for a later time slot

Out of scope

A slot system is capacity planning per center per hour, a different problem from "can it reach me now". Leave it out unless the interviewer steers there.

Courier earnings, ratings and tips

Out of scope

Ordinary records with no scale or consistency challenge in this round.

Payments

Out of scope

A payment provider and its retries are a separate system. Assume the charge happens once the order commits and leave the details out.

Search and the catalog

Out of scope

The keyword on the availability call is a filter over items the nearby centers hold, not a search engine. Say that and move on.

Cancellations and returns

Out of scope

A cancellation may need to return stock and free a courier depending on how far the order got, and a return has its own restocking rules. The status field makes the states expressible, but the flows are long and don't test anything new.

Non-functional requirements

RequirementWhat 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

QuantityWorkingResultWhat 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

EntityWhat 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

EndpointNotes

GET /availability?lat&long&keyword&page → items deliverable here

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. keyword filters that list; it is not a catalog search. The customer comes from the token, and page keeps the response small.

POST /orders { items, location } → Order

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 409 Conflict with the items that ran out. Take an idempotency key so a retried request does not place two orders.

POST /couriers/location { lat, long } (courier from the token)

Called by the courier app every few seconds. It updates the courier's entry in the in-memory position index and their last_seen_at; it writes nothing to Postgres. The response is empty; the courier app does not wait on it.

GET /orders/{id}/tracking → courier position and ETA, over a WebSocket or by polling

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: 404.

The high-level design

browseorderstockCustomer appAvailabilityOrdersPrimary

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.

browseordercenterstravelstockmisstransactionclearreplicatepackedclaimpositionlatestETApushCustomer appAvailabilityserviceOrders serviceNearby serviceTravel-timeserviceRedis cacheInventoryreplicasPostgres primaryCourier serviceCourier appCourier positionsWebSocketservers
The whole design

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?

A concept that helps here: Geospatial indexingA concept that helps here: Replication and read replicasA concept that helps here: Cache invalidation and TTLs

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?

A concept that helps here: Optimistic and pessimistic lockingA concept that helps here: Distributed locks and leasesA concept that helps here: Queues and logs

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?

A concept that helps here: WebSockets, server-sent events and pollingA concept that helps here: Heartbeats and presenceA concept that helps here: Routing messages between stateful servers

Concepts this guide uses

Sources

  1. Educative: GoPuff System Design Explained
  2. System Design School: Local Delivery (Gopuff) system design solution
  3. Redis documentation: Geospatial data type
  4. MDN: The WebSocket API (WebSockets)
  5. PostgreSQL documentation: SELECT
  6. Uber Engineering: H3, Uber's Hexagonal Hierarchical Spatial Index
Sign in to report a mistake