Redis Patterns for Coding Agents
Redis Patterns for Coding Agents
Comprehensive Redis design patterns, best practices, and command references.<br>This site is optimized for both human developers and LLM coding agents.
Important: These documents are specific to Redis<br>(redis.io) and may not apply to other systems that share<br>parts of the codebase, such as Valkey, KeyDB, Dragonfly, or other Redis-compatible databases.<br>When working with forks or alternatives, verify compatibility as implementations may differ.
How to Use This Documentation
For Humans: Browse the sections below and click on any pattern to read the documentation.
For LLM Coding Agents: Start by fetching the machine-readable index:
llms.txt — Lists all available documentation files with descriptions
Then fetch individual .md files as needed. All pattern documents are written in Markdown with Redis commands shown as indented code blocks.
For Command Reference: The official Redis documentation is mirrored locally:
commands-index.md — Auto-generated index of all commands by category
commands/content/commands/ — Individual command docs (e.g., set.md, hset.md)
Official Resources:
• Source Code: github.com/redis/redis
• Documentation: github.com/redis/docs → redis.io/docs
Commands Reference
Browse Redis commands organized by category:
Commands Index: (html) (markdown) — Auto-generated index of commands by category
Command Documentation: commands/content/commands/ — Individual command files
Full Documentation: commands/content/ — develop/, integrate/, operate/ guides
This is a mirror of github.com/redis/docs.
Fundamental Design Patterns
Core architectural patterns for building systems with Redis.
Atomic Update Patterns<br>(html) (markdown)
Ensure data integrity with atomic read-modify-write operations using WATCH/MULTI/EXEC for optimistic locking, Lua scripts for complex logic, and shadow-key patterns for safe bulk updates.
Cache-Aside Pattern (Lazy Loading)<br>(html) (markdown)
Use Cache-Aside for read-heavy workloads: on cache miss, fetch from the database and populate the cache; on write, invalidate or update the cache explicitly.
Cache Stampede Prevention<br>(html) (markdown)
Prevent multiple clients from simultaneously regenerating an expired cache key using locking, probabilistic early refresh, or request coalescing.
Server-Assisted Client-Side Caching<br>(html) (markdown)
Eliminate network round-trips for frequently accessed keys by caching values in application memory, with Redis 6+ automatically sending invalidation messages when data changes.
Cross-Shard Consistency Patterns<br>(html) (markdown)
Detect and handle torn writes across multiple Redis instances using transaction stamps, version tokens, and commit markers when atomic multi-key operations aren't possible.
Delayed Queue Pattern<br>(html) (markdown)
Schedule tasks for future execution using a Sorted Set where the score is the Unix timestamp when the task should run.
Distributed Locking with Redis<br>(html) (markdown)
Implement mutual exclusion across distributed processes using `SET key value NX PX timeout` for atomic lock acquisition with automatic expiration.
Hash Tag Co-location Patterns<br>(html) (markdown)
Force related keys to the same Redis Cluster slot using hash tags, enabling atomic multi-key operations, transactions, and Lua scripts across logically related data.
Lexicographic Sorted Set Patterns<br>(html) (markdown)
Use Sorted Sets with identical scores (typically 0) to create B-tree-like indexes supporting prefix queries, range scans, and composite key lookups on string data.
Memory Optimization Patterns<br>(html) (markdown)
Reduce Redis memory consumption by leveraging compact encodings (listpack, intset), using Hashes for small object storage, and choosing memory-efficient data structures.
Probabilistic Data Structures<br>(html) (markdown)
Count unique items with HyperLogLog (12KB fixed), test set membership with Bloom filters, or estimate frequencies with Count-Min Sketch—trading small accuracy loss for massive memory savings.
Rate Limiting Patterns<br>(html) (markdown)
Implement distributed rate limiting using fixed window, sliding window log, sliding window counter, token bucket, or leaky bucket algorithms with Redis atomic operations.
Redis as a Primary Database<br>(html) (markdown)
Use Redis as the authoritative data store for applications requiring sub-millisecond latency and high write throughput, treating disk as a recovery mechanism rather than the primary storage layer.
The Redlock Algorithm<br>(html) (markdown)
Achieve fault-tolerant distributed locking by acquiring locks on a majority (N/2+1) of N independent Redis instances, tolerating node failures without losing lock consistency.
Reliable Queue Pattern<br>(html) (markdown)
Guarantee at-least-once message delivery using LMOVE to atomically transfer messages to a processing list, enabling recovery if consumers crash before completing work.
Streams Consumer Group Patterns<br>(html) (markdown)
Implement...