AlgoViz

Design a food delivery service like DoorDash

hard 60 min

A customer orders, a kitchen cooks, a courier carries, and all three watch the same order while the food cools.

Video lesson · Chapter 1 of 5

Dinner is accepted before a courier is chosen

0:00 / 4:44

Theory

Read or listen first. About 2 min.

What it is

A food delivery service connects three sides. Customers order, restaurants cook, couriers carry. The service takes the order and the money, finds a courier, and tells everyone what is happening.

It is a shop and a moving map at once. The shop part is orders and payments, where each step must happen once. The map part is thousands of couriers, whose newest position replaces the one before.

Why it exists

Food has a short life. Ten minutes too early and the courier waits; ten minutes too late and the meal is cold. So the question is not only who is near, but who arrives when the pot is ready.

For a designer it joins three hard things: a process that crosses services and companies, a stream of small fast updates, and a matching problem that repeats every few seconds.

A short history

  1. 1955

    Harold Kuhn publishes the Hungarian method for the assignment problem, a way to match workers and jobs at the lowest total cost.

  2. 1994

    Pizza Hut takes an early online food order.

  3. February 2008

    Gustavo Niemeyer launches geohash.org: short strings that name areas of the map.

  4. December 2011

    RFC 6455 standardises WebSocket, a two-way connection that apps use for live updates.

  5. January 2013

    Stanford students launch PaloAltoDelivery.com, which becomes DoorDash.

Words to know

Courier
The person who carries the order. Some companies use another name.
Ping
One position report from a courier's phone.
Dispatch
Choosing which courier is offered which order, and when.
Offer
A short, exclusive chance to accept one order, here 30 seconds.
ETA
Estimated time of arrival. A range is more honest than one minute.
Batching
Two meanings: collecting orders for a few seconds before matching, and carrying two orders in one trip (stacking).
Market
One city area that runs on its own: its restaurants, couriers and orders.
Saga
A process of local steps, each with an undo step.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Order. Store, hold the money, ask the kitchen

Understand it

Soup by kite
ABCThree kitchens, one meadow

The slate must be right. The sky changes all the time.

Scope

What are we building?

Customers find restaurants that deliver to them, order and pay. Restaurants accept and cook. Couriers are offered orders, pick up and deliver. Everyone sees the same state and an estimate.

  • Order

    Exact, paid once, never lost.

  • Dispatch

    The right courier at the right minute, again and again.

  • Track

    52,000 pings a second at peak, and 209,000 people watching.

  • Predict

    Cooking, driving and parking, as a range.

Similar applications

ProductThe twist
Uber EatsShares couriers and maps with ride hailing
GrubhubGrew from restaurant ordering
PostmatesMany kinds of shops
InstacartGroceries: somebody picks items and replaces missing ones
DeliverooStrong in European cities
Swiggy, ZomatoIndia; Zomato began with restaurant discovery
GopuffOwn small stores, so stock is known

How it differs from its neighbours

Compared withSameDifferent
Ride hailingPositions, matching, offersA kitchen's timing in between; nobody waits at the kerb
A web shopBasket, payment, state machineMinutes instead of days; three parties live
Restaurant searchPlaces near youOnly those that deliver to your door now

The finished system

Where the design ends up

This is the last version. Design begins with one service and one database, and names the break that forces each box.

The whole system

Customers, couriers and restaurants reach a CDN and a gateway. The gateway sends browsing to discovery and a search index, orders to the order service with PostgreSQL and a payment service that calls a card provider outside, and pings to the location service, which overwrites the last position in Redis. Changes and pings flow through an event stream to dispatch, the live gateway and a ping history. Dispatch uses Redis and an ETA service.
Blue is reading, pink is writing, orange happens later. Dashed means asynchronous.

Where to read what

QuestionInterview tabConcept page that owns the idea
What must it do?ScopeScoping
How big is it?NumbersNumbers that decide
What is the interface?API and dataIdempotency, real-time APIs
How does it grow?DesignSaga, log-based queues
What is hard?Deep divesStream processing, pub-sub
Can I do it aloud?Mock45 minutes

Summary

The whole topic in one minute.

An order is a small state machine in a transactional store: stored before money is touched, charged with one key, and moved forward only from the expected state. A courier is claimed for it by one conditional write.

Positions are the opposite: the newest one lives in memory, older ones stream to a short history, and watching customers get pushed updates. Dispatch collects due orders every few seconds, asks who is near, estimates minutes, matches, and offers.

  • Store the order before you touch the money.
  • A courier and an order are joined by one conditional write.
  • Aim for arrival when the food is ready, not for the nearest person now.
  • The newest position of all couriers is 35 MB. Keep it in memory; stream the rest.
  • Cooking and the courier's trip to the kitchen overlap, so take the larger, not the sum.
  • A city is a natural boundary: run each market on its own.

Sources