redis-patterns

Implements Redis caching, rate limiting, distributed locks, and streams with atomic Lua-based patterns.

Updated Aug 3, 2026
One-click install
npx skills add https://github.com/m-de-graaff/skills --skill redis-patterns-m-de-graaff
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: redis-patterns
Source: https://github.com/m-de-graaff/skills/tree/main/skills/redis-patterns
Command: npx skills add https://github.com/m-de-graaff/skills --skill redis-patterns-m-de-graaff

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Redis bugs almost always come from non-atomic read-then-write sequences, missing TTLs, stale caches, and locks released by the wrong process. This Skill provides correct, production-tested patterns for the most common Redis use cases so you avoid race conditions, stampedes, and silent data loss. ## Core Features & Use Cases - Data structure selection: Maps use cases (sessions, leaderboards, feeds, counters) to the right Redis structure with naming conventions. - Caching patterns: Cache-aside, write-through, tag-based invalidation, and stampede protection via locks or probabilistic early expiry. - Rate limiting: Fixed-window and atomic sliding-window implementations in Lua, including the boundary-double-spend flaw. - Distributed locks: Token-based acquire/release with Lua check-and-delete, TTL sizing, and idempotency guidance. - Streams vs pub/sub: Consumer groups, acknowledgement, and pending-entry recovery for at-least-once delivery. - Use Case: You are adding a rate limiter to an API. The Skill gives you the atomic sliding-window Lua script and explains why the naive INCR/EXPIRE approach lets clients exceed the limit at window boundaries. ## Quick Start Ask the AI to implement a Redis sliding-window rate limiter for 100 requests per minute per user using an atomic Lua script.

Frequently Asked Questions about redis-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a rate limiter with Redis?▼

Use a sliding-window rate limiter in Lua: trim expired entries with ZREMRANGEBYSCORE, check ZCARD against the limit, then ZADD the request. Running it as a script makes the read-decide-write atomic, unlike the fixed-window INCR/EXPIRE approach which allows 2x the limit across window boundaries.

How do I safely release a Redis distributed lock?▼

Acquire the lock with SET NX PX and a unique token, then release via a Lua script that deletes the key only if the stored value matches your token. A plain DEL can remove another process's lock after your TTL expired, letting two workers into the critical section.

Redis pub/sub vs streams: which should I use?▼

Use pub/sub only for disposable fan-out like presence or cache-invalidation hints, since disconnected subscribers permanently miss messages. Use streams with consumer groups when delivery matters: they provide at-least-once delivery, acknowledgement, replay, and pending-entry recovery via XAUTOCLAIM.

Why is my Redis cache returning stale data?▼

Stale reads happen when a write path updates the database without invalidating the cache key. Every write path touching an entity must delete its cache key; the TTL is only a backstop. Tag-based invalidation with a Set of related keys handles grouped expiry.

What Redis eviction policy should I use for caching?▼

Use allkeys-lru for a general-purpose cache, volatile-lru when mixing cached and durable data, and noeviction for queues or locks you cannot lose. Never share one instance between a cache and a queue under allkeys-lru, since eviction can silently delete queued jobs.

Why should I avoid KEYS in production Redis?▼

KEYS is O(N) and blocks the server for the entire scan, stalling all other clients. Use SCAN with a cursor instead, which iterates incrementally without blocking. Consistent key naming like namespace:resource:id makes SCAN patterns practical.