Caching a Next.js API proxy layer with Redis (ioredis) — what to cache, what not to.

Every Next.js app eventually talks to a slower upstream API. A thin proxy layer with Redis in front can cut response times from hundreds of milliseconds to single digits — but only if you cache the right things. Here is a practical, numbers-driven guide to deciding what belongs in Redis and what does not.

personal image

Hossam Mohamed

April 25, 2026

About Why a proxy layer at all

1- Next.js App Router already has fetch-level caching, but it is tied to the render cycle and hard to invalidate precisely across instances. 2- A dedicated proxy (route handlers or a small Node service) gives you one place to add Redis, rate limits, auth headers, and schema validation — and lets you share the cache between SSR, client fetches, and background jobs.

Next.js API proxy layer with Redis caching

1- What you should cache

Good cache candidates are read-heavy, change slowly, and are expensive or slow to regenerate. These are the wins that show up clearly in p95 latency graphs.

Strong candidates:

Example: cached upstream call with ioredis

ioredis cache-aside helper wrapping an upstream fetch

Typical before/after latency

p50 and p95 latency before and after adding Redis

2- What you should NOT cache

Caching the wrong response is worse than no cache — it leaks data, masks bugs, and makes incidents harder to diagnose. The rule of thumb is: if a wrong cached value can hurt a user, do not cache it.

Anti-patterns to avoid:

Cache key shape for per-user data

user-scoped cache key example

3- TTL and invalidation — the part everyone gets wrong

Picking a TTL is not about guessing a number. It is about how fresh the data must look and how much load the upstream can take during a cache stampede.

Rules that hold up in production:

4- You cannot tune what you do not measure

Before you add a single cache, write down the numbers you expect to move. After the rollout, check them. If the hit rate is low or the latency did not change, the cache is paying rent for nothing.

Metrics worth shipping from day one:

cache hit rate and latency dashboard

Newsletter

Leave message for me to get updates about my latest projects. or if you have any suggestion i will be happy to hear .