Introduction to Redis
What Redis is, its use cases, and architecture
Skills taught: redis.basics
What is Redis?
Redis (Remote Dictionary Server) is an open-source, in-memory data structure store. Because it stores data in RAM rather than on disk, it delivers sub-millisecond response times and can handle millions of operations per second on a single node.
- In-memory
- Data is stored in RAM rather than on disk. Access is nanoseconds instead of milliseconds, but data may be lost on restart unless persistence is configured.
- Key-Value Store
- Every piece of data is stored and retrieved by a unique string key. The value can be a string, list, set, hash, sorted set, stream, and more.
Installing and Connecting
# macOS
brew install redis
brew services start redis
# Ubuntu / Debian
sudo apt update && sudo apt install redis-server -y
sudo systemctl start redis
# Docker (easiest for local dev)
docker run -d --name redis -p 6379:6379 redis:7-alpine
# Connect with redis-cli
redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET hello world
OK
127.0.0.1:6379> GET hello
"world"
Why Redis is Fast
Redis achieves its speed from three design decisions: data lives in RAM (no disk I/O), Redis is single-threaded (no lock contention), and it uses a non-blocking I/O event loop (thousands of concurrent connections without threads).
Redis vs a SQL Database for Caching
- Redis: In-memory, microsecond latency
- PostgreSQL: Disk-based, millisecond latency
Common Use Cases
- Caching — Store database query results or API responses with a TTL to reduce load
- Session storage — Keep user sessions in Redis instead of a database for fast auth
- Rate limiting — Use atomic counters to limit requests per user per second
- Leaderboards — Sorted sets maintain ranked lists automatically
- Job queues — Lists support blocking push/pop for background workers
- Pub/Sub — Broadcast events to multiple subscribers in real time
- Distributed locks — SET NX EX implements a lightweight mutex
Tip: Redis is single-threaded for command execution. This eliminates race conditions on data structures without needing locks — a INCR is always atomic.
Warning: Redis stores everything in RAM. Without persistence configured, a restart means data loss. Enable RDB snapshots or AOF before going to production.
Key Naming Conventions
Redis keys are just strings, but a good naming convention prevents collisions and makes debugging easy. Use colon-separated namespaces: object:id:field.
# Good key naming patterns
user:1001:profile # user object
user:1001:sessions # list of session IDs
session:abc123 # session data
rate:api:192.168.1.1 # rate limit counter
cache:product:42 # cached product
leaderboard:weekly # sorted set
# Avoid:
user1001profile # no separator
USER:1001 # inconsistent case
Frequently Asked Questions
Can Redis persist data?
Yes. Redis supports RDB (periodic snapshots to disk) and AOF (append-only log of every write). The recommended setup is hybrid: AOF with RDB preamble.
How much data can Redis hold?
Limited only by available RAM. A single Redis instance can hold tens of GBs. Use Redis Cluster for datasets that exceed a single machine's memory.
Is Redis open source?
Redis was open source under BSD until 2024. Redis 7.4+ uses a dual license (RSALv2 / SSPLv1). Valkey is the community fork that remains BSD-licensed.