# Server-Side Caching with Redis

> Cache expensive results in Redis or Memcached using the right pattern, TTLs and invalidation strategy. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/server-side-caching
Last updated: 2026-11-28

Cache expensive results in Redis or Memcached using the right pattern, TTLs and invalidation strategy.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [Indexes and Query Performance](https://www.internhack.in/roadmaps/tracks/backend/indexes-query-performance)

A cache stores the result of expensive work (a slow query, an external API call, a rendered page) so the next request is served from fast memory. Caching is often the cheapest big performance win, and also a common source of stale-data bugs.

### Stores

- **Redis**: an in-memory data structure server with strings, hashes, lists, sets, sorted sets, TTLs, pub/sub and streams. The default choice; also used for sessions, rate limiting and queues.
- **Memcached**: a simpler, multi-threaded key-value cache. Fine when you only need plain get/set.
- **In-process caches** (an LRU map in your app): fastest, but each instance has its own copy and memory is limited.

### Patterns

- **Cache-aside** (most common): read from cache; on a miss, load from the database and populate the cache with a TTL.
- **Write-through**: write to the cache and database together.
- **Write-behind**: write to cache, flush to the database later (risky, rarely needed).

### Invalidation

"There are only two hard things in computer science: cache invalidation and naming things." Options: short **TTLs**, deleting keys on writes, or versioned keys (`user:42:v7`). Choose based on how stale data may be.

### Pitfalls

- **Cache stampede**: a hot key expires and thousands of requests hit the database at once. Use locking, request coalescing or jittered TTLs.
- Caching per-user data under a shared key and leaking it to other users.
- Treating the cache as the source of truth. It can be evicted any time.

You are ready to move on when you can add cache-aside caching to a slow endpoint, measure the latency improvement and explain your invalidation rule.

## Key concepts

- **Redis data structures**
- **Memcached**
- **In-process LRU caches**
- **Cache-aside pattern**
- **Write-through and write-behind**
- **TTLs and invalidation**
- **Cache stampede prevention**
- **Eviction policies**

## Mini project

Add Redis cache-aside to a slow product listing endpoint, invalidate on product updates, add jittered TTLs, and load-test before and after to report p50 and p95 latency.

## Check yourself

A popular cache key expires and your database CPU spikes to 100 percent. What happened and how do you prevent it?

## Free resources

- [Redis documentation](https://redis.io/docs/) (Redis)
- [Memcached](https://memcached.org/) (Memcached)
- [Caching guidance (Azure Architecture Center)](https://learn.microsoft.com/en-us/azure/architecture/best-practices/caching) (Microsoft Learn)

Previous: [Continuous Integration and Delivery](https://www.internhack.in/roadmaps/tracks/backend/ci-cd) · Next: [HTTP Caching, CDNs and Client Caches](https://www.internhack.in/roadmaps/tracks/backend/http-caching-cdn)

## More in Caching for Speed

- [Server-Side Caching with Redis](https://www.internhack.in/roadmaps/tracks/backend/server-side-caching)
- [HTTP Caching, CDNs and Client Caches](https://www.internhack.in/roadmaps/tracks/backend/http-caching-cdn)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
