Redis Interview Questions and Core Concepts Explained
This article provides a comprehensive overview of Redis, covering why it is used as a cache, its single‑threaded efficiency, multithreading in version 6.0, common data structures and their use cases, pipeline advantages, persistence mechanisms, transactions, Bloom filter for massive URL checks, internal data organization, and incremental rehashing.
1. Why Use Redis as a Cache?
Cache Benefits
The purpose of using a cache is to improve read/write performance, especially read performance, enabling higher concurrency.
Advantages of Redis
Fast read speed, easily handling 100k+ concurrent requests on a single node.
Supports multiple data structures such as strings, lists, sets, sorted sets, and hashes.
Rich features like replication, clustering, and persistence.
Can be used for additional functions such as message queues and distributed locks.
2. Why Is Redis’s Single‑Threaded Model Still Highly Efficient?
C Implementation : Written in C, which runs close to the hardware and offers high performance.
Single‑Thread Advantages : Eliminates context‑switch overhead and lock contention, simplifying maintenance.
Pipeline : Redis is limited by memory and network rather than CPU; pipelining reduces network round‑trip time by batching commands.
Storage Optimizations : Each core data structure has multiple implementations, allowing the engine to choose the most efficient one based on size.
3. Why Did Redis 6.0 Introduce Multithreading?
Multithreading is applied only to I/O threads; command execution remains single‑threaded.
Redis can handle 80k–100k QPS, which is sufficient for most companies, but ultra‑high‑traffic scenarios (>100M operations) require higher throughput, prompting the addition of I/O threads that can double performance.
Cluster solutions also increase QPS but bring management complexity, command incompatibility, and hotspot issues.
4. Common Redis Data Structures and Their Use Cases
String
Use Cases
Counting – fast counters and cache queries.
Shared session storage – centralizing user sessions.
Rate limiting – e.g., limiting an SMS API to 5 calls per minute.
Hash
Similar to Java’s HashMap but stores field‑value pairs.
Use Cases
Ideal for storing object‑like data; reduces memory compared to many separate string keys. set user:1 {"id":1,"name":"test1","age":18} Using hash:
hmset user:1 name test1 age 18
hmset user:2 name test2 age 20List
Ordered collection of strings, up to 2^32‑1 elements.
Use Cases
User‑specific article lists with pagination.
Message queues – lpush + rpop can implement a blocking queue.
Set
Unordered collection of unique strings.
Use Cases
Tag management – finding users with common interests.
Random selection for lotteries, social graphs, etc.
Sorted Set (ZSET)
Each element has a score used for ordering; supports range queries and ranking.
Typical Scenario
Leaderboards for videos, based on time, views, likes, etc.
5. Benefits of Pipeline and Why Use It
Redis client command flow: send → queue → execute → return.
Network round‑trip time (RTT) dominates latency.
Example RTT calculation for an 800 km distance:
800 km × 2 / (300000 km/s × 2/3) ≈ 8 msSince command execution is usually microseconds, network latency becomes the bottleneck.
Pipeline batches many commands into a single RTT, dramatically reducing total time (over 100× faster in tests).
Reduces network overhead; execution is faster than issuing commands one by one.
The larger the client‑server latency, the more noticeable the gain.
6. Why Doesn’t Redis Officially Provide a Windows Version?
The Linux version is stable and widely used; maintaining a Windows build would introduce compatibility challenges.
7. Redis Persistence Mechanisms and Their Differences
RDB (Redis DataBase)
Creates point‑in‑time snapshots of memory data.
Advantages
Single dump.rdb file simplifies backup.
Good disaster recovery; can be stored safely.
Faster startup for large datasets compared to AOF.
Disadvantages
Potential data loss between snapshots; not suitable for strict durability requirements.
AOF (Append‑Only File)
Logs every write command; on restart, Redis replays the log to rebuild state.
Disadvantages
Larger file size and slower recovery.
Startup slower than RDB for large datasets.
8. What Is a Redis Transaction and How Does It Work?
A transaction groups commands between MULTI and EXEC, guaranteeing sequential execution without interleaving.
Redis transactions are weak: they only detect syntax errors, not runtime errors.
When a syntax error occurs, the whole transaction is aborted; runtime errors may still allow execution.
9. Fast URL Existence Check Among Hundreds of Billions
Traditional HashMap : O(1) lookup but huge memory consumption (e.g., 100 billion URLs × 64 B ≈ 640 GB).
Bloom Filter : Probabilistic data structure using a bit array and multiple hash functions; dramatically reduces memory at the cost of false positives.
Widely used in web blacklists, spam filters, crawler deduplication, and Google’s Bigtable.
False‑Positive Mitigation
Increase array size.
Use multiple hash functions and require all to match.
10. How Redis Organizes Its Internal Data Structures
All key‑value pairs are stored in a global hash table (array of buckets), providing O(1) access.
When the hash table grows, rehashing can cause blocking; Redis uses chained hashing (linked lists per bucket) to resolve collisions.
11. What Is Incremental Rehash?
Redis maintains two hash tables during rehashing: the old one and a newly allocated larger one.
Instead of copying all entries at once, Redis moves a small portion of entries from the old table to the new one on each client request, spreading the cost over many operations and avoiding long pauses.
This incremental approach keeps the server responsive while gradually expanding capacity.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
