Chapter 01 / 24 · System Design

The brief, and one request end to end

Who uses Dabba, how much, and at what peak; then where the time goes when a customer opens the app at 8 pm, and how an optional service can take the home screen down.

System design is the work of deciding where a request spends its time, where its data lives, and what happens when one of those places misbehaves. That work needs a product with users and numbers, because every choice in this course is argued from them. This chapter sets up the product, then follows its most common request from a customer’s thumb to the database and back.

The product is Dabba, a food-ordering and delivery app that operates across Indian cities. A customer in Pune opens it at eight in the evening and sees the restaurants that deliver to her flat. She opens two or three menus, places an order and pays. A restaurant accepts the order on a tablet next to the kitchen pass, a rider picks it up, and she watches the rider’s dot move across the map until the doorbell rings. We will design all of that over the course. Here we write the brief and trace the first tap.

Write the brief before drawing a box

A brief turns “build a food delivery app” into something we can reason about. It names who uses the system, what they do, how often, and which assumptions we are making on their behalf. Without it, every design choice is defensible and none can be checked.

Dabba has four groups of users. Customers use a phone app, mostly on Android, many on inexpensive handsets and many on an app version that is months old because they never update. Restaurants use a partner app on a tablet in the kitchen. Riders run a rider app for a whole shift with GPS switched on. Dabba’s own support and operations staff use internal tools to look up orders, issue refunds and move riders between zones.

The core operations are: browse restaurants that deliver to an address; read a menu; place and pay for an order; accept or reject it and mark it ready; assign a rider, who picks up and delivers while sharing a live location; notify each party at each step; and let restaurants upload photos of their dishes. We leave out grocery, table bookings, advertising and cash on delivery. Each would add real work (cash on delivery, for example, brings its own reconciliation problem), and none is needed to teach what follows.

Now the numbers. Each one is an assumption, and the right-hand column is what it implies.

QuantityAssumptionWhat follows
Customers2 million daily active, about 2 sessions each4 million app sessions a day
Orders1 session in 4 ends in an order1 million orders a day
API callsabout 25 per session (home 8, four menus at 3 each, cart and checkout 5)100 million calls a day
Menu views4 per session16 million a day, 16 for every order
Restaurants50,000 active, about 80 menu items each4 million menu items
Dish photosabout 3 million, each in 4 sizes, ~60 KB at the size most screens useabout 100 image loads per session, 400 million a day
Riders50,000 online at the dinner peak, location sent every 5 s10,000 location updates a second
Order recordabout 2 KB with items, address and price linesabout 2 GB a day, about 730 GB a year before indexes
Notificationsabout 6 per order across customer, restaurant and riderabout 6 million a day

A few assumptions sit outside the table. Dabba runs in one country with one time zone, so every city eats dinner at the same clock time; there is no geography to spread the peak. The backend runs in one cloud region in Mumbai with three availability zones (a second region comes much later, in the multi-region chapter). Payments go through an external payment provider that handles UPI, cards and wallets, and a UPI payment can be confirmed seconds or minutes after the customer approves it. A typical 4G round trip from a phone to Mumbai is around 60 ms, and often worse in a crowded area.

Finally, the targets. The home screen and menu should answer within 300 ms at the 99th percentile, measured from when our edge receives the request. Placing an order should answer within 1.5 s at the 99th percentile, not counting the time the customer spends in their UPI app. And 99.95% of order placement attempts between 11:00 and 23:00 should succeed. These numbers are planning estimates. Their job is to make choices arguable: if someone believes the photo count is ten times higher, we can see exactly which decisions change.

Predict first

Dabba takes a million orders a day. At 8:30 pm, which part of the system receives the most writes per second, and roughly how many?

Show the worked answer

It is not orders. The rider app reports a location every 5 seconds from 50,000 phones, which is 10,000 writes a second. Orders, as the next section works out, peak at around 150 a second even with generous headroom. Order status changes add roughly six writes per order, still under a thousand a second.

The entity at the centre of the business is not the heaviest load. The two streams also differ in what a lost write costs. Losing one rider location barely matters, because a newer one arrives five seconds later. Losing one paid order is a refund, a support call and a customer who may not return. They will end up in different stores, for reasons the storage chapter makes precise.

Turn daily totals into the rate the system must survive

A million orders a day averages 1,000,000 / 86,400 ≈ 11.6 orders a second. That number is nearly useless, because Dabba is quiet at 4 am and busy at 8:30 pm. Systems fall over at their peak, so we need the peak.

Assume the dinner hour, 8 to 9 pm, carries 15% of the day’s orders. That is 150,000 orders in 3,600 seconds, about 42 a second. Inside that hour the rate is not flat either. Rain starts in Mumbai and nobody wants to go out. A cricket final reaches the innings break. Marketing sends a push notification to two million phones at 8:15. Any of these can double or triple the rate for a few minutes. So the order path should handle about 120 orders a second, and we will design and load-test it at 150.

The same arithmetic gives the other design targets. API calls: 100 million a day, 15% in the peak hour, is 15 million in 3,600 seconds, about 4,200 a second; allowing about 2.4 times for spikes gives a target of 10,000 requests a second. Menu views: 16 million a day gives about 670 a second in the peak hour, and we design for 2,000.

The numbers are as useful for what they rule out. 150 orders a second is a small number for a single well-provisioned Postgres primary, which can sustain thousands of short transactional writes a second. So we will not split orders across machines on day one, and the hard part of ordering will be correctness, especially around an external payment provider, rather than raw throughput. On the other hand, 400 million image loads a day at about 60 KB each is 24 TB of data a day. Spread evenly that is 24 × 10¹² × 8 / 86,400 ≈ 2.2 gigabits a second, and the peak is several times higher. That is a CDN problem from the first day, and the CDN chapter treats it as one.

Follow one tap: opening Dabba at 8 pm

The customer in Pune taps the icon. Before Dabba’s servers do any work at all, the phone has to reach them. If the app has not talked to api.dabba.in recently, it first needs the address: a DNS lookup, usually answered by the mobile operator’s resolver, costing one round trip if the resolver has the answer cached and more if it has to ask upstream. Then a TCP handshake, one round trip. Then a TLS 1.3 handshake, one more (TLS 1.2 needed two). Only then does the request leave, and the response comes back one round trip plus the server’s work later.

cold open · 60 ms round trip · 150 ms of server workDNS lookupTCP handshakeTLS 1.3 handshakeGET /homeserver390 mswarm · connection reusedGET /homeserver210 ms0 ms100 ms200 ms300 ms400 mscold, the handshakes cost more time than the server does
Fig 1 · Opening the app at 60 ms per round trip. Cold, three round trips pass before the request is sent

With a 60 ms round trip and 150 ms of server work, the cold open takes about 390 ms to the first byte of the response, and the warm one 210 ms. On a cold connection the handshakes cost more than everything our servers do. There is also a cost the figure leaves out: a phone that has been idle puts its cellular radio into a low-power state, and the first packet afterwards can wait tens of milliseconds more while the radio reconnects.

This changes where effort pays. Shaving 20 ms off server time is worth less than keeping one connection open and reusing it for every call in the session, which HTTP/2 does by carrying many requests over a single connection. Terminating TLS close to the customer, which a CDN does, turns three long round trips into three short ones. HTTP/3 (over QUIC) folds the transport and TLS handshakes into one round trip and can resume a known server with none, which matters on lossy mobile networks. And asking for the home screen in one request instead of eight removes round trips that no amount of server tuning can remove. The API chapter returns to that last point.

Once the request arrives, it passes a short chain of boxes in Mumbai. A load balancer picks one of many identical servers. An API gateway checks the customer’s login token and routes the request to the home service. The home service has to answer one question: which restaurants deliver to this address right now, in what order, and with what delivery time and offers. To answer it, it asks several other services.

clientDNS~1 RTT, cachedLB / CDNterminate TLSapp serveryour codecache~1 msdatabase~10 msthe p99 budget is spent here, one hop at a timea cache hit ends the story at ~1 ms · a miss pays the database and warms the next hit
Fig 2 · The chain a request crosses. Each hop has a cost, and a cache hit can end the trip early

Where the 300 ms goes on the server

A latency target only helps if we split it into a budget per step, because then each new hop has to justify its share. Here is the home request on a normal evening.

StepTypical timeWhat it depends on
Edge to load balancer in Mumbai5–20 mshow far the edge location is from Mumbai
Gateway: verify token, route2–5 msa signature check, CPU only, no database
Restaurants serving this delivery zone~5 msa cached lookup keyed by zone
Availability, delivery estimate, offers, recommendations~60 mscalled in parallel, so the slowest of the four
Rank and assemble the list10–20 msnumber of candidate restaurants
Serialise and compress~5 msresponse size

That adds up to roughly 90 to 115 ms on a typical request, comfortably under 300. The remaining room is not spare. It is there for the tail: the slow garbage collection pause, the cache miss, the dependency that takes a second today. Two decisions in the table already protect it. The four calls in the middle row run in parallel; in series they would take about 240 ms and leave no room at all. And the gateway checks the token’s signature locally instead of calling an authentication service for every request.

Predict first

The home service calls four services in parallel and waits for all of them. Each answers in 20 ms 99% of the time and takes 250 ms the other 1%. What share of home requests take at least 250 ms?

Show the worked answer

A home request is fast only if all four calls are fast. The chance of that is 0.99 × 0.99 × 0.99 × 0.99 ≈ 0.961, so about 3.9% of home requests wait 250 ms or more. Each dependency is slow for 1 in 100 calls, yet the page is slow for nearly 4 in 100.

With ten parallel dependencies the share is 1 − 0.99¹⁰ ≈ 9.6%. This is why fan-out is expensive at the tail even when it is cheap on average. The usual remedies are to make fewer dependencies required, to give each call a timeout with a sensible default, and, for idempotent reads, to send a second copy of a slow request to another replica after a short wait (Dean and Barroso’s paper “The Tail at Scale” describes this “hedged request” and its cost: a few percent of extra load).

The evening the delivery-estimate service slowed down

Here is the failure that the budget is meant to prevent, followed step by step. At 8:20 pm a slow query on the delivery-estimate service’s database pushes its response time from 20 ms to about 2 seconds. The estimate is a nice-to-have: customers like seeing “30 min”, but they can order without it.

The home service calls the estimate service with its HTTP client’s default timeout, which is 30 seconds. Home requests arrive at about 170 a second (600,000 peak-hour sessions over 3,600 seconds). Little’s law says the number of requests in progress is the arrival rate times the time each one spends in the system. Normally that is 170 × 0.1 s ≈ 17. Now each request waits 2 seconds for its estimate, so it is 170 × 2 ≈ 340.

The home service runs on ten instances, each allowing 32 requests at a time: 320 slots. At 340 in progress, the slots are full. New requests queue behind them. The queue makes them older, so they hold slots longer, so the queue grows. At 8 seconds the gateway gives up and returns an error. Customers see a spinner, pull down to refresh, and send another request while the first still holds its slot. Within a couple of minutes the home screen is failing for almost everyone. Nothing required is broken. An optional number on each restaurant card took the page down.

The fix has three parts. First, a timeout that matches the budget: the estimate call gets 100 ms, and if it misses, the card shows a zone-level average (“30–40 min”) that is recomputed every few minutes and cached. Second, a separate limit per dependency, sometimes called a bulkhead: each instance allows at most, say, 20 estimate calls in flight, and a request that would be the 21st skips the call and uses the fallback immediately. That stops one slow dependency from consuming every slot. Third, a written decision about which dependencies are required.

DependencyRequired?If it is slow or down
Restaurants serving the zoneyesserve the last cached list; fail only if there is none
Availability (open, busy, closed)yes, with a fallbackuse state up to 60 s old; mark cards “may be busy”
Delivery estimatenoshow the zone average
Offersnoomit them; checkout recomputes offers anyway
Recommendationsnoorder by popularity in the zone

The fix has costs. Some customers see a coarse estimate on a bad evening. Every fallback is a code path that runs rarely, and a path that runs rarely tends to be broken when it finally runs, so the fallbacks need tests and occasional deliberate exercise in production. The table also has to be kept current as the product adds features to the home screen.

The table also changes availability arithmetic. If a request needs five services and each is available 99.9% of the time, the request can be available at most 0.999⁵ ≈ 99.5% of the time, which is about 3.6 hours of failure in a 30-day month. With only two required, it is 0.999² ≈ 99.8%. Every required dependency costs nines, so making a dependency optional is often the cheapest reliability work available. The failure chapter later in the course builds on this.

Count availability in orders, not minutes

“99.95% uptime” sounds precise, but minutes are the wrong unit for Dabba. A ten-minute outage at 8:30 pm on a Friday hits about 600 seconds × 42 orders a second ≈ 25,000 order attempts. The same ten minutes at 4 am hits a few hundred. A minute-based measure rates the two outages as equal. The business does not.

So we measure availability as the share of requests, or for the order path the share of order attempts, that succeed. At 99.95% and a million orders a day, the allowance is about 500 failed order attempts a day. That allowance is useful in practice: it tells the team how much risk a deploy at 7:45 pm is allowed to carry, and it makes a peak-hour incident visibly more expensive than a quiet-hour one.

When this much planning is more than the problem needs

If Dabba were a pilot in one neighbourhood taking 200 orders a day, the brief would still be worth writing, but its numbers would say one server and one database, and the right move would be to build and learn. Designing for 150 orders a second on day one means paying to run capacity nobody uses, and building fallbacks for services that do not exist yet.

The brief is also a hypothesis. We should revisit it when measured traffic differs from an assumption by more than about a factor of two, when the peak hour’s share changes (a lunch business behaves differently from a dinner one), or when a new product such as grocery delivery arrives with a different shape. Keeping the brief as a living page, with measured numbers replacing assumptions as they arrive, is cheap and keeps later arguments honest.

We now know what Dabba must do and how often. The next chapter, Designing the API, writes the contract for the most important of those operations, placing an order. It also faces the case this chapter kept stepping around: a request that succeeds on the server while the customer’s screen says it failed.