Old routines
4, using Redis problems encountered in the process
1. What is the Redis
Before the introduction Redis first look Nosql (non-relational database)
We all know MySql is a relational database, what is non-relational database? What does it do?
In order to address the high concurrency, high availability, highly scalable, database solution for large data storage and a series of problems that arise, it is NoSql. It is not a replacement for relational databases, good only as a supplement relational database.
Redis is using c language development of a high-performance key-value database . Redis by key type of data stored.
Redis usage scenarios : Cache (data queries, short connections, news content, goods, content, etc.)
(Most used) distributed cluster architecture separate session
Online chat room buddy list
Task Queue
(Spike, buy, 12306, etc.) application ranking
Website statistics
Processing data has expired (accurate to milliseconds)
2. Why use Redis
In order to address the high concurrency, high availability, high scalability, large data storage and other issues, MySql not be well for us to provide services, the introduction of Redis.
So why use Redis it?
1, fast: First Redis is written in C language, pure memory operation, the second core is based on the non-blocking IO multiplexing mechanism, single-threaded multi-threaded avoid the frequent context switching problem
2, supports a variety of data types, five kinds of data types: String, Hash, List, the Set, zset (you may be asked during the interview), as well as other data structures: HyperLogLog, Geo, Pub / Sub
3, support for multiple languages, Java / PHP
4, support for persistent storage, Redis snapshots by way of the data persisted to disk
3, how to use Redis
First, let's look at Redis5 data types: String, Hash, List, Set, Zset
type of data | General operation | Scenarios |
String | Assignment syntax: set key value The value syntax: get key Delete Syntax: del key23: 19: 59 Increase or decrease in value syntax: incr key Decreasing numerical syntax: decr key |
Product number, order number using string of incremental generation digital features Auto-increment primary keys Cache counter Sharing session |
Hash | One gets a field value syntax: hget key field One can get more field values syntax: hmget key field [field ...] Get all field values syntax: hgetall key Delete You can delete a field you can delete multiple syntax: hdel key field [field ...]
|
Suitable for storing objects Change data, modify, read user attributes |
List | Add elements to the list on the left syntax: lpush key value [value ...] The syntax to add elements to the right of the list: rpush key value [value ...] |
Reviews list of users to post product reviews, will be converted into json review information stored in the list. User comments page query list, remove the json data to show a page from the redis. message queue The principle: the use of push operations list, the list will be present in person, and then the worker thread and then POP operation will be taken to perform the task |
Set | Add / remove element syntax: sadd key member [member ...] Obtains the set of all elements of syntax: smembers key To determine whether the elements in the collection syntax: sismember key member Poor set syntax: sdiff key [key ...] Intersection syntax: sinter key [key ...] And set syntax: sunion key [key ...] |
label Mutual friend, the second time friends Use of unique, independent IP can count all access to the site
|
token | Increased element syntax: zadd key score member [score member ...] Gets the element score syntax: zscore key member Delete element syntax: zrem key member [member ...] Gets the number of elements in the collection syntax: zcard key |
Merchandise sales charts 游戏的用户得分排行榜 排行榜 |
4、Redis使用过程中遇到的问题
1、Redis缓存雪崩:
情况:Redis缓存雪崩指一段时间内,缓存大面积失效,Redis崩溃,数据请求全部打到MySql上,使MySql崩溃。
举个例子,双十一抢购,庞大的数据请求在同一时间对淘宝发起请求,数据请求量超过Redis缓存量,用户的请求全部打到数据库上面,数据库全面崩溃,当数据库重启,新的数据又到了,数据库又崩溃。
那出现这种情况怎么解决呢?
解决方案:
批量往redis存数据的时候,把每个key的失效时间加上个随机数,这样的话就能保证数据不会在同一个时间大面积失效。
2、Redis缓存穿透:
情况:用户请求一个数据库和缓存中都不存在的数据。
正常使用缓存的流程是,先对缓存进行查询,如何缓存中的key值过期或是不存在,再去数据库进行查询,并把查询到的数据放在缓存中,如果查询的对象为空,则不放入缓存。
解决方案:
- 在接口层增加校验,不合法的参数直接返回。不相信任务调用方,根据自己提供的API接口规范来,作为被调用方,要考虑可能任何的参数传值。
- 在缓存查不到,DB中也没有的情况,可以将对应的key的value写为null,或者其他特殊值写入缓存,同时将过期失效时间设置短一点,比如60秒,以免影响正常情况。这样是可以防止反复用同一个ID来暴力攻击。
- 高级用户布隆过滤器(Bloom Filter),这个也能很好地防止缓存穿透。原理就是利用高效的数据结构和算法快速判断出你这个Key是否在DB中存在,不存在你return就好了,存在你就去查了DB刷新KV再return。
3、Redis缓存击穿:
情况:缓存击穿,是指一个key非常热点,在不停的扛着大并发,大并发集中对这一个点进行访问,当这个key在失效的瞬间,持续的大并发就穿破缓存,直接请求数据库,就像在一个屏障上凿开了一个洞。
解决方案:
- 对热点key,加互斥锁
- 设置热点数据永不过期
缓存雪崩、穿透和击穿,是缓存最大的问题,要么不出现,一旦出现就是致命性的问题。所以一定要谨慎对待,当然,面试的时候也是常考点。