A beginner-to-intermediate guide to Redis.
Redis (REmote DIctionary Server) is an in-memory data structure store used as a database, cache, and message broker.
# Ubuntu / Debian
sudo apt install redis-server
# macOS (Homebrew)
brew install redis
# Start Redis
redis-server
# Redis CLI
redis-cli
| Data Type | Description |
|---|---|
| String | Text or binary data |
| List | Ordered collection of strings |
| Set | Unordered unique values |
| Hash | Key-value pairs inside a key |
| Sorted Set | Ordered by score |
| Stream | Log-like data structure |
SET username "alice"
GET username
INCR page_views
APPEND username "_123"
LPUSH tasks "task1"
LPUSH tasks "task2"
LRANGE tasks 0 -1
RPOP tasks
SADD tags "redis" "database" "cache"
SMEMBERS tags
SISMEMBER tags "redis"
HSET user:1 name "Alice" age 25
HGET user:1 name
HGETALL user:1
ZADD leaderboard 100 "Alice"
ZADD leaderboard 200 "Bob"
ZRANGE leaderboard 0 -1 WITHSCORES
SET session:123 "active"
EXPIRE session:123 60
TTL session:123
# Subscriber
SUBSCRIBE news
# Publisher
PUBLISH news "Hello Redis!"
MULTI
INCR balance
DECR balance
EXEC
SAVE
BGSAVE
CONFIG SET appendonly yes