Redis basics: understand data types, persistence, and replication
Redis is more than a cache that stores strings by key: it is a data structure server with commands for strings, hashes, lists, sets, sorted sets, and streams. These structures support fast key-based work such as caches, counters, queues, leaderboards, and event processing. To use Redis safely, plan memory limits, key expiration and eviction, disk persistence, and the data-loss window of asynchronous replication.
Redis is more than a cache that stores strings by key: it is a data structure server with commands for strings, hashes, lists, sets, sorted sets, and streams. These structures support fast key-based work such as caches, counters, queues, leaderboards, and event processing. To use Redis safely, plan memory limits, key expiration and eviction, disk persistence, and the data-loss window of asynchronous replication.
This guide describes self-managed Redis Open Source concepts from official documentation checked on November 23, 2026. Managed Redis Cloud and Redis Software procedures and product-specific limits are out of scope. Available types and operations vary by Redis version and deployment.
What does Redis store and how do clients use it?
The basic unit is a key and value, but the value is not limited to an opaque string. Type-specific commands make different operations natural: strings for general values and counters; hashes for field/value records; lists for ordered sequences; sets for unique members and set operations; sorted sets for scored ranking and range queries; and streams for event logs and consumer groups. Redis data types · Strings · Hashes · Lists · Sets · Sorted sets · Streams
Clients send Redis commands for keys and types. A single JSON string can hold a product record, but a hash or Redis JSON feature may fit better if the application often reads and updates individual fields. Choose a type from the operations you need, not just the shape of the data.
Client commands: SET / HSET / ZADD / XADD
│
▼
Redis key space: key → string, hash, list, set, sorted set, or streamThis is a simplified conceptual view, not a memory-layout or cluster diagram.
Which data type fits a common task?
| Need | Consider | Trade-off |
|---|---|---|
| Cache value with TTL or simple counter | String | SET can overwrite; use a conditional option when creation-only behavior is required |
| Read or update selected profile fields | Hash | Field-level operations; very large or highly variable hashes can have memory costs |
| Push/pop work at either end | List | Ordered queue-like operations; use streams when consumer tracking and retained events matter |
| Unique membership or intersections | Set | Fast membership and set operations; large sets use memory |
| Rank members by score | Sorted set | Stores member/score for ranking and score ranges |
| Append events and consume in groups | Stream | Event-log and consumer-group features; retention and retry policy remain design decisions |
For example, a shop might cache a product price with a string and TTL, update only a user's display name in a hash, and rank popular products with a sorted set. This is a hypothetical illustration, not a performance measurement.
How do expiration and eviction differ?
Expiration (TTL) is a lifetime rule for a key: the key stops being valid after its time limit. It fits cache entries that can be rebuilt from a source of truth. Keys without expiration can remain until explicitly deleted or affected by a memory policy, so define retention for sessions, cache entries, and temporary results. Redis string commands can set a value and an expiration together. String commands
Eviction is the server policy for removing keys when memory reaches its configured limit. Removing expired keys and evicting still-valid keys are different. For a cache, choose whether a policy such as LRU/LFU or an expiration-focused policy fits, and decide what writes should do when nothing can be evicted. If irreplaceable application state shares a cache eviction policy, it may disappear unexpectedly. Match the policy to whether the data can be regenerated. Key eviction
Can an in-memory Redis deployment persist data?
Redis Open Source can write state to disk. RDB makes point-in-time snapshots; its compact single-file form can help with backup and transfer. AOF records write operations and replays them during startup. You can disable persistence or combine RDB and AOF. These choices trade recovery point, disk activity, file size, and recovery work; enabling persistence alone does not prove a backup can be restored. Choose based on how much data loss is acceptable and test the actual restore procedure. Redis persistence
Are replication and Redis Cluster the same?
Base replication copies a leader's changes to one or more replicas. Redis Open Source replication is asynchronous by default: the leader does not wait for every replica before acknowledging each write. This keeps latency low, but a recent write that has not reached a replica can be lost during failure. Replicas therefore do not imply strong consistency or zero data loss. Redis replication
Redis Cluster shards keys across nodes using hash slots and can replicate each shard for availability. A multi-key command, transaction, or script requires all its keys to be in the same slot. Hash tags such as {42} in key names can co-locate related keys, so key naming should account for multi-key operations. Cluster behavior during partitions and its operational requirements must be understood; it does not guarantee that every failure preserves every write. Redis Cluster scaling
When should Redis be a cache or a primary store?
As a cache, Redis is suitable for copies that can be rebuilt from an authoritative source. Set clear TTLs and provide a path to reload missing or evicted values. If Redis is the only copy of sessions, job state, or leaderboard data, evaluate persistence, backup, recovery point, replica lag, and failover against your loss tolerance. Neither “Redis is in memory, so it is volatile” nor “it is replicated, so it is safe” is a universal rule; the guarantees depend on product, version, configuration, and hosting environment.
Model the needed operations with a small set of types, then define key names, TTLs, memory limits, eviction behavior, restore method, replication mode, and whether Cluster's multi-key constraint fits. Document what can be regenerated and which write losses are acceptable. The following commands are illustrative and were not run or benchmarked:
SET cache:product:42 '{"name":"Mug","price":12.50}' EX 300
GET cache:product:42
HSET user:17 name "Mina" plan "basic"
HGET user:17 name
ZADD leaderboard 1250 user:17
ZRANGE leaderboard 0 9 REV WITHSCORESEX 300 gives that string key a 300-second TTL. The application must be able to rebuild the cached value. Hash and sorted-set commands operate on different types, so use consistent key naming and handle wrong-type errors.
Scope and official sources
The official Redis pages below were checked on November 23, 2026. Detailed stream consumer-group procedures are covered elsewhere; this overview only identifies the stream's role. Other Redis distributions may differ in persistence, replication, limits, and operations.
[Redis data types](https://redis.io/docs/latest/develop/data-types/)
[Redis strings](https://redis.io/docs/latest/develop/data-types/strings/)
[Redis hashes](https://redis.io/docs/latest/develop/data-types/hashes/)
[Redis lists](https://redis.io/docs/latest/develop/data-types/lists/)
[Redis sets](https://redis.io/docs/latest/develop/data-types/sets/)
[Redis sorted sets](https://redis.io/docs/latest/develop/data-types/sorted-sets/)
[Redis Streams](https://redis.io/docs/latest/develop/data-types/streams/)
[Redis persistence](https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/)
[Redis replication](https://redis.io/docs/latest/operate/oss_and_stack/management/replication/)
[Redis Cluster and scaling](https://redis.io/docs/latest/operate/oss_and_stack/management/scaling/)
[Key eviction](https://redis.io/docs/latest/develop/reference/eviction/)