Home›Design›Leaderboards and counters
Design doc · Leaderboards and counters

Leaderboards, counters and rate limits

Where should fast-changing counts go: views, likes, scores, rate limits?

Applications like this: game leaderboards, like counts, view counters, API rate limits. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.

The short answerRedis or Valkey: sorted sets for leaderboards, INCR with a TTL for counters and rate limits. Flush totals to the database in batches if they must be kept.
1 · The workload

What the application asks of the database

DimensionThis application
WritesVery high, on a few hot keys.
ReadsTop N, my rank, the current count.
Read to writeBalanced or write-heavy.
ConsistencyApproximate is usually fine. A like count can be a second behind.
LatencySub-millisecond per operation.
GrowthSmall. One entry per player or per object.
Connected

Real failures behind this design

These incidents are why the choices below matter. Each one is written up with its numbers and sources.

2 · What goes where

Each kind of data, its store, and why

ApplicationLeaderboardsRedis sorted setsCAN BE LOSTCounters and rate limitsRedis INCR with EXPIRECAN BE LOSTTotals you must keepThe database, updated in batchesSOURCE OF TRUTH

Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.

DataWhere it goesWhy
LeaderboardsRedis sorted setsRank queries are built in, and each update is O(log n) in memory.
Counters and rate limitsRedis INCR with EXPIREAtomic increments with no lock, and expiry built in.
Totals you must keepThe database, updated in batchesOne write per minute per object instead of one per event.

How each family of store behaves under traffic: Database families.

3 · Data model

The core tables and the queries they serve

# leaderboard
ZINCRBY lb:season7 50 player:42
ZREVRANGE lb:season7 0 9 WITHSCORES   # top 10
ZREVRANK lb:season7 player:42           # my rank

# fixed-window rate limit: 100 requests per minute per key
MULTI
INCR rl:api:key123:202611181305
EXPIRE rl:api:key123:202611181305 60 NX
EXEC
# allow the request only if the count returned is 100 or less
4 · How it grows

Start simple, change one thing at a time

  1. Counters in the databaseFine at low rates.
  2. Hot rowsMove counting to Redis, flush to the database every minute.
  3. Very large leaderboardsShard by season or region, and keep top N in one key.

Scale it: watch this design grow

Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.

5 · What breaks first

The failures to plan for

What happensWhat you seeWhat I do
UPDATE counters SET n = n + 1 on one hot rowLock waits, lock pile-upMove the counter to Redis, or spread it across several rows and sum them.
KEYS * in productionRedis stalls for everyoneUse SCAN.
Counters with no TTLMemory grows foreverExpire window keys.
6 · Alternatives

What I considered and did not pick

Cost. Small. A modest Redis node handles very high operation rates. The cost risk is memory from keys that never expire.

How this page was writtenFrom the engines' documentation and common practice. Measured on real servers: a Redis leaderboard and rate limit, in the notebooks. Before you build: check the anti-patterns, size it with the capacity calculator, and write your own version from the design doc template. Want a second opinion? Submit it for review.
7 · Where to run it

On-prem, managed, EC2 or Kubernetes

StageWhere it runs
Where I would startManaged Redis or Valkey
When it growsThe same. MemoryDB-style durable options if the totals must survive a failover

For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.

8 · Estimate it

The numbers, worked live

The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.

Part 3 of 4 · Design · 24 pagesAll pages

Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.