Redis · Solution
Realtime leaderboards and rate limiting
Sorted sets for leaderboards, atomic Lua scripts for rate limiting — both run inside Redis without a round trip to your database.
How it works
Rank players with a sorted set and read the top scores back in order. For rate limiting, a single Lua script checks and updates a counter atomically, so concurrent requests never race.
Sorted sets for ranking
Score and rank players with ZADD and ZRANGE.
Atomic rate limiting
A single Lua script checks and updates a counter without a race condition.
Code sample
luaGCRA rate-limit as a single atomic script
-- KEYS[1]: the rate-limit key-- ARGV[1]: emission interval in milliseconds (e.g. 200 = 5 req/s)-- ARGV[2]: burst tolerance in milliseconds (e.g. 1000 = burst of 5)-- Returns: 1 if allowed, 0 if rate-limited. local now = redis.call('TIME')local now_ms = tonumber(now[1]) * 1000 + math.floor(tonumber(now[2]) / 1000)local emission_interval = tonumber(ARGV[1])local burst = tonumber(ARGV[2]) local last = tonumber(redis.call('GET', KEYS[1])) or 0local tat = math.max(last, now_ms)local allow_at = tat - burst if now_ms < allow_at then return 0end redis.call('SET', KEYS[1], tat + emission_interval, 'PX', burst + emission_interval)return 1Sizing
A sorted set with a million members fits comfortably within a few hundred megabytes of memory.
Examples
- Game leaderboard prototypes
- API rate-limit experiments
- Realtime ranking demos
Frequently asked questions
Are Lua scripts atomic?
Yes — a script runs as a single atomic operation on the Redis event loop.
What happens if two players score at once?
Sorted set updates are atomic, so concurrent writes resolve correctly.
Is INCR really safe under concurrency?
Yes — Redis processes commands single-threaded, so INCR is atomic by definition. Same for INCRBY, DECR, sorted-set updates, and Lua scripts.
How do I avoid hot-key contention on rate-limits?
Most rate-limits are naturally sharded across IPs or API keys, so single-key contention is rare. If you need one global limit, shard the key in your application (for example by minute bucket) and sum on read.
Build the realtime pattern.
256 MB free Redis handles tens of thousands of concurrent leaderboard entries. Luceris Cloud is for development environments and not for production.