System Design · Concept Library · Free article
What Is Redis, How Does It Work, and When Should You Use It?
Use shared in-memory data structures for low-latency reads and coordination, with explicit limits around durability, large commands, and cluster placement.
Redis is an in-memory data-structure server. Applications address values by key and choose structures such as strings, hashes, lists, sets, sorted sets, streams, and geospatial indexes. Redis commonly serves as a cache, counter, leaderboard, rate limiter, ephemeral coordination layer, or stream. Commands execute with atomic semantics, while pipelining reduces network round trips for independent commands. Persistence, replication, and Redis Cluster add recovery and scale, but acknowledged writes can still be lost in documented failure windows. Use Redis when low latency, shared state, and a known access pattern matter. Keep critical truth in a system whose durability contract matches the data.
What is Redis, and what is it for?
Redis is an in-memory data-structure server: you address a value by key and the value is a real structure, a string, hash, list, set, sorted set or stream, with commands that operate on it in place. Hatch first uses Redis as a shared cache: url:x7Gk2 points to a copied destination. The same server can hold a counter, hash, sorted set, or stream because Redis operations belong to the value type behind the key. That makes Redis more than a plain string cache.
The useful design move is to start from the operation. A string supports GET, SET, and atomic increments. A hash groups fields for one object. A sorted set maintains members by score. A stream records ordered entries for consumer processing. Choosing the structure fixes which commands are available and their complexity.
The interactive version of this example is available when JavaScript loads.
| Need | Structure | Typical operations |
|---|---|---|
| Cached response, token, or counter | String | GET, SET, INCR, expiry |
| Fields for one compact object | Hash | HGET, HSET, HINCRBY |
| Membership or set algebra | Set | SADD, SISMEMBER, intersections |
| Ranking by a score | Sorted set | ZADD, rank and range reads |
| Ordered work or event entries | Stream | XADD, consumer groups, acknowledgement |
One command is atomic; a workflow needs its own boundary
Redis serves commands through an event-driven execution model. Current Redis can use threads for network I/O and background tasks, while command execution retains the ordered semantics applications rely on. A single command such as INCR clicks:x7Gk2 completes atomically, so two clients do not both read 41 and write 42.
That guarantee does not automatically combine several client commands. A GET, local calculation, and SET can race with another client between calls. Use a command that already expresses the operation, a transaction with the required checks, or server-side scripting when the steps must execute as one atomic unit.
Pipelining batches independent commands
For small Redis operations, network round trips can dominate application time. Pipelining lets a client send several commands without waiting for each reply before sending the next. Redis still processes the commands and queues their replies, so large pipelines should be bounded.
Pipelining is not a transaction. It reduces waiting between client and server but does not make a read-dependent sequence atomic. Use a transaction or script when later work depends on earlier results under one consistency boundary.
What should you use Redis for?
Work that is keyed, small, and tolerant of loss: caching, rate limiting, counters, session state, leaderboards, queues and locks. What those share is an explicit access pattern and a failure contract you can accept, which is the pair worth checking before the feature list.
| Job | Why Redis fits | Boundary to defend |
|---|---|---|
| Application cache | Shared low-latency reads, expiry, and eviction | The source must survive misses; copies can be stale. |
| Counter or rate limiter | Atomic increment and expiry operations | Define the window algorithm and make multi-step updates atomic. |
| Leaderboard | Sorted sets maintain score order | Watch hot keys and collection size. |
| Ephemeral lock | SET conditions, expiry, and token-checked release | Failover and lease expiry can violate exclusive ownership. |
| Stream or work queue | Ordered entries, consumer groups, and acknowledgements | Choose a dedicated log when retention, replay, or scale requirements exceed Redis Streams. |
A Redis lock needs a token, expiry, and failure model
A basic Redis lock uses SET resource random-token NX PX ttl. Only one caller can create the key, and expiry prevents a dead owner from holding it forever. Release must compare the stored token with the caller's token and delete only on a match, normally in one script.
This controls ordinary contention but does not make every downstream action exactly once. A client can pause past its lease, or a failover can create a window in which another client acquires what it believes is the same lock. If stale work could corrupt data, use fencing tokens or enforce the final condition in the durable system that performs the write.
Is Redis durable?
Redis supports no persistence, point-in-time RDB snapshots, an append-only file, or both. RDB recovery can lose writes since the most recent snapshot. AOF loss depends on the configured flush policy; the common every-second policy can lose roughly the latest second during a failure. Stronger flush behavior costs performance.
Replication improves availability but Redis Cluster uses asynchronous replication and documents windows where acknowledged writes can be lost. Match the configuration to the consequence of loss. A rebuildable cache can accept a wider window. A payment balance cannot.
Redis Cluster maps every key to one of 16,384 slots
Redis Cluster computes a hash slot for each key with CRC16 modulo 16,384, then assigns slot ranges to primary nodes. Clients keep a slot-to-node map and follow MOVED replies when ownership changes. Adding capacity moves slots and their keys between nodes.
Multi-key commands, transactions, and scripts generally require all involved keys to share a slot. A hash tag inside braces makes Redis hash only that substring, so cart:{42}:items and cart:{42}:total land together. Co-location enables atomic multi-key work, but it can also concentrate traffic. Key naming is part of both correctness and load distribution.
The interactive version of this example is available when JavaScript loads.
When should you not use Redis?
When the data must survive a crash without question, when queries are ad hoc rather than by key, when the working set will not fit in memory, or when you need joins and multi-key transactions. Each of those asks Redis for the guarantee it deliberately does not offer.
- Do not make Redis the only copy of data whose accepted writes cannot be lost unless the chosen Redis product and configuration explicitly satisfy that durability requirement.
- Do not hold a dataset in memory when its size and access pattern make a disk-backed store more economical.
- Do not force flexible joins, ad hoc predicates, or broad analytical scans into a key-access model.
- Do not use a Redis lease as the only correctness boundary for irreversible downstream work.
- Do not choose Streams by habit when the workload requires a long-retention, independently replayable event log at a scale better served by a dedicated system.
Primary references
- Redis: data types
- Redis: pipelining
- Redis: persistence
- Redis: Cluster specification
- Redis: distributed locks
Pick Redis from the operation and the failure contract
By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.