The edge: CDNs and static delivery
Dish photos and public menus served from near the customer, with URLs that never need purging and headers that never show one customer another’s orders.
A customer in Guwahati opens a menu. The menu document is about 40 KB, or around 8 KB compressed, and the screen shows 25 dish photos at about 60 KB each, so the screen needs roughly 1.5 MB. Dabba’s servers are in Mumbai, about 2,000 km away in a straight line and further along the fibre routes. The menu cache from the last chapter made the server’s part fast. The distance and the bytes remain.
A content delivery network, or CDN, keeps copies of responses in many locations close to customers, so most requests are answered nearby and never reach Mumbai. This chapter works out what that saves Dabba, how to name photos so they never need to be purged, which parts of the menu may be cached at the edge, and how one wrong rule can show a customer someone else’s orders.
What the edge changes in the arithmetic
A CDN runs servers in many cities, often called points of presence. The customer’s request is steered to a nearby one, either by DNS answering with a nearby address or by anycast, where every location announces the same address and internet routing delivers packets to the closest. If the edge has a fresh copy of the response, it answers. If not, it fetches from Dabba’s servers in Mumbai, which the CDN calls the origin, keeps a copy if the headers allow, and answers.
Suppose the phone in Guwahati has a 35 ms round trip to a CDN location in Kolkata and a 90 ms round trip to Mumbai; both include the mobile network’s own delay. The first chapter showed that a cold connection costs three round trips before a request leaves (DNS, TCP, TLS). To Mumbai that is 270 ms; to the edge it is 105 ms. The saving of about 165 ms applies even to requests the edge cannot cache, such as placing an order, because the handshakes end at the edge and the edge forwards the request to Mumbai over a connection it already has open.
For photos the saving is in bytes as well as time. The brief estimated 400 million image loads a day at about 60 KB, or 24 TB a day. If the edge answers 98% of them, Mumbai sends about 480 GB a day instead, and the busiest hour’s image traffic never touches Dabba’s servers. That is the main reason a food app with photos on every screen needs a CDN from its first large city.
How the customer is sent to a nearby edge, and when that goes wrong
The saving above assumes the phone in Guwahati actually reaches a nearby edge. With DNS-based steering, the CDN’s DNS server chooses which edge address to return, and it decides based on where the DNS question came from. But the question does not come from the phone. It comes from the resolver the phone uses, usually the mobile operator’s, and an operator may run its resolvers in a few large cities. If the resolver serving that customer sits in Delhi, the CDN sees a question from Delhi and returns a Delhi edge, and the customer’s round trip to “nearby” is longer than it should be.
Two mechanisms reduce this. Some resolvers pass a shortened form of the client’s address along with the question (an extension called EDNS Client Subnet), so the CDN can answer for the client’s network rather than the resolver’s. And with anycast, where every edge announces the same address, the choice is made by internet routing from the phone’s own network, without depending on the resolver at all. Routing picks the path the networks prefer, which is usually but not always the nearest, so CDNs tune their announcements city by city.
For Dabba the practical consequence is to measure from the customer’s side rather than trusting a map of edge locations. The app records, for a sample of requests, how long the connection setup and first response took and which edge answered (most CDNs add a response header naming it). Grouped by city and mobile operator, those numbers show where customers are being sent somewhere distant. A city where a large operator’s customers consistently land on an edge 1,500 km away is a conversation to have with the CDN provider, and it would never show up in server-side metrics, because from Mumbai every request looks the same.
A restaurant replaces a blurry biryani photo. The app loads it from /photos/item_9001.jpg, served with Cache-Control: max-age=86400 (one day). How long until every customer sees the new photo? Does purging the URL from the CDN fix it?
Show the worked answer
Up to a day. Each CDN location that has the old photo may keep serving it until its copy is a day old. A purge removes those copies within seconds or minutes, but it cannot reach the phones: each app and browser that downloaded the old photo keeps it in its own cache for up to a day as well, and does not ask again.
Shortening the lifetime to make updates faster means every phone re-downloads or re-checks every photo more often, which is exactly the traffic the CDN was meant to remove. The way out is to stop reusing the URL. If the new photo gets a new URL, the customer sees it as soon as her app receives a menu that points to it, and the old URL can stay cached forever because nothing will ask for it.
Give every photo a URL that never changes meaning
When a restaurant uploads a dish photo, Dabba stores the original and produces a few variants: 200 pixels wide for list thumbnails, 480 for menu cards, 1080 for the full view, in WebP for apps that support it. Each variant’s URL contains a hash of the image content:
https://img.dabba.in/p/3f9a1c07e2/480.webp
Cache-Control: public, max-age=31536000, immutableBecause the URL is derived from the content, the bytes behind a URL never change. That makes the longest caching safe: a year at the edge and a year on the phone, and immutable tells browsers not to even check for a newer copy when the user reloads. When the restaurant replaces the photo, the new image gets a new hash and so a new URL, and the menu document is updated to point at it. The reads chapter already made that menu change propagate within a minute through its version number. No purge is involved.
There is a quieter benefit. During the minute when some customers have the old menu and some the new, both URLs work. A customer with the old menu sees the old photo rather than a broken image. Designs that overwrite a file in place have a window where the old menu points at a URL whose bytes have changed or vanished.
The costs are storage and housekeeping. Old variants stay in object storage after nothing references them, so a cleanup job deletes variants that no menu has referenced for 30 days. And the rule must be absolute: a URL is never reused for different bytes, because a cached copy of the old bytes may survive for a year somewhere.
Image format needs one more decision. Browsers advertise the formats they accept in the Accept header, and a server could pick WebP or AVIF from it. But then the response depends on a request header, the CDN must store a separate copy per variation of that header (Vary: Accept), and real Accept headers vary in many small ways, which splits the cache into many partly-filled pieces. Dabba’s app knows which formats it supports, so it puts the format in the URL it requests (480.webp), and the website does the same with the HTML <picture> element. One URL, one set of bytes, one cache entry.
Resizing turned out to matter as much as caching. A restaurant’s original upload is often a 2 MB phone photo. Sending that to a menu card 480 pixels wide on an inexpensive phone wastes the customer’s data and seconds of loading, wherever it is served from.
Can the menu itself live at the edge?
A response can be stored at the edge and served to others only if it is the same for everyone who asks for that URL. The public part of a menu (dishes, descriptions, prices, photo links) is the same for every customer, which is why the reads chapter kept personal offers out of it. So it can be cached at the edge.
Dabba can borrow the photo trick here as well. The restaurant list for a zone already carries each restaurant’s menu_version, so the app requests /v1/restaurants/48213/menu?v=17. The bytes behind that URL never change, so it can be cached for a long time everywhere. When the restaurant edits its menu, the next restaurant list carries version 18 and the app asks for a new URL. Freshness of the menu is now decided by freshness of the restaurant list, which is cached for 30 seconds.
Availability, the sold-out markers and open or closed state, is also the same for everyone but changes during service, so it gets a short shared lifetime: the edge may keep it for 15 seconds, and phones must ask again each time. Personal offers and anything that depends on who the customer is are marked private and never stored in a shared cache.
One detail ruins edge caching quietly: the cache key. The edge stores responses by URL, so anything that makes URLs unique makes every request a miss. If the app adds the customer’s exact latitude and longitude to the restaurant list URL, no two customers share an entry and the hit rate is close to zero. Dabba sends a delivery zone ID instead, of which there are a few thousand. Tracking parameters such as utm_source, added by marketing links, are stripped from the cache key at the edge for the same reason.
The night one customer saw another’s orders
Here is the failure that makes CDN configuration a security matter. It is reconstructed for Dabba, but incidents of this shape have happened to large companies; Steam’s store showed some users other users’ account pages in December 2015 after a caching configuration change made under the pressure of an attack.
On a Saturday evening, Dabba’s API receives a flood of requests from a botnet. To relieve the origin, an engineer adds a CDN rule: cache every response under /v1/ for 60 seconds. Load on Mumbai drops immediately. A few minutes later, support receives the first call: a customer opened her order history and saw someone else’s orders, with their name, address and phone number.
The order history endpoint is GET /v1/orders. The URL is the same for every customer; the server knows whose orders to return from the login token in the Authorization header. The new rule told the edge to cache by URL. The first customer to request /v1/orders through a given edge location after the rule went live had her response stored, and every customer who asked that location for the same URL in the next 60 seconds received it. The same happened to profiles, saved addresses and the active order screen.
The immediate fix is to remove the rule and purge everything cached under the path. The lasting fix has three parts. First, the origin marks every response that depends on the caller as Cache-Control: private, no-store, set by shared middleware for every authenticated route, so no individual endpoint can forget. Second, the CDN honours origin headers and only caches an explicit list of path prefixes (images, versioned menus, availability, zone lists); a new rule cannot override private. Third, a test in the build pipeline calls every route with a test account and fails if any authenticated response comes back cacheable, or if any response that sets a cookie is cacheable.
The fix has a cost the team should say out loud: during the next attack, the edge cannot absorb load on personal endpoints by caching them. That load has to be handled by rate limits and by blocking abusive traffic at the edge, which the rate limiting and security chapters cover. Caching personal data was never a safe tool for that job.
Revalidation, stale copies and the origin shield
When a cached copy expires, the edge does not have to download the whole response again. If the origin sent an ETag (a version tag for the response), the edge asks “I have this tag; is it still current?” and the origin answers 304 Not Modified with no body when it is. That saves bandwidth but not the round trip to Mumbai. stale-while-revalidate removes the wait: the edge may serve the expired copy for a stated time while it checks with the origin in the background. stale-if-error goes further: if the origin is failing, the edge may keep serving the expired copy for a stated time.
That last directive changes what an origin outage means for Dabba. With stale-if-error=600 on zone lists and availability, a ten-minute failure in Mumbai still lets customers open the app, browse restaurants and read menus with slightly old availability. They cannot place orders, because that path is never cached. Partial service is not full service, but browsing customers who can still see menus tend to come back and order when things recover.
Distance between the edge and the origin creates one more problem. Dabba’s CDN has locations in perhaps thirty cities. When a popular menu version or a new zone list appears, each location misses separately and fetches from Mumbai, so one change causes thirty origin requests. Most CDNs offer a middle tier, usually called an origin shield or tiered cache: edge locations fetch from one designated regional cache, and only that cache fetches from the origin. Thirty misses become one. The same tier helps the long tail: a small restaurant in a small town is rarely requested at any single edge location, so its photos are often evicted there, but the regional tier still has them and the origin is spared.
| Content | Origin sends | How a change reaches customers |
|---|---|---|
| Dish photo variants | public, max-age=31536000, immutable | new content, new URL, referenced by a new menu version |
| Website scripts and styles | public, max-age=31536000, immutable | fingerprinted file names in new HTML |
| Menu at a given version | public, max-age=31536000, immutable | the zone list points at the next version |
| Restaurant list for a zone | public, max-age=0, s-maxage=30, stale-while-revalidate=60, stale-if-error=600 | edge refreshes every 30 s |
| Availability and open state | public, max-age=0, s-maxage=15, stale-if-error=600 | edge refreshes every 15 s |
| Orders, profile, offers, addresses | private, no-store | never stored outside the device |
| Checkout and payment calls | no-store; no cache rule matches the path | always reach the origin |
max-age applies to every cache, including the phone, while s-maxage applies only to shared caches such as the CDN. Setting max-age=0 with a short s-maxage lets the edge absorb load while making sure phones always ask for the current zone list, from the nearby edge.
Where the edge does not help
Live data that changes every few seconds per order, such as a rider’s position, gains nothing from caching; at best the edge carries the connection, which the realtime chapter discusses. The same applies to payment callbacks from the provider, which must reach Dabba exactly as sent. And if nearly all customers were in Mumbai, a short distance from the origin, the edge would save little time on uncacheable requests, though photos would still justify it on bandwidth alone.
We should revisit the setup when the CDN bill outgrows what it saves in origin bandwidth and servers, when a second region arrives and the origin is no longer one place, or when the hit rate on photos drops, which usually means a new client is requesting sizes or formats outside the fixed set and creating near-duplicate entries.
Photos now come from nearby, and the menu’s bytes rarely leave Mumbai. Behind the caches, though, every piece of data still needs a permanent home. The next chapter, Where the data actually lives, chooses stores for orders, menus, rider locations and photos, and opens the storage engines to see why each choice fits.