cat ~/blog

blog

notes, essays & long-form posts by SAIKUMAR

Hello, world — starting this blog

I've been meaning to write more for a while. Not because I have anything particularly important to say, but because writing something down forces me to actually think it through instead of letting it float around in my head as a vague feeling.

So this is the first post. The bar is low. The plan is to write short things, often, about whatever I'm building or reading.

What I'll probably write about

Write drunk, edit sober. — not actually Hemingway

If you're reading this — thanks. See you in the next one.

metawriting

Predictive caching in backend systems

Most caching strategies are reactive: you wait for a request, miss the cache, hit the database, and store the result for next time. That works, but it pays the latency cost on the first request every time.

Predictive caching flips this around — you try to figure out what's likely to be requested next, and warm the cache before the request arrives.

A simple example

If a user just loaded page 1 of a paginated list, there's a good chance page 2 is coming next. So you fetch it in the background and put it in Redis with a short TTL.

async function getPage(userId, page) {
  const key = `list:${userId}:${page}`;
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached);

  const data = await db.fetchPage(userId, page);
  await redis.setex(key, 60, JSON.stringify(data));

  // warm the next page in the background
  prefetch(userId, page + 1);

  return data;
}

The trick is not overdoing it. Prefetch too aggressively and you're just burning database load on things nobody ever asks for.

backendcachingredis