Ranking by exponential decay without a cron job
I built a board of 1,000 tiles where anyone can pay to take a tile away from whoever currently holds it. Tiles are sorted by "heat" — a value that decays over time — so the board reorders itself continuously. The obvious way to do that is a scheduled job that recomputes every row on a timer. I didn't want a scheduled job. Here's how you avoid one. The problem Heat is the sum of every payment a…
The author built a board with 1,000 tiles where the order changes based on a value called heat. Heat is calculated by adding up payments, each with an exponential decay factor. To avoid frequent calculations, the author stored heat as a single pair (P, t_P) instead of individual payments. They then used logarithms to simplify heat calculations and stored it as rank_key.
This rank_key is used to order tiles, with no need for a scheduled job to recompute rankings. The author also provides a function for adding heat values in a numerically stable way. They caution against choosing the wrong time unit for rank_key calculations and emphasize that rank_key is only for ordering, not for computing actual heat values.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.