← Statpro dev log

A live score is a promise about time

#web#product#infra

A straight amber line carries a glowing dot through a gate and onward, while a dashed path that loops and arrives late is stopped at the gate by a small red bar

"Final" was sitting on a game that had just gone to overtime. Scores update, and that's supposed to be the whole job: poll the API, patch the screen, go home. Last night Statpro, my sports stats site and mobile app, was full of that lie.

Fixing it turned into the most interesting bug class I've hit in a while: time itself as a caching problem.

A live score is a fact with a shelf life

The score was 24-21 at 8:42 in the third. By the time a phone renders it, that fact might already be history. So I can't cache it like a standings table. I also can't have every phone hammering the origin every second during a playoff game.

The shape I landed on is two public snapshot endpoints, one per league scoreboard and one per game. They're cheap to serve because the edge caches each response for five seconds:

Cache-Control: public, s-maxage=5, stale-while-revalidate=10

The first directive lets the shared cache keep a response for five seconds. The second lets it serve the old copy for up to ten more while it fetches a fresh one. During a game, the first request after the cache expires pays the origin cost, and everyone else inside that five-second window shares it.

Between games the same endpoints serve the last snapshot, which is why the mobile app can poll them around the clock without a special game-day mode.

The app polls every 30 seconds, and only when three things are true: a game in view is live, the screen is focused and the app is in the foreground. A backgrounded phone sending heartbeat requests is how you earn battery-drain reviews and a rate-limited API in the same week.

Two clocks, one ordering bug

Once there are two clocks, the game and the poller, there are ordering problems. The nasty one goes like this. A poll fires and its response sits in a socket queue for a few seconds. A newer poll returns first. Then the stale one lands and overwrites it.

The game just went backwards. Q3 4:12 flips to Q3 5:01, and users screenshot that.

BEFORE the last response to arrive wins Newer snapshot lands first Stale snapshot lands late Cache takes the last arrival Q3 4:12 flips back to Q3 5:01 backwards AFTER the newest observedAt wins Newer snapshot lands first Stale snapshot lands late Merge compares observedAt the older snapshot is dropped in order
Same network, same late response. The merge is the only place left to enforce order.

The fix is boring on purpose. Every snapshot carries an observedAt timestamp. The merge into the React Query cache compares it against what's already there and drops anything older. A late snapshot never moves a game backwards, however strange the network gets.

Uncertainty gets its own UI

The subtle design call was what to show when I don't know. The feed can stall: the provider lags, or a poll fails. A confident, wrong clock is worse than an honest one, so the label carries its own state.

  • While the data is fresh, the game clock renders as it arrived.
  • If no label arrives, or the data goes stale, it degrades to a plain "Live" chip in the live color.
  • When the game ends, it says "Final".

Each state is a claim about the world that I can back.

A milder case on the Ask pages

The second commit of the night treated the same disease. Ask is the box where you type a sports question in plain English, and each answer becomes a page. Those answer pages were re-reading related questions on every answer render. Now they re-read once a minute per league.

Realtime is mostly timestamp discipline

The transport is rarely the hard part. The hard part is deciding, at the merge point, which fact wins. Arrival order is all UDP promised in 1980, and it's still lying to UI engineers today.

If your app shows anything time-shaped (scores, prices, delivery ETAs, "active now" dots), ask what your equivalent of observedAt is. If the answer is "nothing, last write wins", you have this bug and haven't seen it yet.

This is the build log for Statpro (statpro.io) · All entries · Weekly essays