Skip to content

Interview PrepCaching · Core

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:

LayerExampleTypical speed
BrowserCache-Control on images, scripts, API responsesNo network at all
CDNCloudflare, CloudFront edge copiesClose to the user, tens of ms
Reverse proxyNginx cacheSkips your app
Application memoryAn in-process Map or LRUNanoseconds to microseconds
Shared cacheRedis, MemcachedUnder a millisecond on the same network
DatabasePostgreSQL's buffer cache of recently used pagesAvoids 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 typeExample useCommands
StringCached JSON, countersGET, SET … EX 60, INCR
HashA user's session fieldsHSET, HGETALL
ListA simple queueLPUSH, BRPOP
SetUnique visitors todaySADD, SCARD
Sorted setLeaderboard by scoreZADD, ZREVRANGE
StreamEvent log with consumer groupsXADD, 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 })
  1. Store a value with a 300-second expiry.
  2. Read it back, or get null if it expired or was never set.
  3. 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:

  1. Request A misses the cache and reads product 42 from the database: price £10.
  2. An admin updates the price to £12 in the database and deletes the cache key.
  3. Request A, slow, now writes its old value, £10, into the cache.
  4. Everyone sees £10 until the TTL expires.

Defences, combined:

ProblemDefence
Missed deletes and racesAlways set a TTL, so mistakes heal themselves
Race in steps 1 to 3Delete again a moment after the write, or store a version number with the value
One change affects many keysTag-based or versioned keys: products:v17:list, bump the version on change
Invalidating from many servicesPublish a change event that every cache listens to
Copies in CDN and browsersShort 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.

your appGET /products/42Redisproduct:42PostgreSQLsource of truth① check② hit: return③ miss: read④ SET product:42 EX 300on update: write PostgreSQL,then DEL product:42
The app checks, fetches and fills. ① Look in the cache. ② If it is there, done. ③ If not, read the database. ④ Store the result with an expiry, then reply. Writes go to the database, and the cache entry is deleted so the next read refills it.
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)
}
  1. A cache error counts as a miss, so a Redis outage slows the site down instead of breaking it.
  2. 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.
  3. 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.

PatternWrite goes toRead seesRiskUse for
Cache-asideDatabase, then delete cache keyFresh after a missFirst read after a write is slowGeneral purpose (question 44)
Read-throughDatabaseCache loads itself on a missSame as cache-asideCaching libraries and products
Write-throughCache and database togetherAlways freshSlower writes; caches unread dataData read immediately after writing
Write-behindCache now, database laterFresh from the cacheData loss if the cache diesView 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 cacheDistributed cache
Lives inEach app instance's memoryRedis or Memcached servers
SpeedFastest, no networkAbout 0.2 to 1 ms per call
Same data on every instanceNoYes
InvalidationMust reach every instanceOnce
Survives deploysNoYes
Good forTiny, hot, rarely changing data such as feature flagsSessions, 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.

DataTypical TTLReason
Static assets with a hash in the name1 yearThe URL changes when the file changes
Category tree, site config1 to 24 hoursRarely changes; invalidate on edit
Product details5 to 15 minutesChanges sometimes; invalidate on edit
Search results, listings30 to 120 secondsMany inputs; hard to invalidate exactly
Stock count, prices in checkoutA few seconds, or noneWrong values cost money
Not-found results30 to 60 secondsStops 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 seconds

Why 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))
})
  1. The key includes the query string, so ?page=2 and ?page=3 are cached separately. Normalise the order of parameters so ?a=1&b=2 and ?b=2&a=1 share a key.
  2. 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.
  3. Personal data: no shared caching at all.
HeaderMeaning
public, max-age=60Anyone, CDNs included, may cache for 60 seconds
private, max-age=60Only the user's own browser may cache
no-cacheCache it, but revalidate with the server before each use
no-storeNever store it anywhere
s-maxage=300A separate lifetime for shared caches such as CDNs
stale-while-revalidate=60Serve stale for 60 seconds while refreshing
ETag and If-None-MatchRevalidate; 304 with no body if unchanged (ETags)

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Databases & Caching
Foundations
SQL
Performance
Concurrency
Scaling
Caching
Other topics
Nothing picked yet