Aditya Dutt Pandey

How we work · 2026

Published · 3 min read

The bug where nothing was wrong

No error, no alert, no failing test. People just saw old ticket types for up to ten minutes. Why cache-aside breaks in the writer you add six months later, the race most people never notice, and how we cache on tunyt.

Some bugs throw errors. This one didn't. No exception, no alert, no red graph, no failing test, every service healthy. An organiser changed their ticket types, and people kept seeing the old ones for up to ten minutes.

Cache the hottest read

Every event page on tunyt needs that event's ticket types. It's one of our hottest reads and it rarely changes, which is the textbook case for a cache. So we put one in front of it. The pattern is cache-aside, and every backend has some version of it:

async function getTicketTypes(eventId: string) {
  const key = `cache:ticket:type:event:${eventId}`;
  const hit = await cacheGet(key);
  if (hit) return hit;

  const rows = await db.ticketType.findMany({ where: { eventId }, take: 200 });
  await cacheSet(key, rows, { ttlSeconds: 600 });
  return rows;
}

Read from the cache. On a miss, read from the database and write the result back.

Why ten minutes

A TTL is a trade. Longer means less database load and more risk of serving stale data. Shorter means fresher data and more load. Ticket types don't change often, so ten minutes saves a lot of work.

The TTL is also the longest any staleness bug can live. Keep that in mind. Those ten lines are fine, and the bug is never in them.

The writer you add later

The first way this breaks shows up months later. Someone adds a new way to change ticket types: a bulk edit, an admin tool, a one-off script. It updates the database correctly and passes its tests. It never touches the cache, because whoever wrote it didn't know the cache existed.

Now the database says one thing, the cache says another, and the cache wins for up to ten minutes. If what changed was a price or a sold-out flag, that's a support ticket at best. This bug is on our own performance audit list, so it's not rare.

The race

The second way it breaks lives inside the ten lines. Two requests at almost the same moment:

  1. A reader misses the cache and reads the ticket types from the database.
  2. A writer updates the ticket types and deletes the cache key.
  3. The reader, holding data from before the update, writes it back into the cache.

The cache now holds stale data until the TTL runs out. Every step was correct. The order was the bug.

You can work around it with versioned keys or a short delayed second delete. It's also why the TTL exists: it puts a ceiling on every bug you haven't found yet.

We didn't shorten the TTL

The tempting fix for both bugs is a shorter TTL. Make it a minute and the damage is smaller.

The cache exists to take load off the database. A shorter TTL hands some of that load back and still serves stale data, just for less time. You pay forever to shrink a bug you could remove. The TTL stays as the safety net.

Every writer invalidates, through one helper

One helper deletes the ticket-type cache for an event, and every code path that changes ticket types calls it. It's a written rule and it's on the checklist anyone, person or coding agent, runs before shipping a cache: invalidation wired at every place the data changes.

A helper beats deleting the key inline because you can search for it. You find every caller in seconds, and a code path that changes ticket types without calling it stands out in review.

One shape for every key

Every key looks like cache:{type}:{identifier}. Search the codebase for a type and you find every place that reads or writes it. If you can't find every reader, you can't be sure you've invalidated them.

How I review caches now

A cache is a second copy of the truth, and every function that writes the first copy has to keep the second one right. So when a new cache shows up in review, I skip the read. It's ten lines and it's always fine. I ask to see every write.

By , CTO of tunyt

← All writing