← Statpro dev log

A cache that could never hit

#web#infra#sports

A timeline of many thin tick marks, with one group of ticks gathered by converging lines into a single glowing amber block

The fantasy football board on Statpro was my top production error of November. Under load, the query behind it kept running into a 10-second statement timeout. That query had a cache in front of it the whole time.

Statpro is a sports stats site and app. The board is its page of fantasy football rankings and start/sit advice (which players to put in your lineup) for the current NFL week. It is slow to compute, so I cache it.

The key changed on every call

The weekly board is memoized, which means a finished result is stored and reused. The memo keys on a cutoff time, the moment up to which data counts. The web page's loader passed a fresh Date on every call. A key that holds the current millisecond is never seen twice, so the memo never hit. One page view also asked for the board more than once, so every visit ran the heavy query at least twice.

Nothing looked wrong in review. There was a cache, it had a key, and the tests passed. It only failed at its one job, which is answering the same question a second time.

BEFORE a new key on every call Page view Key holds the current millisecond Memo misses, every time Heavy query, 10 s timeout at least twice per view AFTER one key per five-minute window Page view Key floored to five minutes Memo hits Board served database slow? the last good board, up to 6 hours old
Same query, same cache. Only the key changed.

Round the clock, keep the last good answer

The fix was already sitting in the code that serves the mobile app: floor the cutoff to five minutes. Every request inside the same window now builds the same key, and the second one is free.

I added a guard for the bad minutes as well. When the database read is unavailable, the loader serves that week's last good board, up to six hours old, and reports one error per call at most. A slightly stale board beats an error page.

Test that the cache hits

Most cache tests check that the cached value is correct. Few check that a second identical call skips the work. Count the calls to the expensive function in a test, and look hard at anything time-shaped in a cache key.

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