How caching works
Caching keeps a copy of an answer close to whoever asks for it. A request is served by the nearest layer that has a fresh copy, and only goes deeper when none does.
A request goes only as deep as it must
Browser cache
Instant, per user
CDN
Shared, near the user
Application cache
In memory, shared by servers
Database
The source of truth
Freshness
Each cached copy says how long it may be reused: for example a max-age header for browsers and CDNs, or a time to live in an application cache. After that the copy must be checked or replaced.
The hard part: invalidation
The difficulty is removing a copy when the data changes. Common answers are short lifetimes, versioned file names for assets (app.4f2a.js) and explicit purges when something is edited.
What not to cache
Do not share responses that depend on the user, such as account pages, in a public cache. Mark them private, or leave them uncached.
Questions
What is a CDN?
A network of servers around the world that keep copies of your files and serve them from the one nearest to the visitor.
What does cache hit ratio mean?
The share of requests answered from a cache. Higher means less load on deeper layers.
Why does my change not show up?
A layer is still serving an older copy. Wait for its lifetime, purge it, or change the file name.
Is caching only for speed?
Also for cost and resilience: fewer requests reach your database, and a cache can keep serving when the origin is slow.