Redis Tutorial
Learn Redis step by step: 42 lessons in 10 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Redis? Learn what Redis is, why it is so fast, how its single-threaded command loop works, what it is used for, and how it differs from databases and Memcached.
- Installing Redis and Using redis-cli Install Redis with Docker, Linux, macOS or WSL, then learn redis-cli: PING, key naming, SELECT databases, INFO, MONITOR and the RESP wire protocol.
Core Data Types
- Strings and Counters Learn Redis strings: SET options (NX, XX, EX, GET), APPEND, GETRANGE, atomic INCR counters, MSET and MGET, binary safety and size limits, with live examples.
- Keys, Expiry and TTL Learn Redis key expiry: EXPIRE, TTL, PERSIST, lazy and active expiry, SCAN versus KEYS, TYPE, RENAME, DEL versus UNLINK and key naming conventions.
- Lists Redis lists: LPUSH and RPUSH, LPOP and RPOP, LRANGE, LTRIM, LINSERT, blocking BLPOP, LMOVE, capped lists for recent items and the O(N) pitfalls.
- Hashes Learn Redis hashes: HSET, HGET, HGETALL, HINCRBY, HEXISTS, HSCAN, field-level expiry (HEXPIRE), hashes versus JSON strings, and why small hashes save memory.
- Sets Learn Redis sets: SADD, SREM, SISMEMBER, SINTER, SUNION, SDIFF, SPOP, SRANDMEMBER and SSCAN, with tags and unique visitors, using a live emulator.
- Sorted Sets Learn Redis sorted sets: ZADD, ZRANGE by index, score and lex, ZRANK, ZINCRBY, ZUNIONSTORE, leaderboards and the skip list behind them, with live demos.
- Streams Learn Redis Streams: XADD and entry IDs, XRANGE, XREAD, consumer groups with XREADGROUP and XACK, pending entries, XTRIM, and streams vs lists and pub/sub.
Specialized Data Types
- Bitmaps and Bitfields Learn Redis bitmaps and bitfields: SETBIT, GETBIT, BITCOUNT, BITPOS, BITOP and BITFIELD for daily active users and packed counters, with a live emulator.
- HyperLogLog and Probabilistic Structures Learn Redis HyperLogLog (PFADD, PFCOUNT, PFMERGE), Bloom filters and Count-Min Sketch: approximate counting, error rates and memory use, with live JavaScript sketches.
- Geospatial Indexes Learn Redis geospatial indexes: GEOADD, GEOPOS, GEODIST, GEOHASH and GEOSEARCH, how geohash scores live in sorted sets, and nearby search, with live demos.
- Choosing the Right Data Type Learn to model data in Redis by access pattern: pick between strings, hashes, lists, sets and sorted sets, design keys, and avoid KEYS and huge keys.
Working with Redis
- Transactions: MULTI, EXEC and WATCH Learn Redis transactions: MULTI, EXEC and DISCARD, why there is no rollback, how queue errors differ from run errors, and WATCH check-and-set locking.
- Pipelining and Pub/Sub Learn Redis pipelining to cut round trips, and Pub/Sub with PUBLISH, SUBSCRIBE and PSUBSCRIBE, including at-most-once delivery and when to use streams.
- Lua Scripting and Functions Learn Redis Lua scripting: EVAL and EVALSHA, KEYS versus ARGV, atomic execution, SCRIPT LOAD, Redis Functions with FCALL, and common script pitfalls.
- Using Redis from Node.js, Python, PHP and Java Connect to Redis from Node.js, Python, PHP and Java: popular drivers, connection pooling, timeouts, retries, error handling and value serialization.
- Redis with Spring Boot, RedisTemplate and @Cacheable Use Redis from Spring Boot: Spring Data Redis, RedisTemplate and serializers, @Cacheable, @CachePut and @CacheEvict, cache TTLs, @RedisHash repositories and pub/sub.
- Redis Caching in Django and Flask Cache with Redis in Django and Flask: the CACHES setting, per-view and low-level cache API, sessions in Redis, Flask-Caching and hand-written cache-aside with invalidation.
Caching and Application Patterns
- Caching Patterns Learn Redis caching patterns: cache-aside, read-through, write-through and write-behind, how to choose TTLs, and how to design versioned cache keys.
- Cache Invalidation and Stampedes Learn how to invalidate Redis caches safely and stop cache stampedes with locks, early refresh, stale-while-revalidate, negative caching and hot-key tactics.
- Memory Limits and Eviction Learn how Redis maxmemory works, what each eviction policy does (LRU, LFU, volatile, allkeys, noeviction), approximated LRU, MEMORY USAGE and encodings.
- Sessions and Rate Limiting Store user sessions in Redis hashes with TTLs and build rate limiters: fixed window, sliding window log, token bucket and leaky bucket, made atomic with Lua.
- Queues, Distributed Locks and Leaderboards Build a reliable Redis queue with LMOVE, delayed jobs with sorted sets, safe SET NX PX locks with fencing tokens, and real-time leaderboards, with demos.
- Redis Stack: JSON, Search and Vectors Store documents with Redis JSON, query them with secondary indexes and full-text search, and use vector similarity for AI caching. Includes runnable stand-ins.
- Redis in System Design, Distributed Cache and Session Store Design with Redis: a distributed cache layer, multi-level caching with a near cache, a central session store, and back-of-the-envelope capacity and throughput estimates.
Persistence and Durability
High Availability and Scaling
- Replication Learn Redis replication: leader and replicas, REPLICAOF, full and partial resync, the replication backlog, stale reads on replicas and the WAIT command, with demos.
- High Availability with Sentinel Learn Redis Sentinel: monitoring, SDOWN and ODOWN, quorum versus majority, automatic failover, how clients find the master and how split-brain is limited.
- Redis Cluster and Sharding Learn Redis Cluster: 16384 hash slots, CRC16 key hashing, hash tags, MOVED and ASK redirects, multi-key limits, resharding and client-side sharding.
Operations, Security and Performance
- Securing Redis Secure Redis the right way: bind and protected mode, ACL users and permissions, requirepass, TLS, dangerous commands, exposed-server incidents and safe defaults.
- Performance and Latency Understand Redis speed: the single-threaded command loop, I/O threads, slow commands, SLOWLOG and LATENCY, big and hot keys, compact encodings and redis-benchmark.
- Monitoring and Troubleshooting Monitor Redis with INFO: hit rate, evictions, connected clients, memory fragmentation, replication lag, CLIENT LIST, OOM and other errors, plus Prometheus.
- Running Redis in Production Run Redis in production: Docker Compose and Kubernetes basics, managed services, Linux settings, backups, rolling upgrades, capacity planning and Valkey.
Projects
- Project: URL Shortener with Click Stats Build a URL shortener on Redis: INCR ids, expiring links with SET NX EX, hashes for stats, HyperLogLog unique visitors and a sorted-set top list.
- Project: Chat Rooms with Presence Build chat rooms on Redis: pub/sub for live messages, streams for history, and sets plus expiring heartbeat keys for who is online, with live demos.
- Project: A Reliable Job Queue Build a job queue on Redis with producers and workers, delayed jobs, retries with exponential backoff, a visibility timeout and a dead-letter list.
- Project: Cached API with Rate Limiting Put Redis in front of a slow API: cache-aside with TTL, a lock that stops cache stampedes, a per-user token bucket, and hit-rate metrics, with live demos.
- Project: Game Leaderboard Service Build a leaderboard on Redis sorted sets: top lists, around-me rank windows, daily and weekly boards with ZUNIONSTORE, TTL cleanup and tie-breaking.
Practice and Review
- Practice Exercises with Solutions Twelve graded Redis exercises on strings, expiry, lists, hashes, sets, sorted sets, transactions, rate limiting, caching and locks, with runnable code and solutions.
- Redis Interview Questions 30 common Redis interview questions with concise answers: single-threaded speed, data types, persistence, eviction, replication, cluster, locks and pitfalls.
- Redis Cheat Sheet A one-page Redis cheat sheet: commands by data type, expiry, transactions, persistence, replication, cluster, config, ACL and safety reminders in compact tables.