SyntaxPilot
Courses/System Design/Lesson 33
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

System Design · Lesson 33 of 40

Design Swiggy/Zomato delivery

queued

These are the notes for System Design episode 33, our seventh full design problem. It's 8:30 pm and Priya orders a chicken biryani: one order, three apps (customer, restaurant, rider), and a rider who should reach the counter at exactly the minute the food is packed. Today you design food delivery: you'll write the requirements, watch a late message turn a delivered order back into preparing and fix it with an order state machine, find that sending the rider at once makes him wait 12.4 minutes at the counter and fix it by sending him at predicted prep time minus travel time (0.9 and 0.9 minutes of waiting), free riders in the dinner rush by batching two orders going the same way (6.9 km → 3.9 km, Rohan only 0.4 minutes later), and replace 800,000 useless polls a second with push over open connections (pub/sub). Every demo is plain Python and runs as-is; outputs are real (Python 3.13). Prep times, travel times, prediction errors, distances, the rider ping interval and customer counts are our own assumptions for the exercise; the only real-world facts come from the Zomato engineering blog listed in the sources at the end.

Food delivery means a platform that takes a customer's order, passes it to a restaurant to cook, and sends a delivery rider to carry it to the customer. Example: Swiggy, Zomato.

The problems

Problem What it teaches
Priya orders biryani at 8:30 pm Requirements: order, restaurant, rider, live map, hot food, correct status
Delivered, then preparing? Last write wins; states, transitions, the order state machine; conditional updates
Somebody always waits Dispatch timing: prep time, travel time, predicting prep time
Dinner rush, too few riders Batching two orders on one rider, with a delay limit
A million phones asking "where is he?" Polling vs push; WebSockets; pub/sub subscriptions
Put it all together Order service, prep-time model, dispatcher, location and tracking services, status events on a queue

Problem 0: Priya orders biryani, 8:30 pm (requirements)

Three apps work on one order:

  1. Customer app: Priya taps Order. Order 501 is created (chicken biryani, ₹280).
  2. Restaurant tablet: the restaurant accepts and starts cooking, about 18 minutes (our assumption).
  3. Rider app: a rider picks it up, and Priya watches him come closer on the live map.

Functional requirements (what the system must do):

  • The customer orders; the restaurant accepts and cooks.
  • A rider is assigned, picks up and delivers.
  • Everyone sees the right status and a live map.

Non-functional requirements (how well it must do it):

  • Hot food: the rider should be at the counter just as the food is packed.
  • Dinner rush: survive many orders when there aren't enough riders.
  • Correct status: the status is never wrong, and never goes backwards.

A few words first:

  • An order status is the one word that says where the order is right now. Example: placed, preparing, delivered.
  • A rider (Zomato calls them delivery partners) is the person on a scooter who carries the food. Example: Ravi, 2 km from the restaurant.
  • Prep time (food preparation time, FPT) is how many minutes the restaurant takes to cook and pack an order. Example: 18 minutes for Priya's biryani.
  • Travel time is how many minutes the rider needs to reach the restaurant. Example: 6 minutes.

Problem 1: the status goes backwards (state machine)

The obvious way: the order has a status, and every app just overwrites it.

order = {"id": 501, "status": "placed"}

def update(new_status):                   # every service just overwrites
    order["status"] = new_status
    print("status ->", new_status)

update("accepted")                        # restaurant accepts
update("preparing")
update("picked_up")                       # rider's app, on the scooter
update("delivered")                       # rider taps Delivered
update("preparing")                       # a late, retried message from
                                          # the restaurant tablet arrives
print("customer sees:", order["status"])

Output:

status -> accepted
status -> preparing
status -> picked_up
status -> delivered
status -> preparing
customer sees: preparing
  • The restaurant tablet lost its network for a moment, then retried its "preparing" message. A retry means sending the same message again because the first try seemed to fail. Example: the tablet didn't get an "OK" back, so it sends "preparing" again a minute later.
  • That late message arrived after "delivered". Priya is already eating, and the app says the restaurant is still cooking.
  • This is last write wins: whichever update arrives last becomes the status, even when it's wrong. Example: here the last write was the stale "preparing".

Messages in a distributed system arrive late and out of order. Anyone could set any status at any time, so the last one wins.

Real life: snakes and ladders, without snakes

A board game An order
Squares on the board: start, middle, finish States: placed, preparing, delivered... (where the order is)
Allowed moves only, no jumping back to start Transitions: preparing → picked_up only (rules for moving)
The finish square: the game is over Final state: delivered, nothing comes after
  • A state is one of a fixed list of situations something can be in. Example: preparing.
  • A transition is an allowed move from one state to another. Example: preparing → picked_up.
  • A final state (terminal state) is a state with no moves out of it. Example: delivered, cancelled.
  • A state machine is a list of states plus the allowed transitions between them; any other move is rejected. Example: the order lifecycle below.
NEXT = {                                  # the only moves allowed
    "placed":    {"accepted", "cancelled"},
    "accepted":  {"preparing", "cancelled"},
    "preparing": {"picked_up"},
    "picked_up": {"delivered"},
    "delivered": set(),                   # the end: nothing comes after
    "cancelled": set(),
}
order = {"id": 501, "status": "placed"}

def update(new_status):
    now = order["status"]
    if new_status not in NEXT[now]:
        print(f"rejected: {now} -> {new_status}")
        return
    order["status"] = new_status
    print("status ->", new_status)

for s in ["accepted", "preparing", "picked_up", "delivered", "preparing"]:
    update(s)
print("customer sees:", order["status"])

Output:

status -> accepted
status -> preparing
status -> picked_up
status -> delivered
rejected: delivered -> preparing
customer sees: delivered
  • NEXT lists, for each state, the only states it may move to. delivered and cancelled lead nowhere (set() is an empty set).
  • update checks the move first. Same messages, same late retry, but now: rejected: delivered -> preparing, and the customer sees delivered.
  • Notice cancelled is only allowed from placed and accepted: once cooking starts, this design doesn't allow a cancel (a business decision; see Practice 1).

In a real database, the check and the write must happen in one step, otherwise two services can both read "preparing" and both write. That's a conditional update: "set status = picked_up only if status is still preparing". Example in SQL: UPDATE orders SET status = 'picked_up' WHERE id = 501 AND status = 'preparing'; if 0 rows changed, the move was illegal or someone else got there first. It's the same idea as the atomic driver claim from episode 32 ("set the driver to offered, only if it is still free"), often called check-and-set (compare-and-set: change a value only if it still equals what you expect).

Problem 2: when to send the rider (dispatch timing)

Real life: somebody always waits

Rule What happens (our example: food needs 18 min, rider is 6 min away)
Send the rider at once He arrives at minute 6 and waits 12 minutes at the counter
Send him when the food is ready The biryani sits on the counter getting cold while he rides over (6 min)
Send him at 18 − 6 = minute 12 He arrives at minute 18, exactly as it's packed: nobody waits
  • Dispatch means choosing a rider for an order and telling him to go. Example: at minute 12 the app sends Ravi to the restaurant.
  • Rider wait time is how long the rider stands at the counter. Each minute there is a minute he isn't delivering someone else's order.
  • Food wait time is how long packed food sits on the counter before the rider arrives. Each minute there, the food gets colder.
  • We don't know the real prep time in advance, so we must predict it. A prediction model (here: a guess) estimates a number from past data. Example: "biryani at this restaurant at 8:30 pm on a Saturday: about 18 minutes". In our demo the guess can be up to 3 minutes wrong.
import random
random.seed(33)
orders = []                               # 1,000 dinner orders
for _ in range(1000):
    prep = random.randint(12, 25)         # real cooking minutes
    guess = prep + random.randint(-3, 3)  # our model's predicted prep time
    travel = random.randint(3, 9)         # rider's minutes to the restaurant
    orders.append((prep, guess, travel))

def simulate(name, send_at):
    rider_wait = food_wait = 0
    for prep, guess, travel in orders:
        arrive = send_at(guess, travel) + travel
        rider_wait += max(0, prep - arrive)   # rider stands at the counter
        food_wait += max(0, arrive - prep)    # food goes cold on the counter
    r, f = rider_wait / len(orders), food_wait / len(orders)
    print(f"{name:<15} rider waits {r:4.1f} min, food waits {f:4.1f} min")

simulate("at once", lambda guess, travel: 0)
simulate("when ready", lambda guess, travel: guess)
simulate("prep - travel", lambda guess, travel: max(0, guess - travel))

Output:

at once         rider waits 12.4 min, food waits  0.0 min
when ready      rider waits  0.0 min, food waits  6.0 min
prep - travel   rider waits  0.9 min, food waits  0.9 min
  • random.seed(33) makes the "random" numbers the same on every run, so you get exactly this output.
  • send_at(guess, travel) is the rule: the minute (after the order) when we send the rider. arrive is when he reaches the counter.
  • At once: the rider waits 12.4 minutes on average. When ready: the food waits 6.0 minutes. Prep − travel: 0.9 minutes each, even with a guess that's up to three minutes off.
  • max(0, guess - travel): if the rider is farther away than the cooking time, send him immediately.

Zomato has written about exactly this: they predict each order's food preparation time to communicate "the right time to reach a restaurant" to delivery partners. FPT is part of the delivery ETA (estimated time of arrival: the time the app promises the customer) together with the rider's travel time, and they also model the rider's wait time at the restaurant and the hand-shake time at the counter (the moment the restaurant hands the order over). Against their baseline, their new model improved 3-minute accuracy by 4% and cut 3-minute breaches by 9% (a breach meaning the prediction was off by more than 3 minutes).

Problem 3: the dinner rush (batching)

Real life: the school van

A school van Food delivery
One car per child: the road fills up One rider per order: not enough riders at 8:30 pm, runs out at rush hour
One van takes children on one route, drops them one by one Batching: two orders from the same restaurant, going the same way, one rider
The last child gets home a bit later, but only a few minutes Delay limit: batch only if the second customer waits 5 extra minutes or less (our assumption)
  • Batching means one rider carries several orders on one trip. Example: Priya's and Rohan's orders, same restaurant, homes in the same direction.
  • A delay limit is the most extra time we'll let the batch add for any customer. Example: +5 minutes.
import math
restaurant = (0, 0)
homes = {"Priya": (3.0, 1.0), "Rohan": (3.4, 1.6)}   # km, same direction
SPEED = 20                                         # km/h on a scooter

def km(*stops):
    return sum(math.dist(a, b) for a, b in zip(stops, stops[1:]))

one_each = km(restaurant, homes["Priya"]) + km(restaurant, homes["Rohan"])
batched = km(restaurant, homes["Priya"], homes["Rohan"])
alone = km(restaurant, homes["Rohan"]) / SPEED * 60
shared = batched / SPEED * 60
print(f"two riders: {one_each:.1f} km | one rider, both: {batched:.1f} km")
print(f"Rohan's ride: {alone:.1f} min alone, {shared:.1f} min batched")
print("batch them?", "yes" if shared - alone <= 5 else "no")

Output:

two riders: 6.9 km | one rider, both: 3.9 km
Rohan's ride: 11.3 min alone, 11.7 min batched
batch them? yes
  • km(*stops) adds up straight-line distances along a list of stops (math.dist is the straight-line distance between two points; real roads are longer).
  • Two riders, one each: 6.9 km. One rider, restaurant → Priya → Rohan: 3.9 km, about 3 km saved.
  • Rohan's food arrives in 11.7 minutes instead of 11.3: under half a minute later, well inside the 5-minute limit. Batch them? Yes.
  • One rider is freed for another order at the busiest time of the evening. A real dispatcher checks many pairs like this every few seconds and batches only when the delay limit holds.

Problem 4: the live map (polling vs push)

Real life: "Are we there yet?"

A family car trip Live tracking
Kids ask every minute: "are we there yet?" Mostly "not yet" Polling: the app asks the server every second
The driver announces: "we just crossed the toll". Only real news Push: the server sends an update when the rider moves
Keep the phone line open: one call, many updates Open connection: a WebSocket, subscribed to one order (pub/sub)
  • Polling means the app asks the server again and again, on a timer, whether anything changed. Example: "where is my rider?" every second.
  • Push means the server sends the update to the app as soon as something changes, without being asked.
  • A WebSocket is a connection between the app and the server that stays open, so either side can send messages at any time. Example: Priya's app opens one WebSocket and receives every new rider position on it.
  • Pub/sub (publish/subscribe, episode 16): subscribers say what they're interested in; publishers send messages to a topic, and the system delivers them to every subscriber of that topic. Example: Priya's phone subscribes to order 501; the rider's new location is published to order 501.
  • A rider ping is the rider's phone sending its current location to the server. Example: every 5 seconds (our assumption).
WATCHING = 1_000_000          # customers watching the live map
POLL_EVERY = 1                # app asks "any news?" every second
RIDER_PING = 5                # rider's phone sends a location every 5 s

polls = WATCHING // POLL_EVERY
useful = WATCHING // RIDER_PING   # answers with a new location
wasted = polls - useful
print(f"polling: {polls:,} requests a second, {wasted:,} say 'no change'")

subscribers = {}                          # order -> open connections
def subscribe(order, phone):
    subscribers.setdefault(order, []).append(phone)
def rider_moved(order, where):            # push only when something changed
    for phone in subscribers.get(order, []):
        print(f"  push to {phone}: rider at {where}")

subscribe(501, "Priya's phone")
rider_moved(501, (2.1, 0.7))
print(f"pushing: {useful:,} messages a second, none wasted")

Output:

polling: 1,000,000 requests a second, 800,000 say 'no change'
  push to Priya's phone: rider at (2.1, 0.7)
pushing: 200,000 messages a second, none wasted
  • A million customers poll every second, but each rider sends a new location only every 5 seconds: 1,000,000 requests a second, 800,000 of them say "no change".
  • subscribers keeps the open connections for each order. rider_moved pushes to exactly those phones.
  • Pushing: 200,000 messages a second, none wasted. Five times less traffic, and the map updates the moment the rider moves, not up to a second later.

The full design

 customer app ──┐                                   ┌── restaurant app (accept, cook)
 (order, map)   │                                   │
                ▼                                   ▼
          ┌───────────────────────────────────────────────┐
          │  ORDER SERVICE: owns the state machine         │──▶ message queue: status events
          │  placed→accepted→preparing→picked_up→delivered │      ├─▶ notifications
          └───────────────────────────────────────────────┘      ├─▶ payments
                │                     ▲                           └─▶ restaurant screen ...
                ▼                     │
   prep-time model ──▶ DISPATCHER (send at prep − travel, batch if +5 min or less)
                                      │
                                      ▼
   rider app ──pings──▶ LOCATION SERVICE (latest position only, episode 32)
                                      │
                                      ▼
                         TRACKING SERVICE ──WebSocket push──▶ subscribed customer apps
  1. The customer app places an order with the order service. The order service owns the state machine: every status change goes through it, as a conditional update, and illegal moves are rejected.
  2. The restaurant app accepts and marks "preparing" through the same service.
  3. A prep-time model predicts how many minutes this order will take to cook.
  4. The dispatcher picks a rider and sends him at predicted prep time − travel time, and batches orders from the same restaurant going the same way when the extra delay is within the limit.
  5. The rider app pings its location to the location service, which keeps only each rider's latest position (as in episode 32), and moves the order to picked_up and delivered.
  6. The tracking service pushes the rider's position over WebSockets to the apps subscribed to that order.
  7. Every status change is published as an event on a message queue, so notifications, payments, the restaurant's screen and analytics all hear about it, each at its own pace. A message queue is a list of messages that producers add to and consumers read from, independently. An event is a message saying "this happened". Example: {"order": 501, "status": "picked_up"}.

The whole chain

  1. Delivered, then preparing → state machine (reject illegal moves).
  2. Rider waits 12.4 min → dispatch at prep − travel (0.9 and 0.9 min).
  3. Too few riders → batching: 6.9 km → 3.9 km, +0.4 min for Rohan.
  4. 800,000 useless polls a second → push over WebSockets (pub/sub).
  5. Order service, prep-time model, dispatcher, location and tracking services, status events on a queue.

Common mistakes (and the real outputs)

Mistake What you see Why Fix
Every app overwrites the status customer sees: preparing (after delivered) Late messages win (last write wins) State machine: reject illegal moves
A typo in a state name: "picked up" (a space, not _) in the preparing set and the list of updates KeyError: 'picked up' (see below) State names must match exactly; NEXT has no key "picked up" Use constants or an enum for states
Send the rider the moment the order is placed rider waits 12.4 min Cooking takes longer than riding Dispatch at predicted prep − travel
Send the rider when the food is ready food waits 6.0 min He still has to ride over Dispatch at predicted prep − travel
Batch without a delay limit (Rohan's home the other way) Rohan's ride: 11.3 min alone, 30.2 min batched The batch detour makes one customer much later Batch only if the extra delay ≤ limit
Poll every second for the map polling: 1,000,000 requests a second, 800,000 say 'no change' Most answers are "no change" Push over open connections
Check and write in two separate steps Two services both read preparing and both write Another write sneaks in between Conditional update in one step

The typo, for real. In the MACHINE demo change "preparing": {"picked_up"} to "preparing": {"picked up"} and "picked_up" to "picked up" in the for list. The move into "picked up" is allowed, but the next update looks up NEXT["picked up"], which doesn't exist. Output (file path shortened):

status -> accepted
status -> preparing
status -> picked up
Traceback (most recent call last):
  File ".../main.py", line 20, in <module>
    update(s)
    ~~~~~~^^^
  File ".../main.py", line 13, in update
    if new_status not in NEXT[now]:
                         ~~~~^^^^^
KeyError: 'picked up'

A KeyError is Python's error when you look up a key that isn't in a dictionary. An enum (enumeration) is a fixed set of named constants, so a typo becomes an error the moment you write it, not later. Example: class Status(Enum): PICKED_UP = "picked_up", then Status.PICKED_UP everywhere.

Mini project: a tiny food delivery backend

An order service with a state machine, prep-time-aware dispatch, a simple batching rule, push subscriptions and a status event list, in 76 lines. Three orders from one restaurant (Priya and Rohan in the same direction, Anil the opposite way), two riders:

# Mini project: a tiny food delivery backend: an order service with a state
# machine, prep-time-aware dispatch, a simple batching rule, and push
# subscriptions for the live map. One process; times are in minutes.
import math

NEXT = {"placed": {"accepted", "cancelled"}, "accepted": {"preparing", "cancelled"},
        "preparing": {"picked_up"}, "picked_up": {"delivered"},
        "delivered": set(), "cancelled": set()}
SPEED = 20 / 60                           # km per minute (20 km/h scooter)
MAX_EXTRA = 5                             # batch only if +5 min or less
REST = (0, 0)                             # the restaurant

def km(*stops):
    return sum(math.dist(a, b) for a, b in zip(stops, stops[1:]))

class OrderService:
    def __init__(self):
        self.orders, self.subs, self.events = {}, {}, []
    def place(self, oid, name, home, guess, real):
        self.orders[oid] = dict(status="placed", name=name, home=home, guess=guess, real=real)
        self.subs[oid] = []
    def subscribe(self, oid, phone):      # an open connection, per order
        self.subs[oid].append(phone)
    def push(self, oid, msg):
        for phone in self.subs[oid]:
            print(f"   push to {phone}: {msg}")
    def move(self, oid, new):
        now = self.orders[oid]["status"]
        if new not in NEXT[now]:
            print(f"   rejected #{oid}: {now} -> {new}")
            return
        self.orders[oid]["status"] = new
        self.events.append((oid, new))    # an event for other services
        self.push(oid, f"#{oid} is {new}")

def make_trips(svc):                      # batching: pair orders going one way
    left, trips = list(svc.orders), []
    while left:
        a = left.pop(0)
        for b in left:
            ha, hb = svc.orders[a]["home"], svc.orders[b]["home"]
            extra = (km(REST, ha, hb) - km(REST, hb)) / SPEED
            if extra <= MAX_EXTRA:
                left.remove(b); trips.append([a, b]); break
        else:
            trips.append([a])
    return trips

svc = OrderService()
svc.place(501, "Priya", (3.0, 1.0), guess=18, real=19)
svc.place(502, "Rohan", (3.4, 1.6), guess=20, real=20)
svc.place(503, "Anil", (-2.5, -2.0), guess=15, real=13)
svc.subscribe(501, "Priya's phone")
for oid in svc.orders:
    svc.move(oid, "accepted"); svc.move(oid, "preparing")

riders = {"Ravi": (1.2, 1.6), "Sita": (0.0, -2.4)}
for trip, (rider, spot) in zip(make_trips(svc), riders.items()):
    travel = km(spot, REST) / SPEED
    guess = max(svc.orders[o]["guess"] for o in trip)
    real = max(svc.orders[o]["real"] for o in trip)
    send = max(0, guess - travel)         # dispatch at prep - travel
    arrive = send + travel
    print(f"{rider} takes {trip}: sent at {send:.1f}, arrives {arrive:.1f}, "
          f"food ready {real} -> rider waits {max(0, real - arrive):.1f}, "
          f"food waits {max(0, arrive - real):.1f}")
    for o in trip:
        svc.move(o, "picked_up")
    if 501 in trip:
        svc.push(501, "rider at (2.1, 0.7)")   # location, pushed on change
    for o in trip:
        svc.move(o, "delivered")

svc.move(501, "preparing")                # a late retry from the tablet
print("events on the queue:", len(svc.events))
print("final:", {o: d["status"] for o, d in svc.orders.items()})

Output:

   push to Priya's phone: #501 is accepted
   push to Priya's phone: #501 is preparing
Ravi takes [501, 502]: sent at 14.0, arrives 20.0, food ready 20 -> rider waits 0.0, food waits 0.0
   push to Priya's phone: #501 is picked_up
   push to Priya's phone: rider at (2.1, 0.7)
   push to Priya's phone: #501 is delivered
Sita takes [503]: sent at 7.8, arrives 15.0, food ready 13 -> rider waits 0.0, food waits 2.0
   rejected #501: delivered -> preparing
events on the queue: 12
final: {501: 'delivered', 502: 'delivered', 503: 'delivered'}
  • Priya's and Rohan's orders were batched on Ravi (the detour adds well under 5 minutes); Anil's home is the other way, so make_trips gave him his own trip. Ravi is 2 km away (6 minutes), so he was sent at minute 14.0 = 20 − 6 and arrived exactly as the batch was ready: 0.0 and 0.0 waiting.
  • Anil's prep was over-predicted (guess 15, real 13), so his food waited 2.0 minutes: dispatch is only as good as the prep-time prediction.
  • Only Priya subscribed, so only her phone got pushes (status changes and the rider's location). The late "preparing" retry was rejected, and every legal move (12 of them: 4 per order) became an event on the queue.

Try extending it: add a fourth order at (3.2, 0.6) and let make_trips build batches of up to three; then make Ravi 4 km away and see when he is sent.

Practice

  1. Homework: in the MACHINE demo, try 'cancelled' after 'preparing'. What happens? Should it be allowed?
  2. Homework: in the DISPATCH demo, make the guess up to 8 minutes off. Who waits now?
  3. Homework: in the BATCH demo, move Rohan's home the other way. Does it still batch?
  4. Homework (comment): who should pay when the food is late: the restaurant, the rider, or the platform?
  5. In the TRACK demo, set POLL_EVERY = 5. Is polling now as good as push?
  6. Quiz variant: predicted prep time 25 minutes, the rider is 9 minutes away, order placed at 9:10 pm. When do you send him?
  7. Why must the state check and the write happen in one step in a real database?
  8. Why does the dispatcher use max(0, guess - travel) and not just guess - travel?
Solutions and hints

1. Change the list to ["accepted", "preparing", "cancelled"]. Output:

status -> accepted
status -> preparing
rejected: preparing -> cancelled
customer sees: preparing

Our NEXT only allows cancelled from placed and accepted. Whether to allow it after preparing is a business decision: the food is already being cooked, so someone pays for it. If you allow it, add it to the set: "preparing": {"picked_up", "cancelled"}. With the list ["accepted", "preparing", "cancelled", "picked_up"]:

status -> accepted
status -> preparing
status -> cancelled
rejected: cancelled -> picked_up
customer sees: cancelled

The state machine now also stops a rider from picking up a cancelled order.

2. Change random.randint(-3, 3) to random.randint(-8, 8). Output:

at once         rider waits 12.3 min, food waits  0.0 min
when ready      rider waits  0.3 min, food waits  6.5 min
prep - travel   rider waits  2.0 min, food waits  2.1 min

Prep − travel still wins, but both waits more than double (0.9 → 2.0 and 2.1). The rule is only as good as the prediction, which is why a better prep-time model matters. ("At once" changes slightly too: the wider range changes the random numbers drawn after it, so the travel times differ a little.)

3. Change Rohan to (-3.4, -1.6). Output:

two riders: 6.9 km | one rider, both: 10.1 km
Rohan's ride: 11.3 min alone, 30.2 min batched
batch them? no

One rider would ride more (10.1 km vs 6.9) and Rohan would wait 19 extra minutes. The delay limit says no.

4. Open-ended. Points to discuss: who caused the delay (the restaurant took longer than predicted, the rider took a long route, the platform dispatched late or batched too aggressively); the platform made the promise (the ETA) to the customer, so it usually answers to the customer first; the events on the queue (with timestamps) tell you which step was slow.

5. Output:

polling: 200,000 requests a second, 0 say 'no change'
  push to Priya's phone: rider at (2.1, 0.7)
pushing: 200,000 messages a second, none wasted

On paper, yes, but only because our model assumes the rider moves exactly every 5 seconds. In reality the poll and the ping aren't in step, so the map can be up to 5 seconds stale, and when the rider is waiting at the restaurant or a red light every poll is still "no change". Push sends only when something actually changed. (The comment in the code still says "every second"; update it too.)

6. 25 − 9 = 16 minutes after 9:10, so 9:26 pm. He arrives at 9:35, as the food is packed.

7. If a service reads preparing, then writes picked_up in a second step, another service can write in between (for example cancelled), and the second write overwrites it. A conditional update (... WHERE status = 'preparing') does the check and the write together.

8. If the rider is farther away than the cooking time (say 20 minutes away for 15 minutes of cooking), guess - travel is negative: you can't send him in the past, so send him now.

Quick quiz

predicted prep time: 22 min
rider's travel time: 7 min
order placed at 8:30 pm

When should the rider be sent?

  • A. 8:30 pm
  • B. 8:37 pm
  • C. 8:45 pm
  • D. 8:52 pm
Answer

C. 8:45 pm. Prep minus travel: 22 − 7 = 15 minutes after 8:30, so 8:45 pm. He arrives at 8:52, just as the food is packed. (A makes him wait 15 minutes at the counter; D is when the food is ready, so it would wait 7 minutes.)

Interview questions: "Design Swiggy/Zomato"

1. Clarify requirements. Ordering, restaurant acceptance, rider assignment, live tracking, payments, ratings? Which are in scope? Then state: correct status that never goes backwards, hot food (rider arrives as food is packed), survive the dinner rush, live map.

2. Estimate (state your own assumptions clearly; these are ours, not real company figures). "Suppose 1 million customers watch the live map at dinner peak. Polling every second is 1 million requests a second; with riders pinging every 5 seconds, 800,000 of those say 'no change'. Push cuts it to about 200,000 messages a second." Add orders per second at peak, riders online, and location pings per second (riders ÷ ping interval).

3. Components.

  • Order service with the state machine; conditional updates; every change published as an event on a message queue.
  • Restaurant app / restaurant service: accept, reject, mark ready; menu and availability.
  • Prep-time model: predicts FPT per order (restaurant, dish, time of day, how busy the kitchen is).
  • Dispatcher: matches riders to orders (nearby, free) and sends them at predicted prep − travel; batches orders with a delay limit.
  • Location service: latest rider position, updated by pings (episode 32's grid for finding "riders near this restaurant").
  • Tracking service: WebSockets, subscriptions per order (pub/sub), pushes positions and status.
  • Notification, payment, ETA services consume status events.

4. Data. Orders table (id, customer, restaurant, status, timestamps per transition); riders' latest locations in memory (fast changing, only the latest matters); events kept on the queue for other services.

Common follow-ups:

  • What if the restaurant tablet's message arrives twice or late? (State machine + conditional update; repeated messages are rejected or ignored, so retries are safe.)
  • What if the prep-time prediction is wrong? (Re-predict as the restaurant updates status; re-plan dispatch; track wait time to improve the model.)
  • How do you choose which orders to batch? (Same restaurant or nearby, same direction, ready at similar times, extra delay under the limit; check candidate pairs every few seconds.)
  • How do you keep millions of WebSockets? (Many tracking servers, each holding connections; a pub/sub layer routes "order 501 moved" to the server holding Priya's connection; apps reconnect and fall back to polling if a connection drops.)
  • What happens when no rider is free? (Queue the order, widen the search radius, batch more, show a longer ETA honestly.)

Glossary

Term Meaning
Food delivery platform Takes the order, passes it to a restaurant, sends a rider to deliver it
Functional / non-functional requirements What the system must do / how well it must do it
Order status The one word saying where the order is, e.g. preparing
Rider / delivery partner The person who carries the food
Prep time (FPT) Food preparation time: minutes to cook and pack an order
Travel time Minutes for the rider to reach the restaurant
Retry Sending the same message again because the first try seemed to fail
Last write wins Whichever update arrives last becomes the value, even if it's stale
State One of a fixed list of situations, e.g. picked_up
Transition An allowed move between states, e.g. preparing → picked_up
Final (terminal) state A state with no moves out, e.g. delivered
State machine States plus allowed transitions; other moves are rejected
Conditional update Change a row only if it still has the expected value, in one step
Check-and-set (compare-and-set) Change a value only if it still equals what you expect (the atomic claim in episode 32)
Dispatch Choosing a rider for an order and telling him to go
Rider wait time Minutes the rider stands at the counter
Food wait time Minutes packed food sits on the counter
Prediction model Estimates a number (here prep time) from past data
ETA Estimated time of arrival promised to the customer
Hand-shake time The handover of the order at the restaurant counter
3-minute accuracy / breach Prediction within 3 minutes of reality / off by more than 3 minutes
Batching One rider carries several orders on one trip
Delay limit The most extra time a batch may add for any customer (ours: 5 min)
Polling The app asks the server again and again on a timer
Push The server sends the update as soon as something changes
WebSocket A connection that stays open so either side can send at any time
Pub/sub Publishers send to a topic; every subscriber to it receives (episode 16)
Rider ping The rider's phone sending its location to the server
Location service Keeps each rider's latest position
Tracking service Pushes rider positions and status to subscribed apps
Message queue Producers add messages, consumers read them, each at its own pace
Event A message saying "this happened", e.g. order 501 is picked_up
KeyError Python's error for a missing dictionary key
Enum A fixed set of named constants, so typos fail immediately

Summary

  • Food delivery has three apps on one order: customer, restaurant, rider. Requirements: correct status, hot food, survive the dinner rush, live map.
  • When every app overwrites the status, a late retry turns delivered back into preparing. An order state machine allows only legal transitions (rejected: delivered -> preparing); in a database, use a conditional update.
  • Sending the rider at once makes him wait 12.4 min; sending him when food is ready makes the food wait 6.0 min; sending him at predicted prep − travel gives 0.9 and 0.9 even with a 3-minute prediction error. Zomato predicts food preparation time to tell riders "the right time to reach a restaurant".
  • In the dinner rush, batch orders going the same way: 6.9 km → 3.9 km, Rohan only 0.4 min later; never batch past the delay limit.
  • For the live map, push over WebSockets with per-order subscriptions (pub/sub) instead of polling: 800,000 wasted requests a second → 0.
  • The design: order service (state machine) + prep-time model + dispatcher + location service + tracking service, with every status change as an event on a queue.

Next episode: Priya pays for her biryani with UPI. The money leaves one bank and lands in another in seconds, and it must never be taken twice. Design UPI payments.

Sources: Zomato engineering blog, "Predicting your order's Food Preparation Time" (fetched 4 Nov 2026): Zomato predicts food preparation time (FPT) to communicate "the right time to reach a restaurant" to delivery partners; FPT is part of the ETA together with the rider's travel time; they also model the rider's "wait time" at the restaurant and the "hand-shake time" at the counter; against the baseline, the new model improved 3-minute accuracy by 4% and cut 3-minute breaches by 9%. Prep times, travel times, prediction error, distances, rider ping interval and customer counts are our own stated assumptions; state machines, conditional updates, WebSockets, pub/sub and polling are general textbook knowledge.