The Complete Guide to Redis¶
Redis is an in-memory data store. Ultra fast. Indispensable.
Basic Operations¶
SET key “value” EX 3600 # with expiration GET key DEL key EXISTS key TTL key
Data Structures¶
Strings¶
INCR counter INCRBY counter 5
Hash¶
HSET user:1 name “Jan” email “[email protected]” HGETALL user:1
List¶
LPUSH queue “task1” RPOP queue
Set¶
SADD tags “python” “devops” SMEMBERS tags
Sorted Set¶
ZADD leaderboard 100 “player1” 200 “player2” ZREVRANGE leaderboard 0 9 WITHSCORES
Use Cases¶
- Cache — the most common, SET/GET with TTL
- Sessions — user session storage
- Rate limiting — INCR + EXPIRE
- Queue — LPUSH + BRPOP
- Pub/Sub — real-time messaging
- Leaderboard — Sorted Sets
Persistence¶
- RDB — point-in-time snapshot
- AOF — append-only file (more durable)
- RDB + AOF — recommended for production
Cluster¶
Redis Cluster for horizontal scaling. Automatic sharding and failover.
Rule¶
Redis is a cache, not a primary database (with exceptions). Always have a fallback to primary storage.
rediscachedatabáze