DEV Community

Cover image for System design caching: five layers that can keep an old price
System Design Simple
System Design Simple

Posted on Originally published at systemdesignsimple.com

System design caching: five layers that can keep an old price

A read can be answered by five caches before it ever reaches the database: the browser, a CDN edge, the web server, the application, and a database-side cache. In a seeded 1,000-read demo, only 215 reads reached the database, and after one price change the CDN still held an unexpired old-price entry 114.2 seconds later.

The five layers

Follow one product page read from a phone to the database. Each stop can keep a copy and answer the next request itself: the browser for one user, the CDN edge for everyone near it, the web server cache for everyone behind it, the app cache in one instance, and the database-side cache for every instance. The last two are code you own, and the app deletes them on write.

Who answered the 1,000 reads?

The demo runs 1,000 reads across 200 items with Zipf popularity, 40 clients, 3 CDN edges, 4 web servers, 4 app instances, and one shared database-side cache in front of SQLite.

The CDN edge answered 542 reads, the browser 79, the database-side cache 136, the web server 13, and the app 15. The database answered 215. In the last 250 reads, once the caches were warm, only 36 of 250 reached the database. The edge did most of the work because it sits in front of everything and is shared by every client.

What happens when the price changes?

At t = 250 seconds the script raises the price of item 1, the hottest row, from $10.01 to $11.01. The app deletes item 1 from its own maps and from the database-side cache, because it owns both.

With timers only, 27 responses carried the old price, the last at +112 seconds. The CDN edge was last sampled with the old value at +114.2 seconds, the web cache at +24.2 seconds, and the browser at +54.2 seconds. Purging the CDN and the web cache at the moment of the write cut that to 2 old-price responses, the last at +36 seconds. But a browser copy was still fresh at +54.2 seconds even after the purge.

That is the point a purge cannot reach: a server cannot push a delete into a browser cache. What works is set before the change: a short max-age for data that changes, or no-cache so the browser revalidates every time.

Budget staleness for the whole chain

Write the worst case down per path: about 120 seconds with timers only, about 150 when the web cache resets the age, and about 60 once you purge the CDN and web on write. The browser is the floor, because a purge cannot reach it.

Our take

Caching at five layers took the database from 1,000 reads to 215 in this demo, and most of that came from the shared edge. List every cache, its TTL and how it is cleared, and write the worst-case staleness for the whole path before you choose TTLs.

Read the full article with the demo tables and the staleness budget: https://systemdesignsimple.com/five-places-a-cache-can-hide/

Top comments (0)