Top 30 Redis Interview Questions & Answers
Top 30 Redis interview questions and answers for backend developers: data types, caching patterns, eviction, persistence, pub/sub, Sentinel and Cluster.
Redis Basics & Data Types
Q1. What is Redis?
An in-memory data store used as a cache, database, message broker and queue. It keeps data in RAM, so most operations take well under a millisecond.
Q2. Why is Redis so fast?
Data lives in memory, commands run on an efficient single-threaded event loop with no lock contention, the data structures are optimised, and the protocol is simple.
Q3. What data types does Redis support?
Strings, lists, sets, sorted sets, hashes, bitmaps, HyperLogLogs, geospatial indexes and streams. Modules and Redis Stack add JSON and search.
Q4. When do you use a sorted set?
When items need ordering by a score, such as leaderboards, rate-limit windows or priority queues. ZADD, ZRANGE and ZRANK work in O(log n).
Q5. When do you use a hash?
For objects with fields, such as HSET user:42 name Asha plan pro. It is more memory-efficient than many separate keys and lets you read or change single fields.
Q6. What is the difference between Redis and Memcached?
Memcached is a simple key-value cache for strings. Redis adds rich data types, persistence, replication, pub/sub, scripting and transactions.
Q7. Is Redis single-threaded?
Command execution runs on one main thread, so each command is atomic. Since Redis 6, network I/O can use extra threads, and some background tasks run on other threads.
Q8. What does KEYS * do, and why avoid it in production?
It scans every key and blocks the server until it finishes, which can freeze a large instance. Use SCAN, which iterates in small batches.
Q9. What is a Redis key naming convention?
Use colon-separated namespaces such as user:42:profile or cache:product:17. It keeps keys readable and easy to find with SCAN patterns.
Q10. What does INCR do?
It increments an integer value atomically and returns the new value. It suits counters, rate limiting and generating IDs without race conditions.
Caching, Expiry & Eviction
Q11. What is the cache-aside pattern?
The app first reads from Redis. On a miss, it loads from the database, writes the result to Redis with a TTL, and returns it. On updates, it deletes or updates the cached key.
Q12. How does key expiry work?
Set a TTL with EXPIRE or SET key value EX 60. Redis removes expired keys when they are accessed and through a periodic sampling process.
Q13. What are Redis eviction policies?
When memory reaches maxmemory, a policy decides what to remove: noeviction returns errors, allkeys-lru removes least recently used keys, and volatile-ttl removes keys with the shortest TTL. allkeys-lfu removes the least frequently used.
Q14. What is cache stampede, and how do you prevent it?
When a popular key expires, many requests hit the database at once. Prevent it with a lock so only one request rebuilds the value, add random jitter to TTLs, or refresh early in the background.
Q15. What is the difference between write-through and write-behind caching?
Write-through writes to the cache and database together, so the cache is always current. Write-behind writes to the cache first and updates the database later, which is faster but can lose data.
Q16. How do you implement rate limiting with Redis?
Use INCR on a key per user per time window with an expiry, or a sorted set of request timestamps for a sliding window. A Lua script keeps the check and the increment atomic.
Q17. How do you store sessions in Redis?
Store session data under a key such as session:<id> with a TTL, and refresh the TTL on activity. All app servers can then read the same sessions.
Q18. How do you implement a distributed lock?
SET lock:job unique-value NX PX 30000 acquires the lock only if it does not exist, with an expiry. Release it only when the value matches, using a Lua script, so you never delete another client's lock.
Q19. What is the difference between DEL and UNLINK?
DEL frees memory immediately and can block on large keys. UNLINK removes the key at once and frees its memory in a background thread.
Q20. How do you avoid big keys?
Split large collections across keys, paginate large lists, and find big keys with redis-cli --bigkeys or MEMORY USAGE, since they slow commands and replication.
Persistence, Messaging & Scaling
Q21. What is the difference between RDB and AOF persistence?
RDB saves point-in-time snapshots, which is compact and fast to restart but can lose recent writes. AOF logs every write, which loses less data but uses more disk. Many setups enable both.
Q22. What is Redis pub/sub?
Clients publish messages to channels and subscribers receive them in real time. Messages are not stored, so subscribers that are offline miss them.
Q23. What are Redis Streams?
An append-only log of entries with IDs. Consumer groups let several workers share messages, acknowledge them and replay unprocessed ones, much like a lightweight Kafka.
Q24. How do transactions work in Redis?
MULTI queues commands and EXEC runs them in order with no other client's commands between them. WATCH adds optimistic locking: EXEC fails if a watched key changed. There is no rollback.
Q25. What are Lua scripts in Redis?
Scripts run with EVAL or as functions that execute atomically on the server. They combine reads and writes in one step, such as check-and-set logic.
Q26. What is pipelining?
Sending many commands without waiting for each reply, then reading all replies together. It cuts network round trips and greatly increases throughput.
Q27. How does Redis replication work?
Replicas copy the primary's data and receive a stream of its writes. Replication is asynchronous, so replicas may lag slightly and a failover can lose the latest writes.
Q28. What is Redis Sentinel?
A system that monitors a primary and its replicas, promotes a replica if the primary fails, and tells clients the new primary's address.
Q29. What is Redis Cluster?
A way to shard data across several primaries using 16,384 hash slots, each with replicas. Multi-key commands work only when all keys are in the same slot, which hash tags like {user:42} ensure.
Q30. How do you monitor Redis?
Use INFO for memory, clients and hit rate, SLOWLOG for slow commands, and LATENCY tools. Track memory usage, evictions, cache hit ratio and replication lag.