Redis Overview: Data Types, Persistence, High Availability, and Common Cache Issues
This article provides a comprehensive guide to Redis, covering its role as a non‑relational database, detailed data types and commands, persistence mechanisms (RDB and AOF), memory eviction strategies, high‑availability features such as replication, Sentinel and clustering, and solutions to typical cache problems like penetration, avalanche, and hot‑key handling.
Redis is a key‑value, in‑memory NoSQL database that excels in high‑performance read/write scenarios. It offers various data structures—string, hash, list, set, sorted set—each with specific commands for manipulation, as shown in the code examples below.
# Increase/modify
redis > set key value
# Retrieve
redis > get key
# Delete
redis > del key
# Add multiple key‑value pairs
redis > mset key1 value1 key2 value2 ...
# Retrieve multiple values
redis > mget key1 key2 ...
# Get string length
redis > strlen key
# Append string
redis > append key value
# Set expiration
redis > setex key seconds value
# Set if not exists
redis > setnx key valueBeyond strings, Redis supports hash (field‑value maps), list (ordered collections), set (unique unordered elements), and sorted set (ordered by score). Each type has its own set of commands, also illustrated in the source.
Persistence
Redis provides two persistence options: RDB snapshots and AOF append‑only logs. RDB offers fast recovery but lower durability, while AOF ensures higher data safety with configurable fsync policies (always, everysec, no). Modern Redis can combine both via hybrid persistence.
Memory Eviction
When memory limits are reached, Redis evicts keys using policies such as volatile‑lru, allkeys‑lru, volatile‑ttl, random, and noeviction. Since Redis 4.0, LFU (least‑frequency‑used) policies (volatile‑lfu, allkeys‑lfu) provide finer‑grained eviction based on access frequency.
High Availability
Redis achieves high availability through replication, Sentinel, and clustering. Replication creates master‑slave copies for read scaling and basic failover. Sentinel adds automatic failover, leader election, and client redirection. Clustering distributes data across multiple nodes, solving write scalability and storage limits.
Sentinel Failover Process
The failover consists of four steps: detecting master down, electing a new master (considering slave‑priority, replication offset, and ID), electing a Sentinel leader (quorum votes), and finally switching the master and notifying clients.
Common Cache Issues and Solutions
Typical cache problems include cache penetration (queries for non‑existent keys), cache avalanche (mass expiration), cache breakdown (hot‑key spikes), large keys, and hot‑key hot spots. Solutions involve using Bloom filters, setting random TTLs, employing mutex locks, implementing secondary caches, and distributing hot keys across multiple instances.
Large Key Detection
Tools such as redis-cli --bigkeys , redis‑rdb‑tools , and the memory usage command help identify oversized keys, which can be split or sharded to improve performance.
Xueersi Online School Tech Team
The Xueersi Online School Tech Team, dedicated to innovating and promoting internet education technology.
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.