Redis (Remote Dictionary Server) is an in-memory data structure store used as a database, cache, message broker, and streaming engine.
It is designed for extreme performance, low latency, and high throughput, often operating at sub-millisecond response times.
This article is a full Redis reference, from internal architecture to every major command and real-world usage pattern.
1. What is Redis?
Redis is:
- In-memory (RAM-based)
- Key-value store
- Single-threaded (core execution)
- Non-relational (NoSQL)
- Extremely fast
- Persistent (optional)
Common Use Cases
- Caching
- Session storage
- Real-time analytics
- Rate limiting
- Pub/Sub systems
- Leaderboards
- Queues
- Distributed locks
2. Redis Architecture
High-Level Architecture
Client
|
TCP Connection
|
Redis Server
|
In-Memory Data Structures (RAM)
|
Persistence (RDB / AOF)
Key Architectural Properties
- Single-threaded command execution
- Event-driven I/O (epoll/kqueue)
- In-memory first
- Optional disk persistence
- Replication and clustering support
3. Redis Internal Design
3.1 Event Loop (I/O Multiplexing)
Redis uses:
-
epoll(Linux) -
kqueue(macOS/BSD)
This allows Redis to:
- Handle thousands of concurrent connections
- Remain single-threaded
- Avoid context switching overhead
3.2 Memory Model
Redis stores data in:
- RAM
- Optimized internal encodings
Examples:
- Small integers → integer encoding
- Small strings → embedded in object
- Large strings → heap allocation
3.3 Redis Objects
Every key maps to a Redis Object:
redisObject {
type
encoding
refcount
ptr
}
Object types:
- String
- List
- Set
- Hash
- ZSet
- Stream
4. Redis Data Types (Complete)
4.1 Strings
Description
- Binary safe
- Max size: 512 MB
Commands
SET key value
GET key
DEL key
EXISTS key
INCR counter
DECR counter
APPEND key "text"
STRLEN key
With Expiration
SET token abc123 EX 60
TTL token
4.2 Lists (Linked Lists)
Description
- Ordered
- Allows duplicates
- Stack & Queue behavior
Commands
LPUSH mylist a b c
RPUSH mylist d
LPOP mylist
RPOP mylist
LRANGE mylist 0 -1
LLEN mylist
Blocking Operations (Queues)
BLPOP queue 0
BRPOP queue 0
4.3 Sets (Unordered, Unique)
Commands
SADD users alice bob charlie
SMEMBERS users
SISMEMBER users alice
SREM users bob
SCARD users
Set Operations
SUNION set1 set2
SINTER set1 set2
SDIFF set1 set2
4.4 Hashes (Key-Value inside Key)
Commands
HSET user:1 name "Ali" age 25
HGET user:1 name
HGETALL user:1
HDEL user:1 age
HEXISTS user:1 name
4.5 Sorted Sets (ZSET)
Description
- Unique members
- Ordered by score (double)
Commands
ZADD leaderboard 100 alice 200 bob
ZRANGE leaderboard 0 -1
ZREVRANGE leaderboard 0 -1
ZSCORE leaderboard alice
ZRANK leaderboard alice
4.6 Streams (Log-Based Data)
Commands
XADD logs * event login user ali
XRANGE logs - +
XREAD COUNT 2 STREAMS logs 0
Consumer Groups
XGROUP CREATE logs group1 0
XREADGROUP GROUP group1 consumer1 STREAMS logs >
5. Keys Management
KEYS *
SCAN 0
DEL key
EXPIRE key 60
PERSIST key
TTL key
TYPE key
⚠️ Avoid KEYS * in production.
6. Redis Persistence
6.1 RDB (Snapshotting)
- Periodic snapshots
- Fast restart
- Risk of data loss
Config:
save 900 1
save 300 10
Manual:
SAVE
BGSAVE
6.2 AOF (Append-Only File)
- Logs every write operation
- Safer than RDB
- Slower
Config:
appendonly yes
appendfsync everysec
Rewrite:
BGREWRITEAOF
6.3 Hybrid Persistence
- RDB + AOF combined
7. Redis Replication
Master → Replica
Master
|
Replica 1
Replica 2
Commands:
REPLICAOF host port
INFO replication
Properties:
- Asynchronous
- Read scaling
- Failover ready
8. Redis Sentinel (High Availability)
Purpose
- Monitor Redis
- Automatic failover
- Leader election
Components:
- Sentinel processes
- Master
- Replicas
9. Redis Cluster (Horizontal Scaling)
Features
- Sharding
- 16384 hash slots
- Automatic rebalancing
Hash slot calculation:
CRC16(key) % 16384
Cluster commands:
CLUSTER INFO
CLUSTER NODES
10. Transactions in Redis
MULTI / EXEC
MULTI
SET a 10
INCR a
EXEC
DISCARD
DISCARD
Optimistic Locking (WATCH)
WATCH balance
MULTI
DECR balance
EXEC
11. Lua Scripting
Purpose
- Atomic operations
- Complex logic
Example:
EVAL "return redis.call('GET', KEYS[1])" 1 mykey
Lua guarantees:
- Atomic execution
- No race conditions
12. Pub/Sub Messaging
Publisher
PUBLISH news "Hello World"
Subscriber
SUBSCRIBE news
Properties:
- Fire-and-forget
- No persistence
13. Security in Redis
Authentication
AUTH password
Config:
requirepass strongpassword
ACL (Redis 6+)
ACL SETUSER ali on >password +get +set
14. Performance Characteristics
- O(1) average operations
- Millions of ops/sec
- Single-threaded = predictable latency
Optimization Tips
- Avoid large values
- Use pipelines
- Use SCAN instead of KEYS
- Set TTLs aggressively
15. Redis as Cache (Real Example)
Node.js Example
import Redis from "ioredis";
const redis = new Redis();
await redis.set("user:1", JSON.stringify(user), "EX", 60);
const cached = await redis.get("user:1");
16. Redis as Rate Limiter
INCR ip:1
EXPIRE ip:1 60
17. Redis as Queue
LPUSH jobs task1
BRPOP jobs 0
18. Redis vs Other Databases
| Feature | Redis | MongoDB | PostgreSQL |
|---|---|---|---|
| In-Memory | Yes | No | No |
| Speed | Ultra Fast | Fast | Moderate |
| Transactions | Limited | Yes | Full ACID |
| Schema | None | Flexible | Strict |
19. When NOT to Use Redis
- Large datasets exceeding RAM
- Complex relational queries
- Strong ACID guarantees required
- Long-term data storage
20. Final Summary
Redis is:
- A high-performance in-memory database
- Ideal for real-time systems
- Powerful when combined with SQL or NoSQL databases
- Simple API, deep internals
Mastering Redis gives you:
- Scalable systems
- Faster applications
- Production-grade architecture skills
Top comments (0)