Redis (Remote Dictionary Server)
Redis is a networked, in-memory, distributed key-value store with optional persistence, written in ANSI C. According to monthly rankings on DB-Engines.com, Redis is the most popular key-value store (based on Wikipedia). It is a high-performance key-value database providing remote services.
1. Why is Redis Fast and Why is it Single-threaded?
Redis is single-threaded with IO multiplexing. The single-threaded model reduces the overhead of context switching between threads and avoids thread safety issues that can arise in multi-threaded environments. IO multiplexing ensures efficient handling of TCP connections and data. The protocol is simplified, using TCP for data transmission and directly performing GET/SET operations.
2. Basic Data Structures
String: Maximum size of 512MB.
Set: An unordered collection of unique elements.
ZSet (Sorted Set): A set with scores, sorted by these scores.
Hash: A collection of field-value pairs.
List: A linked list of elements.
Other data structures include:
Bitmap: A boolean map, often used for Bloom filters, sign-in statistics, or user session tracking.
HyperLogLog: Used for approximating cardinality (e.g., unique page views or user visits).
GEO: Used for geospatial data, such as location-based services (e.g., maps and nearby people).
Stream: Primarily used for implementing message queues, allowing simple message queuing.
3. Persistence
RDB (Redis Database): It takes snapshots of data, and an asynchronous thread performs full data persistence. The drawback is that if Redis crashes between snapshots, some data may be lost.
AOF (Append Only File): It logs every write operation, allowing for full persistence. The drawback is that the file size can grow large, though it can be compressed (e.g., removing redundant records).
Choosing the right persistence approach depends on business requirements—whether strong data consistency is necessary or performance is prioritized.
4. Cache Expiry Policies
Lazy Deletion: Data is removed when accessed after expiry.
Periodic Deletion: Data is deleted at fixed intervals, regardless of access.
5. Cache Eviction Policies
Random: A random key is removed.
LRU (Least Recently Used): Removes the least recently used data.
LFU (Least Frequently Used): Removes the least frequently used data.
6. Distributed Locks
SETNX and Redisson: The main difference is whether automatic renewal of the lock (watchdog mechanism) is needed.
6.1 Locking Principle
Redis uses Lua scripts to perform locking, ensuring atomicity. Since Redis is single-threaded, no other commands can interrupt the operation. The data structure used for locking is a Hash. The Big Key represents the business key, and the Small Key is the thread's UUID. If the key doesn't exist, a lock is acquired by setting the value to 1.
6.2 Mutex (Mutual Exclusion Lock)
Distributed locks ensure mutual exclusion by using the same Lua script for lock acquisition. Thread 1 successfully acquires the lock with value 1. Thread 2, with a different UUID for the small key, will return the remaining lock time for thread 1 and spin-wait for the lock.
6.3 Reentrant Lock
Similar to mutex locks, a reentrant lock allows a thread that already holds the lock to acquire it again. The thread executes the Lua script again, incrementing the value by 1, and successfully retains the lock.
6.4 Lock Release
To release a lock, the value is decremented (lock.unlock), and when the value reaches 0, the lock is deleted.
6.5 Watchdog
The watchdog mechanism extends the lock's validity. A background thread checks the expiration time and automatically renews the lock. By default, the lock expires after 30 seconds, and the watchdog checks the expiration every 10 seconds.
This is a key difference from the SETNX operation. If an expiration time is set, the watchdog mechanism won't be effective. If the system crashes, the watchdog mechanism will fail, and the lock will be automatically released when the time is up.
If a crash occurs, will it impact the business? It depends on the specific case. For example, for acknowledgment checks (ACK), RedLock (election mechanism), and master-slave replication, the situation should be analyzed based on specific conditions. There is no absolute solution, only the most suitable one for the scenario:)
Redis架构思想 Remote dictionary server
Redis(Remote Dictionary Server)是一个使用ANSI C编写的支持网络、基于内存、分布式、可选持久性的键值对存储数据库。根据月度排行网站DB-Engines.com的数据,Redis是最流行的键值对存储数据库(基于维基百科)
提供高速远程服务的键值对数据库(个人理解)
1 Redis为什么快,为什么是单线程的
单线程+IO多路复用,单线程减少线程切换的开销,也避免了多线程场景下的线程安全问题,IO多路复用保证TCP连接高效处理数据,
简化传输协议,使用TCP进行传输协议,直接进行get set操作
2 基本数据结构
String 字符串最大512MB、Set、ZSet、Hash 、List
了解
Bitmap 布尔map可以做布隆过滤器 可以用于签到统计、用户登录态判断等
HyperLogLog 可以根据场景选择使用,常用于网页 PV、UV 的统计
GEO 比如说百度地图、高德地图、附近的人等
Stream 主要就是消息队列了,可以实现一个简单的消息
3 持久化
RDB数据做快照,异步线程做全量数据存储,缺点是在每次刷新RDB间隔中发生宕机,会丢失一些数据
AOF日志记录,记录每一条Redis的记录,优点是可以全量地保持持久化,缺点文件过大,但可以做文件压缩:比如压缩某些重复记录
结合业务场景去选择,适合的持久化方案,是要数据强一致还是要性能
4 缓存过期策略
惰性删除和定期删除
5 缓存淘汰策略
random,LRU,LFU
6 分布式锁
SETNX 和 redission 方案,最大的区别就是是否需要自动续期的看门狗机制。
6.1 加锁原理
底层使用lua脚本进行加锁,lua脚本保证了原子性,因为Redis是单线程的,中间不会有别的命令来打断这个操作。加锁的数据结构是Hash,大Key是业务Key,需要根据业务设计,小Key是线程uuid,如果key不存在,加锁value如果获取锁成功为1。
6.2 互斥锁
分布式锁如何互斥,加锁使用的是同一个lua脚本,线程1和线程2竞争锁,线程1获取锁成功value为1,线程2获取小key uuid不同会返回线程1锁的剩余时间,并自旋获取锁资源
6.3 锁可重入
这个跟锁互斥是一样的,线程1再次获取锁资源,执行同一个lua脚本,value+1,线程再次成功获取到锁
6.4 锁释放
lock.unlock,value-1,value为0时删除锁,
6.5 看门狗
锁续期机制,相当于起了一条后台线程定时的去查询锁过期时间,并自动进行锁续期,默认是30秒过期时间,每10秒去查询一次。
这是于setnx的关键不同。
如果设置了过期时间这看门狗的机制不会生效。
如果发生了宕机,看门狗机制也会失效,到时间自动释放
如果放生宕机,会不会影响业务?具体情况具体分析,比如回查ACK、红锁(选举机制),主从,具体情况具体分析,没有绝对,只有最适合。
Top comments (0)