Last modified: March 23, 2026
This article is written in: 🇺🇸
Redis is an open-source, in-memory data store that serves as a high-performance cache, message broker, and general-purpose database. It is often called a "data structure server" because it natively supports rich types like strings, lists, sets, sorted sets, and hashes. Because every operation happens in memory, Redis achieves sub-millisecond latency for both reads and writes, making it one of the most widely adopted caching layers in modern backend architectures. Understanding how to leverage Redis effectively can dramatically improve the performance, scalability, and responsiveness of your applications.
+-----------+ +-----------+ +-----------+
| Client A | | Client B | | Client C |
| (Web App) | | (Worker) | | (CLI) |
+-----+-----+ +-----+-----+ +-----+-----+
| | |
| SET/GET | LPUSH/BRPOP | PUBLISH
| | |
+----------+----------+----------+----------+
| |
v v
+-------------------------------------------+
| Redis Server (Single) |
|-------------------------------------------|
| Engine: Single-threaded event loop |
| Storage: In-memory key-value store |
| Features: |
| - Data Structures (String, List, Set...) |
| - Pub/Sub messaging |
| - Lua scripting |
| - Transactions (MULTI/EXEC) |
| - TTL-based key expiration |
+---------------------+---------------------+
|
(Optional persistence)
|
+---------------+---------------+
| |
v v
+-----------+ +-----------+
| RDB Dump | | AOF Log |
| (Snapshot) | | (Append) |
+-----------+ +-----------+
user:1001:profile) keeps the keyspace organized and easy to scan.SET key value EX 60 or add it later with the EXPIRE command.Redis supports several first-class data structures, each optimized for different access patterns.
| Redis Data Structures |
+--------------------------------------------------------------+
| |
| STRING LIST SET SORTED SET |
| +------+ +---+---+---+ +---+---+---+ +-----+-----+ |
| | "Hi" | | A | B | C | | X | Y | Z | |a:1.0|b:2.5| |
| +------+ +---+---+---+ +---+---+---+ +-----+-----+ |
| |
| HASH STREAM |
| +--------+---------+ +-----+-----+-----+ |
| | field1 | value1 | | ID1 | ID2 | ID3 | |
| | field2 | value2 | +-----+-----+-----+ |
| +--------+---------+ |
+--------------------------------------------------------------+
INCR and DECR make strings ideal for counters, rate limiters, and simple flags.MGET and MSET commands let you read or write multiple string keys in a single roundtrip, reducing network overhead.LPUSH, RPUSH, LPOP, and RPOP make lists a natural fit for queues and task pipelines.BRPOP allow workers to wait for new items without polling, enabling efficient consumer patterns.SUNION, SINTER, and SDIFF let you compute relationships between groups server-side.ZRANGEBYSCORE command retrieves members within a score range, which is useful for time-series data or ranked feeds.HGET and HSET without fetching the entire structure, saving bandwidth.By default Redis is an in-memory store, but it offers two durability mechanisms that can be used independently or together.
Redis Server
+----------------+
| In-Memory |
| Dataset |
+-------+--------+
|
+----------------+----------------+
| |
(Point-in-time (Every write
snapshot) appended)
| |
v v
+---------------+ +----------------+
| RDB File | | AOF File |
| (binary dump) | | (command log) |
+---------------+ +----------------+
| - Compact | | - Durable |
| - Fast load | | - Human-readable|
| - Data loss | | - Larger size |
| between | | - Rewritable |
| snapshots | | via BGREWRITE|
+---------------+ +----------------+
appendfsync directive controls how often the OS flushes the log to disk: always, everysec, or no.BGREWRITEAOF to compact it by replaying only the commands needed to reconstruct the current state.| Primary |
| (Read + Write) |
+--------+--------+
|
+--------------+--------------+
| async replication | async replication
v v
+-----------------+ +-----------------+
| Replica 1 | | Replica 2 |
| (Read Only) | | (Read Only) |
+-----------------+ +-----------------+
MOVED or ASK responses when a key lives on a different node, and smart client libraries handle this transparently.+-------------------+ +-------------------+ +-------------------+
| Node A (Primary) | | Node B (Primary) | | Node C (Primary) |
| Slots 0-5460 | | Slots 5461-10922 | | Slots 10923-16383 |
+---------+---------+ +---------+---------+ +---------+---------+
| | |
v v v
+-------------------+ +-------------------+ +-------------------+
| Node A' (Replica) | | Node B' (Replica) | | Node C' (Replica) |
+-------------------+ +-------------------+ +-------------------+
+-----------+ +------------------+ +--------------+
| Publisher | PUBLISH | Redis Channel | message | Subscriber A |
| (Service) +---------->| "notifications" +--------->| (Web Socket) |
+-----------+ +--------+---------+ +--------------+
|
| message
v
+--------------+
| Subscriber B |
| (Worker) |
+--------------+
product:8842:details) for clarity.INCR with a TTL-bound key to count requests per client and reject traffic that exceeds the threshold.ZREVRANGE returns the top-ranked members instantly.
| Feature | Redis | Memcached |
| Data structures | Strings, lists, sets, hashes, streams | Strings only |
| Persistence | RDB snapshots, AOF log | None (pure cache) |
| Replication | Built-in primary-replica | Not built-in |
| Clustering | Redis Cluster (hash slots) | Client-side consistent hashing |
| Pub/Sub | Native support | Not supported |
| Scripting | Lua scripting on the server | Not supported |
| Threading model | Single-threaded event loop | Multi-threaded |
| Max value size | 512 MB | 1 MB (default) |
| Memory efficiency | Moderate (structural overhead) | High (slab allocator) |
| Use case | Cache, message broker, data store | Simple high-throughput caching |
To interact with Redis from your backend code, you need a Redis client library. Popular options include:
redis.asyncio.Choose a client library that best fits your programming language, concurrency model, and deployment requirements.
INFO command returns detailed statistics about memory usage, connected clients, keyspace hits and misses, and replication status.SLOWLOG GET surfaces commands that exceeded the configured threshold, helping you find and optimize expensive operations.BGSAVE or BGREWRITEAOF operations during off-peak hours to reduce the impact of persistence I/O on live traffic.requirepass directive and use ACLs (introduced in Redis 6) to grant fine-grained per-user permissions.FLUSHALL and CONFIG in production by renaming them in the configuration file.