Skip to content

Caching Basics

Caching stores a copy of frequently accessed data in a fast storage layer. When data is requested again, the cache serves it instead of recomputing or fetching from the slow database.

Analogy — Coffee shop:

  • Without cache: every latte requires roasting beans, grinding, and brewing (slow).
  • With cache: the barista pre-makes popular drinks and serves them instantly.

flowchart LR
Client["📱 Client Browser"] -->|"1. Browser Cache"| CDN["🌐 CDN Edge"]
CDN -->|"2. CDN Cache"| Reverse["Reverse Proxy / LB"]
Reverse -->|"3. Server Cache<br/>(Redis, Memcached)"| App["App Server"]
App -->|"4. Database Cache<br/>(Buffer Pool)"| DB[("Database")]
style Client fill:#7c3aed,color:#fff
style CDN fill:#4f46e5,color:#fff
style Reverse fill:#6366f1,color:#fff
style App fill:#8b5cf6,color:#fff
style DB fill:#dc2626,color:#fff
Cache LayerLatencyCapacityExample
Browser~1 msSmallLocalStorage, HTTP cache headers
CDN~10-50 msMediumCloudFlare, Fastly, CloudFront
Server Cache~1-5 msMediumRedis, Memcached
Database Cache~0.1 msLargeInnoDB buffer pool

Data TypeCache?Why
Static assets (images, CSS, JS)✅ YesNever changes, serve from CDN
User session data✅ YesRead on every request, fast access
Database query results✅ YesExpensive queries, slow to recompute
User feed (personalized)✅ MaybeCache for a few seconds, then refresh
Real-time stock prices❌ NoChanges too fast, stale data is bad
Passwords / sensitive data❌ NoSecurity risk

ScenarioLatency
Cache hit (Redis, in-memory)~1-5 ms
Cache miss (query database)~10-100 ms
Cache hit (CDN, edge)~10-50 ms
No cache (full origin)~100-500 ms

A cache hit is 10-100× faster than a database query.


  • Cache adds complexity (another system to manage).
  • Stale data: cache might serve outdated content.
  • Cache breakdown: if the cache goes down and all requests hit the database at once (thundering herd), the database can crash.

  • Cache = storing copies of slow-to-get data in a fast place (memory).
  • Cache data that’s read often and changes rarely.
  • A cache hit takes milliseconds. A cache miss costs a database query (tens of milliseconds).