Databases 17 min read

Mastering Redis: 16 Essential Interview Questions and Answers

This article provides a comprehensive overview of Redis, covering its definition, data structures, performance characteristics, persistence mechanisms, expiration strategies, memory eviction policies, hot key handling, cache pitfalls, deployment modes, Sentinel and Cluster features, and replication details, all framed as interview questions.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Mastering Redis: 16 Essential Interview Questions and Answers

1. What is Redis? What can it do?

Redis stands for Remote Dictionary Server, a key‑value storage system written in C, often described as a remote data or dictionary service.

Typical use cases include caching, databases, message queues, distributed locks, like lists for likes, leaderboards, and more.

2. What are Redis’s eight data types and their scenarios?

Redis offers five basic data structures and three special ones.

string : simple strings, useful for counters, fan counts, simple distributed locks.

hashmap : key‑value maps.

list : ordered lists, can act as stacks, queues, or blocking queues.

set : unordered collections without duplicates, suitable for likes, favorites.

zset : sorted sets with scores, ideal for leaderboards.

geospatial : geo‑type introduced in 3.2 for location and distance calculations.

hyperloglog : cardinality estimator, commonly used for unique visitor counting.

bitmap : bit‑level storage for binary states, useful for tracking active/inactive users.

3. Why is Redis so fast?

Operates entirely in memory.

Uses a single‑threaded request model, avoiding context switches.

Employs I/O multiplexing.

Implemented in C with many optimizations such as the SDS dynamic string.

4. Does Redis’s multithreading after version 6.0 cause thread‑safety issues?

No. Redis still processes client commands in a single thread; multithreading is only used for I/O and protocol parsing, so thread safety is maintained.

The multithreaded I/O improves network throughput, which was the main performance bottleneck.

5. What persistence mechanisms does Redis provide and their pros/cons?

Redis supports two persistence methods: AOF and RDB.

AOF (Append‑Only File)

Every write command is appended to an .aof file and flushed to disk according to the fsync policy.

Advantages:

Better data durability – at most one second of data loss.

High write performance due to sequential appends.

Human‑readable log, facilitating emergency recovery.

Disadvantages:

File size larger than RDB snapshots.

Higher write overhead; requires periodic rewriting.

Slower recovery, unsuitable for cold backups.

RDB (Snapshot)

Periodically forks a child process to write the entire dataset to a binary .rdb file.

Advantages:

Fast recovery of large datasets.

Minimal impact on client service during snapshotting.

Disadvantages:

Potential data loss between snapshots (e.g., up to 5 minutes).

Forking large datasets may pause Redis for seconds.

6. What are Redis’s key expiration deletion strategies?

Redis provides three expiration policies:

Timed expiration : each key with TTL creates a timer that deletes it exactly when it expires; CPU‑intensive.

Lazy expiration : keys are checked for expiry only upon access; saves CPU but may leave many expired keys in memory.

Periodic expiration : Redis scans a subset of keys at regular intervals, balancing CPU and memory usage.

7. How does Redis handle memory exhaustion?

Redis defines eight eviction policies:

noeviction – returns an error, no keys are evicted.

allkeys‑lru – evicts any key using LRU.

volatile‑lru – evicts only keys with an expiration using LRU.

allkeys‑random – evicts random keys.

volatile‑random – evicts random keys with an expiration.

volatile‑ttl – evicts keys with the shortest TTL.

volatile‑lfu – evicts based on LFU among keys with expiration.

allkeys‑lfu – evicts based on LFU among all keys.

8. How to solve Redis hot‑key problems?

Hot keys receive massive concurrent requests, potentially crashing the server.

Cache results in local memory.

Distribute hot keys across multiple servers.

Set keys to never expire.

9. What are cache penetration, cache breakdown, and cache avalanche, and how to mitigate them?

Cache penetration

Requests for data that neither exist in cache nor in the database.

Mitigation: use Bloom filters or return empty objects.

Cache breakdown

When a hot key expires, a flood of requests hits the database.

Mitigation: use mutex locks or set the key to never expire.

Cache avalanche

Many keys expire simultaneously, overwhelming the database.

Mitigation: stagger expirations, add mutex locks, keep some keys permanent, or employ a double‑cache strategy.

10. What deployment modes does Redis support?

Standalone (single‑node) – basic mode for development.

Sentinel – provides automatic failover, monitoring, and notifications.

Cluster – sharding across multiple masters with built‑in high availability.

Master‑slave replication – separates read and write traffic for load balancing.

11. What are the functions of Redis Sentinel?

Monitors master and slave instances for health.

Performs automatic failover, promoting a slave to master when needed.

12. How does Sentinel elect a leader?

The first Sentinel detecting a master failure proposes itself as leader.

Other Sentinels vote for the first proposer if they haven’t voted yet.

If the proposer receives votes from a majority and exceeds the quorum, it becomes the leader.

Concurrent elections repeat until a leader is chosen.

13. How does Redis Cluster store data?

Cluster splits the keyspace into 16,384 slots and distributes them evenly among master nodes.

14. How does Redis Cluster handle node failures?

Nodes periodically ping each other; if a node stops responding, others mark it as subjectively down. When a majority agrees, the node is marked as objectively down and removed from the cluster.

15. What is the principle of master‑slave synchronization?

Slave sends SYNC to master; master creates an RDB snapshot in the background.

After snapshot, master sends the RDB file and buffered commands to the slave.

Slave writes the snapshot to a temporary file, then replaces its existing RDB without blocking.

Subsequent write commands are streamed asynchronously to the slave.

16. What is disk‑less replication?

When master’s RDB snapshot is disabled, replication can transmit data directly over the network to slaves, avoiding disk I/O but still consuming network resources.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

rediscachingPersistenceClusterinterviewIn-Memory Database
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.