Back to Case Studies
I read about caching and here's what I know now
Web Development

I read about caching and here's what I know now

Akash Sarki

Akash Sarki

9 September 2026

Caching:

Caching is a core computer science concept where you store copies of frequently accessed data in a temporary, high-speed storage layer (the cache) that is closer to the requester than the original data source.

The Goal:

Reduce latency (time taken to get data) and throughput (number of requests the original data source can handle) by avoiding repeated, expensive operations (like API calls, database queries, or heavy computation).

🛠️ The Core Mechanics of Caching:

Every cache operates on two basic principles:
1. The Cache Hit/Miss and Cache Eviction.

Cache Hit - The requested data is found in the cache - Data is served instantly from the cache. Fast and efficient.

Cache Miss - The requested data is not found in the cache. - The system must query the original source (e.g., the API or database). The result is then stored in the cache for future requests. Slow.

2. Cache Eviction (When to Delete Data)

Caches have finite size. When a cache is full, a policy dictates which existing item to remove to make space for a new one. Common policies include:

a. Least Recently Used (LRU): Removes the item that has been accessed the least recently. (Most common and effective).

b. Time-To-Live (TTL): Removes an item after a specified amount of time, regardless of access frequency (e.g., an item expires after 1 hour). (Most common for web data like API responses).

c. First-In, First-Out (FIFO): Removes the oldest item, regardless of use.

🏗️ The Caching Hierarchy: Types and Layers:

Caching is implemented at multiple points, forming a hierarchy from the user's browser all the way to the database server. These are the main types you will encounter:

1. Client-Side Caching (Frontend/Browser)

HTTP Caching (Browser Cache): Controlled by HTTP headers (Cache-Control, ETag, Last-Modified). Stores static assets (images, CSS, JS) and sometimes API responses. This is what you see when you inspect the browser's disk cache.

Service Workers: A programmable proxy running in the browser that allows developers to precisely control which network requests are cached and how (used extensively in PWAs).

Client-Side Storage: Using Local Storage (persistent) or IndexedDB (structured data) to save application state or large data structures.

2. Edge/CDN Caching (Content Delivery Network)

CDNs are globally distributed networks of servers (edge servers) that sit between the user and your application server.

Mechanism: They cache public, static, or rapidly generated content (like the entire HTML page or optimized images) at geographically closer points to the user.

Example: When a user in London requests your site hosted in New York, the HTML and assets are served from a CDN edge server in London. Next.js's Route Cache heavily relies on this layer.

3. Server-Side Caching

This includes caches that live within your backend infrastructure.

Reverse Proxy Caching: Tools like NGINX or Varnish can cache full server responses before they even hit your application code.

In-Memory Caching (Data Cache): Dedicated, lightning-fast services like Redis or Memcached. These store key/value pairs of database query results or rendered HTML snippets. Next.js's Data Cache conceptually falls into this category on the server.

Database Caching: The database itself caches frequently accessed query results (e.g., MySQL's internal buffer pool).

Disk Cache - User's Hard Drive (SSD/HDD) - Persistent (across sessions) - Medium-Fast - Large assets (images, fonts, large scripts)

Memory Cache - User's RAM (Volatile Memory) - Non-Persistent (cleared on close) - Extremely Fast - Recently accessed, smaller assets for the current session