redis
description
- Redis is an open source in memory data structure store used as a database, cache, message broker, and streaming engine.
effect
- Reduce the stress of databases and support more speed than databases.
Usage
- Produce token
- Shared session
- Distributed lock
- Incring id
- Verification code
app
- redis-server
- redis-cli
- redis-benchmark
IO-reuse model
- select
- epoll
- evport
- kqueue
- redis support a common type : aeApistate
Reactor design model
- 事件驱动循环流程,通过Reactor的方式,可以将用户线程轮询IO操作状态的工作统一交给handle_events事件循环进行处理。
- Reactor线程负责调用内核的select/epoll函数检查socket状态。当有socket被激活时,则通知相应的用户线程(或执行用户线程的回调函数),执行handle_event进行数据读取、处理的工作。
Redis thread model
- redis-client在操作的时候,会产生具有不同事件类型的socket。在服务端,有一段I/0多路复用程序,将其置入队列之中。然后,IO事件分派器,依次去队列中取,转发到不同的事件处理器中。
data type
- common
- keys patten : return all keys that match the patten. patten : * means one or some random charators. ? means one random charators.
- del key1 key2
- exists key
- rename keyname newkeyname
- expire key second: set expired time of the key
- ttl key : time to live
- persist key
- type key : return “string” “list” “set” “hash” “zset” if the key exists and return “node” if not.
- string
- set key value
- get key
- getset key value
- incr key / decr key
- incrby key increment / decrby key decrement
- append key value
- list
- lpush key value1 value2 …
- rpush key value1 value2 …
- Irange key start end : get values whose index begin with the “start” and end with the “end” from list; it starts with the tail of list when the “start” is setted with -1.
- lpushx ket value : only when the key exsits.
- rpushx
- lpop key
- rpop key
- rpoplpush resource destination
- llen key
- lset key index value
- set
- sadd key value1 value2 …
- srem key member1 member2 …
- smembers key : get all members of key
- sismember key member : judge member in member of key
- sdiff key1 key2 … : return different set
- sinter key1 key2 … : return inter set
- sunion key1 key2 …
- scard key : get numbers of members of the set
- scandmember key : random retunr of members of the set
- sdiffstore destination key1 key2 … : save the difference to destination
- sinterstore destination key1 key2 …
- sunionstore destination key1 key2 …
- hash (use as a map)
- hset key field value : set the field/value of the key
- hmset key field1 value1 field2 value2 … set mutil-
- hget key field : get value of field of key
- hmget key field1 field2 …
- hkeys key : get all keys
- hvals key : get all values
- hgetall key : get all field-value of key
- hdel key field1 field2 …
- hincrby key field increment
- hexists key field
- hlen key
- zset(sorted-set)
- zadd key score1 member1 score2 member2
- zscore key member : return socre of member of key
- zcard key : return size of members of key
- zcrm key member1 member2
- zrange key start end [withscore] : return member with score
- zremrangebyrand key start end : remove member by the rank of score
- zremrangescore key min max : remove member by the range of score
- zrangebyscore key min max [withscore] [limit offest count] : return members whose score begins with min and ends with max. [limit offest count] : offest menas that begin from the member whose index is offest. count means the count of returning member.
- zincrby key increment member : incre the score of member by increment.
- zcount key min max : return count of what is larger than min and less than max.
- zrank key member : return the larger rank of the member in zset
- zrevrank key member : return the less rank of the member in zset
- bitmaps
- hyperloglogs
- geospatial indexes
- streams
the usage scenes
- string
- set/get : use as the counter
- hash
- session
- list
- message queue; lrange use as paging
- set
- global deduplication
- zset
- the rank list; the time-daley task; sort in the range
expired-strategy and memory-clean mechanism
- timing remove
- remove random keys per 100 seconds
- delay remove
- remove keys when it is used
maxmemory-policy
- noevicion : report error when memory isn’t enough to write.
- allkeys-lru : remove keys that is laste used when …
- allkeys-random : remove random keys when …
- volatile-lru : remove keys that is laste used from the all what are setted expired time when …
- volatile-random : remove random keys from what are setted expired time when …
- volatile-ttl : remove keys that is laste time to expire from all what are setted expired time when …
the consistance of redis and database
- 一致性问题是分布式常见问题,还可以再分为最终一致性和强一致性。数据库和缓存双写,就必然会存在不一致的问题。答这个问题,先明白一个前提。就是如果对数据有强一致性要求,不能放缓存。我们所做的一切,只能保证最终一致性。另外,我们所做的方案其实从根本上来说,只能说降低不一致发生的概率,无法完全避免。因此,有强一致性要求的数据,不能放缓存。
回答:首先,采取正确更新策略,先更新数据库,再删缓存。其次,因为可能存在删除缓存失败的问题,提供一个补偿措施即可,例如利用消息队列。
缓存穿透
- description
- 黑客故意去请求缓存中不存在的数据,导致所有的请求都怼到数据库上,从而数据库连接异常。
- solution
- (一)利用互斥锁,缓存失效的时候,先去获得锁,得到锁了,再去请求数据库。没得到锁,则休眠一段时间重试。
- (二)采用异步更新策略,无论key是否取到值,都直接返回。value值中维护一个缓存失效时间,缓存如果过期,异步起一个线程去读数据库,更新缓存。需要做缓存预热(项目启动前,先加载缓存)操作。
- (三)提供一个能迅速判断请求是否有效的拦截机制,比如,利用布隆过滤器,内部维护一系列合法有效的key。迅速判断出,请求所携带的Key是否合法有效。如果不合法,则直接返回。
缓存雪崩
- description
- 即缓存同一时间大面积的失效,这个时候又来了一波请求,结果请求都怼到数据库上,从而导致数据库连接异常。
- solution
- (一)给缓存的失效时间,加上一个随机值,避免集体失效。
- (二)使用互斥锁,但是该方案吞吐量明显下降了。
- (三)双缓存。我们有两个缓存,缓存A和缓存B。缓存A的失效时间为20分钟,缓存B不设失效时间。自己做缓存预热操作。然后细分以下几个小点
- I 从缓存A读数据库,有则直接返回。
- II A没有数据,直接从B读数据,直接返回,并且异步启动一个更新线程。
- III 更新线程同时更新缓存A和缓存B。
How to solve the parallel getting key problem.
- analyze
- 这个问题大致就是,同时有多个子系统去set一个key。这个时候要注意什么呢?大家思考过么。需要说明一下,博主提前百度了一下,发现答案基本都是推荐用redis事务机制。博主不推荐使用redis的事务机制。因为我们的生产环境,基本都是redis集群环境,做了数据分片操作。你一个事务中有涉及到多个key操作的时候,这多个key不一定都存储在同一个redis-server上。因此,redis的事务机制,十分鸡肋。
- solution
- (1)如果对这个key操作,不要求顺序,这种情况下,准备一个分布式锁,大家去抢锁,抢到锁就做set操作即可,比较简单。
- (2)如果对这个key操作,要求顺序,假设有一个key1,系统A需要将key1设置为valueA,系统B需要将key1设置为valueB,系统C需要将key1设置为valueC.期望按照key1的value值按照 valueA–>valueB–>valueC的顺序变化。这种时候我们在数据写入数据库的时候,需要保存一个时间戳。
- 如利用队列,将set方法变成串行访问也可以。