The Last CachebenderPrologue
Prologue
Prologue · Lesson 1 of 14
Lesson One

The Forgotten Art of Caching

Technique unlocked The Sight seeing where time is being wasted

The problem

Open the storefront's product page. It takes 900 milliseconds to appear.

Now open it again. It takes 900 milliseconds again.

Nothing changed between those two visits. The product has the same name, the same description, the same picture. Your server did the exact same work, twice, and got the exact same answer, twice.

That is the waste. Caching is the art of noticing it.

What a cache is

A cache (say it “cash”) is a copy of an answer, kept somewhere faster to reach than the place the answer came from.

That is the whole idea. Everything else in this course is detail about where you keep the copy, how long you keep it, and what happens when the copy stops being true.

Why things are slow: two reasons

Almost all slowness comes from one of two causes.

1. Distance. The data is far away and has to travel.

2. Recomputation. The work was already done once, and you are doing it again.

A cache fixes both, in the same way: keep the answer nearby, and stop redoing the work.

How far is “far”?

These numbers are approximate, and they vary by hardware. What matters is not the exact figures but the gaps between them.

Where the data is Roughly how long it takes
In your program's own memory 100 nanoseconds
On a local SSD 100 microseconds (1,000× slower)
On another server in the same datacentre 500 microseconds (5,000× slower)
On a server on another continent 150 milliseconds (1,500,000× slower)

Those numbers are too small to feel. So here is the same table again, with every figure multiplied by the same amount.

These are not real durations. Only the ratios are real. If reading from memory took 1 second, then:

Where the data is If memory took 1 second
Program memory 1 second
Local SSD 17 minutes
Same datacentre 1.4 hours
Another continent 17 days

This is the single most useful intuition in caching. Moving an answer from “another continent” to “program memory” is not a small improvement. It is the difference between waiting 17 days and waiting 1 second.

The trade-off

Caching gives you speed, but it charges you in freshness and memory.

You want What caching does The cost
Speed Reuses an answer instead of producing it again —
Freshness May return an older copy The answer may be out of date
Low memory use Stores additional copies Every copy takes space

A cached copy that no longer matches the latest answer is called stale.

Caching buys speed by accepting some risk of stale data and using additional memory. Every technique in this course makes that trade-off differently.

Why caching is a design decision

Caching is sometimes described as an optimisation added after a system becomes slow. That is only partly true.

Adding a cache creates another copy of your data. That copy may fall behind the original.

For a product description, that may be harmless. For a price or stock count, it may cause a customer to see incorrect information.

You do not need to build every cache from the beginning. But you do need to decide which answers are allowed to be old and for how long. That is a correctness decision, so it belongs in the system design.

At the Storefront

Put the page beside its freshness promises. The same seven fields now become seven different correctness decisions.

Product page example Wayfinder Supply
Wayfinder Supply Atlases Field notes Bag 1Cache note 7
Home Atlases Four Realms Atlas

The Wayfinder collection · Vol. I

Four Realms Atlas

Cache note 3
★★★★★4.9248 reviewsCache note 6

A hand-bound guide to the roads, rivers and hidden passages connecting every realm.

$84.00Cache note 4

Hardcover Illustrated field edition

In stock — 12 leftCache note 5
Qty 1

An illustrated storefront for this course. Checkout is not available.

Freshness contract

What is allowed to be old?

Each limit reflects what happens if the answer is out of date.

# Part May it be stale? If it is, what happens
1 Asset bundle Indefinitely Nothing — version the file
2 Product images For days An old photo remains
3 Product details For hours A correction appears late
4 Price For seconds A customer sees an old price
5 Stock count Barely, if at all You may oversell
6 Reviews For minutes A new review appears late
7 Cart badge Not applicable Another user's answer is wrong

Look at each part and ask two questions.

What does it cost to produce? How long does it take, and how many database queries does it need? That tells you what is worth caching. This is The Sight — you cannot decide what to cache until you know what is expensive.

How old is it allowed to be? That tells you what you are permitted to cache, which is a different question entirely.

Look at part 1 again — the CSS and JavaScript files. It is drawn below the storefront because it is not really part of this page.

It is part of every page.

The same bundle is used by the home page, the category listing, the search results, the checkout, and the account screen. Cache it once and you have not made one page faster — you have made the whole site faster, for every visitor, on every page after their first.

The same goes for anything that repeats: the header, the navigation menu, the footer, the category list that fills the menu.

The value of a cached copy is not what it saves once — it is what it saves multiplied by how often it is reused.

A product description is reused by everyone who views that product. A logo is reused by everyone who views anything.

And the counter-example is on the same page. The cart badge appears on every page too, exactly like the header does. But “on every page” and “the same for everyone” are different things — and only the second one makes something safe to share.