Redis Interview Questions and Answers
Redis, as a high-performance key-value storage system, frequently involves core data structures, performance tuning, cluster architecture, and persistence mechanisms in interviews. The following questions focus on high-frequency interview points for Redis, providing deep analysis based on actual scenarios.
Data Types and Memory Management
## What data types does Redis support?
Redis supports five core data types: String (String), Hash (Hash), List (List), Set (Set), Sorted Set (ZSet). Among them, the string type is foundational, while hash, list, set, and sorted set are used for complex data storage, supporting dynamic expansion and efficient operations.
## How to optimize Redis memory usage?
Optimize memory consumption by compressing strings (e.g., using Redis's COMPRESS option) and controlling it via memory eviction policies (such as LRU or LFU). Additionally, use the eviction policy when memory is insufficient (maxmemory-policy) to prevent system crashes. The choice should be balanced according to business scenarios regarding caching versus performance.
Performance Optimization and Cluster Architecture
## How does Redis achieve high concurrency?
Improve I/O efficiency through a multi-threaded event loop and asynchronous IO, combined with memory reuse (such as the OOM-Offlimit mechanism in Redis) to optimize resource utilization. For high-concurrency scenarios, adopt master-slave replication to distribute load and use sentinel mode for automatic failover.
## How does a Redis Cluster ensure data consistency?
Redis Cluster distributes data across multiple nodes using sharding (Sharding) and the consistent hashing algorithm, ensuring operation atomicity via RPC communication. However, note that in cluster mode, data writing requires inter-node communication, which may introduce brief delays; this must be designed carefully based on business scenarios.
Persistence and Data Recovery
## How does Redis achieve persistence?
Redis supports two persistence methods: RDB (Snapshot) (saved periodically via save or bgsave commands) and AOF (Append-only file) (appending writes to logs). RDB is suitable for quick recovery, while AOF is safer but has slower recovery speeds; the choice depends on business requirements.
## How to restore Redis persistence data?
Use the redis-cli restore command to directly restore an AOF file, or use the redis-check-server tool to check and repair RDB files. Note that AOF files need to be synchronized regularly to disk to prevent data loss due to disk failures.
Transactions and Locking Mechanisms
## How do Redis transactions ensure atomicity?
Wrap multiple commands in a transaction block (BEGIN); Redis executes them sequentially while ignoring conflicts. If any command within the transaction fails, the entire transaction rolls back, ensuring data consistency.
## How does Redis implement locking?
Use SETEX or SETNX commands to set timeout locks, or implement distributed locks via Lua scripts. Pay attention to lock expiration times and race conditions; avoid deadlocks caused by network delays.
Common Issues and Debugging
## How to investigate Redis memory leaks?
Check memory usage using redis-cli info memory and analyze leak causes with redis-cli debug memory. Common issues include unreleased key-value pairs and unclosed connections; regularly check logs and monitoring metrics.
## How to troubleshoot Redis performance bottlenecks?
Analyze performance indicators using redis-cli pstats, combined with the INFO command for statistics on memory, connections, and command execution. Common bottlenecks include high-concurrency writes, unoptimized query statements, or inappropriate caching strategies.
Network and Security
## How does Redis handle network latency?
Improve network efficiency through TCP connection optimization (e.g., using SO_REUSEADDR, SO_KEEPALIVE) and asynchronous IO. Configure DNS resolution and IP whitelists to restrict access and prevent malicious attacks.
## How is password protection configured for Redis?
Set the password using the requirepass instruction, limit access IPs via bind, and verify identity with the auth command. Note password security; avoid hardcoding passwords in configuration files.