Appearance
Caching Basics
41. What is caching, and why is it used?
The short answer
A cache keeps a copy of data somewhere faster to reach than its source, so repeated requests skip the slow work. Reading a product from Redis in memory takes well under a millisecond; computing it from five database joins may take 30 milliseconds. Caching cuts latency, reduces load on databases and APIs so they can serve more users, and saves cost. The price is that the copy can be stale, out of date compared with the source, so every cache needs a plan for when and how copies are refreshed.
Caches are everywhere along a request:
| Layer | Example | Typical speed |
|---|---|---|
| Browser | Cache-Control on images, scripts, API responses | No network at all |
| CDN | Cloudflare, CloudFront edge copies | Close to the user, tens of ms |
| Reverse proxy | Nginx cache | Skips your app |
| Application memory | An in-process Map or LRU | Nanoseconds to microseconds |
| Shared cache | Redis, Memcached | Under a millisecond on the same network |
| Database | PostgreSQL's buffer cache of recently used pages | Avoids disk reads |
What makes something worth caching: read often, changed rarely, expensive to produce, and acceptable if slightly out of date. A product page fits all four. A bank balance shown before a payment does not.
42. What is Redis, and when would you use it?
The short answer
Redis is an in-memory data store that holds keys with values of several types, strings, hashes, lists, sets, sorted sets, streams and more, and answers in well under a millisecond. It runs commands one at a time on a single main thread, so each command is atomic. Use it as a cache, a session store, for rate limit counters, leaderboards with sorted sets, distributed locks, queues and job systems, and pub/sub messaging. Do not use it as the only home of data you cannot lose without understanding its persistence settings (Redis persistence).
| Data type | Example use | Commands |
|---|---|---|
| String | Cached JSON, counters | GET, SET … EX 60, INCR |
| Hash | A user's session fields | HSET, HGETALL |
| List | A simple queue | LPUSH, BRPOP |
| Set | Unique visitors today | SADD, SCARD |
| Sorted set | Leaderboard by score | ZADD, ZREVRANGE |
| Stream | Event log with consumer groups | XADD, XREADGROUP |
ts
import { createClient } from 'redis'
const redis = createClient({ url: config.REDIS_URL })
await redis.connect()
await redis.set('product:42', JSON.stringify(product), { EX: 300 }) // (1)
const cached = await redis.get('product:42') // (2)
await redis.zAdd('leaderboard:2026-10', { score: 4820, value: 'u_42' }) // (3)
const top10 = await redis.zRangeWithScores('leaderboard:2026-10', 0, 9, { REV: true })- Store a value with a 300-second expiry.
- Read it back, or get
nullif it expired or was never set. - A sorted set keeps members ordered by score, so the top ten is one fast command.
Licence note for interviews. Redis changed its licence in 2024, and the Linux Foundation project Valkey forked from it; Redis 8 later added an open-source AGPL option. Cloud providers offer both. The commands are the same.
43. What is cache invalidation, and why is it difficult?
The short answer
Cache invalidation means removing or updating cached copies when the source data changes, so users do not see stale data. It is hard because of timing and distribution. The cache and the database are two systems with no shared transaction, so a crash between "update database" and "delete cache key" leaves a stale entry. Concurrent requests can re-fill the cache with old data just after you deleted it. One piece of data may appear in many cached results, such as a product in dozens of listing pages. And copies may live in many places at once: memory, Redis, CDN and browsers.
The classic race, step by step:
- Request A misses the cache and reads product 42 from the database: price £10.
- An admin updates the price to £12 in the database and deletes the cache key.
- Request A, slow, now writes its old value, £10, into the cache.
- Everyone sees £10 until the TTL expires.
Defences, combined:
| Problem | Defence |
|---|---|
| Missed deletes and races | Always set a TTL, so mistakes heal themselves |
| Race in steps 1 to 3 | Delete again a moment after the write, or store a version number with the value |
| One change affects many keys | Tag-based or versioned keys: products:v17:list, bump the version on change |
| Invalidating from many services | Publish a change event that every cache listens to |
| Copies in CDN and browsers | Short max-age, plus CDN purge APIs; versioned URLs for static files |
The rule to say out loud: delete the cache entry after the database commit, never before, and never update the cache with a value you computed before the commit.
44. What is the cache-aside pattern?
The short answer
Cache-aside, also called lazy loading, is the most common caching pattern. The application manages the cache itself: on a read, it checks the cache first; on a hit, it returns the cached value; on a miss, it reads from the database, stores the result in the cache with a TTL, and returns it. On a write, it updates the database, then deletes the cache key so the next read reloads fresh data. Only data that is actually requested gets cached, and if the cache is down, the app still works by going straight to the database.
ts
export async function getProduct(id: number): Promise<Product | null> {
const key = `product:${id}`
const hit = await redis.get(key).catch(() => null) // (1)
if (hit) return JSON.parse(hit)
const { rows } = await pool.query('SELECT id, name, price_pence FROM products WHERE id = $1', [id])
const product = rows[0] ?? null
await redis.set(key, JSON.stringify(product), { EX: product ? 300 : 30 }).catch(() => {}) // (2)
return product
}
export async function updatePrice(id: number, pricePence: number) {
await pool.query('UPDATE products SET price_pence = $1 WHERE id = $2', [pricePence, id])
await redis.del(`product:${id}`) // (3)
}- A cache error counts as a miss, so a Redis outage slows the site down instead of breaking it.
- Cache "not found" too, briefly. Otherwise someone requesting a non-existent product ID over and over hits the database every time. This is called negative caching.
- Delete, do not update, after the database write. The next read loads the true value.
45. What are write-through and write-behind caching?
The short answer
In write-through, every write goes to the cache and the database at the same time, usually through a cache layer that does both, so the cache is always populated and fresh, at the cost of slower writes and caching data nobody may read. In write-behind, also called write-back, writes go to the cache first and are saved to the database later, in batches, so writes are very fast and the database load is smooth, but a cache crash before the flush loses data. Write-behind suits counters and analytics; payments must never use it.
| Pattern | Write goes to | Read sees | Risk | Use for |
|---|---|---|---|---|
| Cache-aside | Database, then delete cache key | Fresh after a miss | First read after a write is slow | General purpose (question 44) |
| Read-through | Database | Cache loads itself on a miss | Same as cache-aside | Caching libraries and products |
| Write-through | Cache and database together | Always fresh | Slower writes; caches unread data | Data read immediately after writing |
| Write-behind | Cache now, database later | Fresh from the cache | Data loss if the cache dies | View counts, likes, metrics |
Write-behind, as an example. A video's view count gets 5,000 increments a second. Writing each one to PostgreSQL would overload it. Instead, INCR video:991:views in Redis, and a job every 10 seconds adds the accumulated count to the database and resets the counter. Losing ten seconds of view counts in a crash is acceptable. Losing ten seconds of orders is not.
46. What is a distributed cache?
The short answer
A distributed cache is a cache that lives on separate servers shared by all your app instances, such as a Redis or Memcached cluster, instead of inside each process's memory. Every instance sees the same cached values, an invalidation happens once for everyone, the cache survives app restarts and deploys, and it can grow beyond one machine's memory by spreading keys across nodes. The cost is a network round trip for each access, and one more system to run and keep highly available.
| In-process cache | Distributed cache | |
|---|---|---|
| Lives in | Each app instance's memory | Redis or Memcached servers |
| Speed | Fastest, no network | About 0.2 to 1 ms per call |
| Same data on every instance | No | Yes |
| Invalidation | Must reach every instance | Once |
| Survives deploys | No | Yes |
| Good for | Tiny, hot, rarely changing data such as feature flags | Sessions, shared results, rate limits |
Using both, a two-level cache. Keep a small in-memory LRU for the hottest keys with a very short TTL, such as 5 seconds, in front of Redis. Most requests never leave the process, and staleness is limited to a few seconds.
47. What is a cache TTL, and how should it be selected?
The short answer
TTL, time to live, is how long a cached entry stays valid before it expires automatically. Choose it by asking how stale may this data be, and how expensive is a miss. Data that changes rarely and is costly to build, such as a category tree, can live for hours. Data that changes often, such as stock levels, gets seconds. Always set a TTL, even when you also invalidate on change, as a safety net. Add a little random jitter so thousands of keys created together do not all expire at the same moment.
| Data | Typical TTL | Reason |
|---|---|---|
| Static assets with a hash in the name | 1 year | The URL changes when the file changes |
| Category tree, site config | 1 to 24 hours | Rarely changes; invalidate on edit |
| Product details | 5 to 15 minutes | Changes sometimes; invalidate on edit |
| Search results, listings | 30 to 120 seconds | Many inputs; hard to invalidate exactly |
| Stock count, prices in checkout | A few seconds, or none | Wrong values cost money |
| Not-found results | 30 to 60 seconds | Stops repeated misses without hiding new items long |
ts
const ttl = (base: number) => base + Math.floor(Math.random() * base * 0.1) // up to 10% extra
await redis.set(key, value, { EX: ttl(300) }) // 300 to 330 secondsWhy jitter matters. If a deploy warms 50,000 keys with exactly 300 seconds, all 50,000 expire in the same second five minutes later, and the database takes 50,000 queries at once (cache stampedes).
48. How would you cache API responses?
The short answer
Use two layers. HTTP caching: send Cache-Control headers so browsers and CDNs can store responses, with public, max-age=60 for shared data, private for per-user data, no-store for sensitive data, and an ETag so clients can revalidate cheaply with 304 Not Modified. Server-side caching: store the computed response in Redis, keyed by everything that changes the result, path, query parameters, user or tenant if personalised, and the API version, with a TTL and invalidation on change. Never let one user's private response be served to another.
ts
app.get('/api/products', async (req, res) => {
const key = `api:v1:products:${new URLSearchParams(req.query as Record<string, string>).toString()}` // (1)
let body = await redis.get(key)
if (!body) {
body = JSON.stringify(await listProducts(req.query))
await redis.set(key, body, { EX: 60 })
}
res.set('Cache-Control', 'public, max-age=30, stale-while-revalidate=60') // (2)
res.type('application/json').send(body)
})
app.get('/api/me/orders', requireUser, async (req, res) => {
res.set('Cache-Control', 'private, no-store') // (3)
res.json(await ordersFor(req.user.id))
})- The key includes the query string, so
?page=2and?page=3are cached separately. Normalise the order of parameters so?a=1&b=2and?b=2&a=1share a key. - Let browsers and CDNs keep it for 30 seconds, and serve a slightly stale copy for up to 60 more while they fetch a fresh one in the background.
- Personal data: no shared caching at all.
| Header | Meaning |
|---|---|
public, max-age=60 | Anyone, CDNs included, may cache for 60 seconds |
private, max-age=60 | Only the user's own browser may cache |
no-cache | Cache it, but revalidate with the server before each use |
no-store | Never store it anywhere |
s-maxage=300 | A separate lifetime for shared caches such as CDNs |
stale-while-revalidate=60 | Serve stale for 60 seconds while refreshing |
ETag and If-None-Match | Revalidate; 304 with no body if unchanged (ETags) |