redis-game-patterns · diff
git:20261114.b353ad0 to git:20261114.1296d8c
110 added, 119 removed. Audit A to A.
What this update changes in the checks
No check changed: every fixed check gives the same answer before and after this update.
A tripped check is a label, not a verdict: read the matched text and the lines below. This week's updates that newly trip a check.
---
name: redis-game-patterns
description: >-
- Use when implementing Redis patterns for games — caching, leaderboards, pub/sub messaging, session storage, rate limiting, or ephemeral game state. Triggers: Redis, cache, leaderboard, pub/sub, rate limit, session.
+ Four Redis patterns for game servers: sorted-set leaderboards, heartbeat
+ presence with short-lived match state and timed buffs, a per-player
+ per-action sliding-window rate limit, and cache-aside for player data. Use
+ when a game needs ranked scores, an online/offline indicator, action
+ cooldowns or anti-spam limits, or a fast read path in front of the player
+ database, on Redis through ioredis.
---
# Redis Game Patterns
- ## Purpose
-
- Redis usage patterns for games — leaderboards, session caching, pub/sub messaging, rate limiting, ephemeral state, and sorted sets for real-time game data.
-
- ## When to Use
-
- Trigger: Redis, pub/sub, leaderboard, session cache, rate limiting, sorted sets, game cache, ephemeral state, TTL, player presence, cross-server messaging
-
- ## Prerequisites
-
- None — this is a foundational infrastructure skill.
-
- ## Core Principles
-
- > Will Wright: "Simulation state must be fast, ephemeral, and disposable — persistence is for outcomes, not for process."
-
- 1. **Redis is for hot data, PostgreSQL is for cold data** — cache what's accessed frequently, persist what matters long-term
- 2. **Every cached value needs a TTL** — stale data is worse than no data; always set expiration
- 3. **Use the right data structure** — sorted sets for leaderboards, hashes for sessions, pub/sub for events, strings for simple counters
- 4. **Cache-aside pattern by default** — check cache, miss, load from DB, populate cache; never assume cache is populated
- 5. **Atomic operations prevent race conditions** — use pipelines and Lua scripts for multi-step operations, never read-then-write
- 6. **Separate Redis connections for pub/sub** — subscriber connections are blocked; use dedicated connections for subscriptions
- 7. **Namespace all keys** — prefix keys by domain (`lb:`, `session:`, `rl:`, `presence:`) to avoid collisions and enable selective flushing
-
- ## Step-by-Step Instructions
-
- ### 1. Install Dependencies
-
- ```bash
- bun add ioredis
- ```
-
- ### 2. Configure Redis Connection
-
- Use `boilerplate/redis-client.ts` for the base connection with reconnection logic and error handling. All other modules import from this file.
-
- ### 3. Set Up Leaderboards
-
- Use `boilerplate/leaderboard.ts` for sorted-set-based leaderboards. Supports adding scores, fetching top N, getting player rank, and fetching nearby ranks. Includes periodic snapshot to PostgreSQL for historical data.
-
- ### 4. Implement Session Caching
-
- Use `boilerplate/session-cache.ts` for player session caching with TTL. Follows cache-aside pattern: check cache → miss → load from DB → populate cache.
-
- ### 5. Configure Pub/Sub
-
- Use `boilerplate/pub-sub.ts` for cross-server messaging. Requires separate Redis connections for publishing and subscribing. Typed event handlers ensure type safety.
-
- ### 6. Add Rate Limiting
-
- Use `templates/rate-limiter.ts` for per-player per-action sliding window rate limiting using sorted sets. Configurable window size and max requests.
-
- ### 7. Manage Ephemeral State
-
- Use `templates/ephemeral-state.ts` for short-lived game state: match state with TTL, player presence via heartbeat, and temporary buffs/effects.
-
- ## Code Examples
-
- ### Adding a player score to a leaderboard
+ Redis holds hot, disposable game state. The database holds outcomes. If Redis
+ restarts empty, players should lose nothing that matters: match results,
+ rewards and currency are written to the database first.
- ```typescript
- import { leaderboard } from './leaderboard';
+ ## Client choice
- await leaderboard.addScore('weekly', 'player_123', 4500);
- const top10 = await leaderboard.getTopN('weekly', 10);
- const rank = await leaderboard.getPlayerRank('weekly', 'player_123');
- ```
+ The code here uses `ioredis` 6, because BullMQ 6 creates its Redis connections
+ with ioredis by default and most game servers in this bundle already run
+ BullMQ (`bullmq-game-queues`). The ioredis README says that for new projects
+ node-redis is the recommended client; if the project has no BullMQ and no
+ ioredis yet, consider node-redis and port the commands. BullMQ 6 marks ioredis
+ as an optional peer, so install it explicitly: `bun add ioredis`.
- ### Caching a player session
+ Every file takes the client as a parameter typed `Redis` from `ioredis`.
+ Create the client once in the server and pass it in. Keep the default
+ `replyMapping: "legacy"`: the parsers expect `WITHSCORES` replies as flat
+ `[member, score, ...]` string arrays. The code needs Redis 6.2 or newer
+ (`ZRANGE ... REV`).
- ```typescript
- import { sessionCache } from './session-cache';
+ ## Pattern 1: leaderboard
- const session = await sessionCache.get('player_123');
- if (!session) {
- const fromDb = await db.query.players.findFirst({ where: eq(players.id, 'player_123') });
- await sessionCache.set('player_123', fromDb, 3600);
- }
- ```
+ Copy `boilerplate/leaderboard.ts` when the game ranks players by score.
- ### Publishing a game event across servers
+ - One sorted set per board and period: `lb:weekly:2026-W46`, `lb:season:3`.
+ A new period is a new key; snapshot the old one, then delete it.
+ - `ZADD` overwrites the score. For "best score counts", decide on purpose:
+ `ZADD ... GT` keeps the higher value; `ZINCRBY` adds points.
+ - Ties: equal scores sort by member ID bytes (reverse order under `REV`), not
+ by who reached the score first. If first-to-score must win, encode the tie
+ break into the score, and keep the whole score inside the exact integer
+ range of a double (2^53).
+ - Rank from `ZREVRANK` is zero-based; the boilerplate returns rank 1 for the
+ top player.
+ - At season end, call `snapshot()` with a function that writes the rows to
+ Postgres (`postgres-game-schema`), then reset the board.
- ```typescript
- import { publisher, subscriber } from './pub-sub';
+ ## Pattern 2: presence and ephemeral state
- await publisher.publish('match-events', {
- type: 'match-started',
- matchId: 'match_456',
- players: ['player_123', 'player_789'],
- });
+ Copy `templates/ephemeral-state.ts` when the game shows who is online, keeps
+ live match state outside the database, or applies timed buffs.
- subscriber.on('match-events', (event) => {
- console.log('Match event:', event.type);
- });
- ```
+ - Presence: the client heartbeats every few seconds; the server writes
+ `presence:<playerId>` with `EX`. A missing key means offline, so a crashed
+ client goes offline without cleanup code. Set the TTL to a few heartbeat
+ intervals so one lost packet does not flash the player offline.
+ - Match state: one JSON key per match with a TTL longer than the longest
+ match. `update()` reads the JSON, applies your pure function and writes
+ the result with a compare-and-set Lua script: the write lands only if the
+ key still holds what was read, else it reads again and retries. Calls for
+ one match from the same process run one after another, so they never race
+ each other; the script catches races with other servers. Do not use
+ `WATCH`/`MULTI` here: concurrent updates on one shared client interleave
+ their `WATCH` and `EXEC` and lose writes.
+ - Timed buffs: one key per effect with `EX`, plus a per-player sorted set
+ scored by expiry time to list them. Expiry needs no sweeper job.
+ - `SET` clears an existing TTL unless you pass `EX` again. Every write in the
+ template passes it.
- ### Rate limiting a player action
+ ## Pattern 3: per-action rate limit
- ```typescript
- import { checkRateLimit } from './rate-limiter';
+ Copy `templates/rate-limiter.ts` when an action needs a cooldown or spam cap:
+ attacks, chat, trades, crafting.
- const result = await checkRateLimit('player_123', 'attack', {
- maxRequests: 1,
- windowSeconds: 5,
- });
+ - Key per player and action: `rl:<action>:<playerId>`. Limits differ per
+ action, so one global limit per player is the wrong shape.
+ - Sliding window on a sorted set: each attempt is a member scored by its
+ timestamp. The template runs remove-old, add, count and expire in one
+ `MULTI`, so two concurrent attempts cannot both pass.
+ - A denied attempt is removed again. Otherwise a player who spams the button
+ stays locked out past the window.
+ - Check the limit on the server for every action message, including actions
+ that arrive over WebSocket (`game-backend-architecture`).
- if (!result.allowed) {
- throw new Error(`Rate limited. Retry after ${result.retryAfter}s`);
- }
- ```
+ ## Pattern 4: cache-aside for player data
- See `boilerplate/` for full implementations and `templates/` for rate limiting and ephemeral state patterns.
+ No file; the code is short enough to write in place.
- ## Cross-References
+ 1. Read `player:<id>` from Redis. On a hit, parse and return it.
+ 2. On a miss, load from the database, write it with `SET key value EX ttl`,
+ and return it. A missing row stays uncached unless you cache a "not found"
+ marker with a short TTL.
+ 3. On every database write to that player, delete the cache key after the
+ write commits. Do not update the cache in place from game logic.
+ 4. If the cached JSON fails to parse, delete the key and fall through to the
+ database.
- - `game-backend-architecture` for integrating Redis into the game server
- - `bullmq-game-queues` for job queues built on Redis
- - `postgres-game-schema` for persisting leaderboard snapshots and session fallback data
+ Cache only what the game reads often and can tolerate seeing a few seconds
+ late (profile, loadout, cosmetics). Wallet balances and inventory counts are
+ read from the database inside the transaction that changes them.
- ## Pitfalls & Anti-Patterns
+ ## Pitfalls
- - **Don't use Redis as a primary database** — Redis is a cache and message broker; always have a persistence layer backing it
- - **Don't forget TTLs** — keys without expiration accumulate forever and eventually exhaust memory
- - **Don't use `KEYS *` in production** — it blocks Redis; use `SCAN` for key enumeration
- - **Don't share a single connection for pub/sub and commands** — subscriber connections are blocked and cannot execute other commands
- - **Don't store large objects in Redis** — keep values small (< 1MB); store IDs and fetch details from the database
- - **Don't assume cache is populated** — always implement cache-aside; never trust that a key exists
+ - Treating Redis as the record of truth for match results, rewards or
+ currency. Write those to the database; Redis may lose them.
+ - Read-then-write on shared keys from more than one server. Use a Lua
+ script (as `update()` does), a single command, or `MULTI`/`WATCH` on a
+ connection that no concurrent code shares; a plain pipeline does not stop
+ another client's command running between yours.
+ - Using Pub/Sub for events a server must not miss. Redis Pub/Sub delivers at
+ most once: a server that is down or disconnected loses the message. Send
+ match results and purchases through a queue (`bullmq-game-queues`).
+ - Subscribing on the shared command connection. Under RESP2 a subscribed
+ connection accepts only subscribe-family commands, and the ioredis README
+ says to create a separate connection for subscribing. Give subscribers their
+ own client (`redis.duplicate()`).
+ - Prefix Pub/Sub channels with the environment (`prod:match-events`). Pub/Sub
+ ignores database numbers, so staging and production on one Redis hear each
+ other.
- ## Designer Philosophy
+ ## Files
- **Will Wright** (SimCity, The Sims): Simulation state management is about speed and disposability. In SimCity, the game constantly recalculates traffic, power, and zoning — none of that intermediate state needs to survive a restart. Redis serves this exact role in multiplayer games: it holds the hot, fast-changing state that drives the simulation, while the database records outcomes that matter. The moment you treat cache as truth, you've confused process with product.
+ - `boilerplate/leaderboard.ts`: copy when building a ranked board; read when
+ checking how ranks and snapshots are computed.
+ - `templates/rate-limiter.ts`: copy when adding per-action limits.
+ - `templates/ephemeral-state.ts`: copy when adding presence, live match state
+ or timed buffs.
+ - `references/sources.md`: read when you need the Redis or ioredis page behind
+ a rule above.
- ## Sources
+ ## Related skills
- - [Redis Official Documentation](https://redis.io/docs/)
- - [Redis Data Types and Abstractions](https://redis.io/docs/data-types/)
- - [ioredis GitHub Repository](https://github.com/redis/ioredis)
- - [Redis Sorted Sets for Leaderboards](https://redis.io/docs/data-types/sorted-sets/)
- - [Redis Pub/Sub Documentation](https://redis.io/docs/interact/pubsub/)
- - [Redis Best Practices](https://redis.io/docs/management/optimization/)
+ - `bullmq-game-queues`: durable jobs and events on the same Redis.
+ - `matchmaking-system`: queue and lobby state on Redis.
+ - `gameplay-analytics`: event buffering on Redis.
+ - `postgres-game-schema`: where leaderboard snapshots and player data live.
+ - `game-backend-architecture`: where the client is created and passed in.