Redis Essentials

1. What are the benefits of using redis there?

  1. Speed, because the data stored in memory, similar to HashMap, HashMap advantage is the time complexity of finding and operations are O (1)

  2. Support for rich data types, support string, list, set, sorted set, hash

  3. Support services, operations are atomic, so-called atomic changes to the data that is either all executed or not executed all

  4. Rich Features: can be used for caching, message, press the key to set the expiration time, will be automatically deleted after the expiration

2, redis memcached What are the advantages compared to?

  1. All values ​​are simple strings memcached, redis as its replacement, support richer data types

  2. redis speed much faster than memcached

  3. persistent data which may redis

3, redis common performance problems and solutions:

  1. Master best not to do any persistent work, such as RDB memory snapshots and log files AOF

  2. If the data is more important, a Slave open AOF backup data, policy is set to sync once per second

  3. In order to replicate the master speed and stability, Master and Slave connection is preferably the same LAN

  4. Try to avoid increase from the library a lot of pressure on the main library

  5. Not copied from the master structure using FIG., One-way linked list structure is more stable, i.e.: Master <- Slave1 <- Slave2 <- Slave3 ...

Such a structure convenient single point of failure to solve the issue and realize the replacement of Master of Slave. If the Master hung up, you can turn immediately Slave1 do Master, the other unchanged.

4, redis most appropriate scene

Redis most suitable for all data in-momory scene, although Redis also provide lasting capabilities, but is actually more of a disk-backed function, with persistence in the traditional sense has a relatively large difference, then we will probably have questions, it seems more like a Redis enhanced version of Memcached, then when to use Memcached, Redis when to use it?

If you simply compare the difference between Redis and Memcached, most will get the following view:

  1. Redis not only support simple k / v types of data, while also providing a storage list, set, zset, hash and other data structures.

  2. Redis supports backup data, i.e., data backup master-slave mode.

  3. Redis supports data persistence, data in memory can be kept on disk, restart when you can load be used again.

(1) the session cache (Session Cache) The most common scenario is using Redis session cache (session cache). Redis caching session with than other storage (such as Memcached) has the advantage of: Redis provide persistence. When not strictly required to maintain a cache coherency, if the user's shopping cart information lost, most people will be unhappy, and now, they will do so? Fortunately, with the improvement of Redis these years, it's easy to find the right document how to use Redis caching session. Even well-known business platform Magento also offers Redis plug-ins.

(2) full-page cache (FPC) in addition to the basic session token, Redis also provides a very convenient platform for FPC. Back consistency, even restart Redis instance, because of the persistent disk, users will not see a decrease page loading speed, which is a great improvement, similar to PHP local FPC. Magento again as an example, Magento offers a plug-in to use Redis as a full-page cache backend. In addition, the WordPress user, Pantheon has a great plugin wp-redis, this plugin can help you with the fastest speed of loading pages you've visited.

(3) a queue memory Reids a big advantage in the art to provide a storage engine list and set operations, which makes Redis can serve as a good platform to use the message queue. Redis used as an operation queue, similar to native language (e.g., Python) push on the list / pop operations. If you do a quick search for "Redis queues" in Google, you'll be able to find a large number of open source projects, the purpose of these projects is to use Redis create a very good back-end tools to meet the needs of various queues. For example, Celery has a background is to use Redis as a broker, you can go to see from here.

(4) Ranking / counter Redis of numbers increment or decrement operation to achieve very good in memory. Collection (Set) and an ordered set (Sorted Set) also allows us the time to perform these operations become very simple, Redis just provide just these two data structures. So, we need to sort the collection to get the highest ranked 10 users - what we call "user_scores", we just need to execute something like the following: Of course, this assumes you are doing based on your user's score ascending order. If you want to return the user and the user's score, you need to perform: ZRANGE user_scores 0 10 WITHSCORES Agora Games is a good example, implemented in Ruby, its ranking is to use Redis to store data, you can here see.

(5) Publish / Subscribe Last (but certainly not least) is Redis publish / subscribe functionality. Publish / Subscribe usage scenario is indeed very much. I have seen people use social networking connections, but also to build a chat system based publish / subscribe Script Triggers, and even publish Redis / subscribe functionality! (No, it's true, you can go to verify).

Redis offers all the features, I feel like this is the least of a person, although it provides users if this multifunction.

5, redis some of the other features

(1) Redis is single-threaded, single-process redis queuing technologies will become concurrent access serial access, eliminating the overhead of traditional database Serial Control

(2) by increasing the number of separate read and write model Slave DB, the read performance can be linear growth. To avoid single points of failure of the Master DB, generally using two cluster Master DB do hot standby, so the availability of the entire cluster of reading and writing are very high. Defects separate read and write architecture that whether it is Master or Slave, each node must preserve the integrity of the data, if in the case of large amount of data, scalability cluster storage capacity is limited to a single node, but also for write-intensive type of application, the architecture is not suitable for separate read and write.

(3) In order to solve the model data piece separate read and write defect model, the model may be applied data piece to come. Each node can be thought of are independent master, and implement service data piece through. The above combination of the two models, each of the master may be designed to model consists of a plurality of master and slave thereof.

(4) Redis recovery strategy

  1. volatile-lru: selection of the least recently used data out of the set of data set expiration time (server.db [i] .expires) in

  2. volatile-ttl: selection of data to be expired expiration time has been set out from the data set (server.db [i] .expires) in

  3. volatile-random: the expiration time has been set from the data set (server.db [i] .expires) arbitrarily selected out of the data

  4. allkeys-lru: selection of the least recently used data out from the data set (server.db [i] .dict) in

  5. allkeys-random: selecting out data from the data set (server.db [i] .dict) any

  6. no-enviction (expulsion): prohibits the expulsion data

Note that the six kinds of mechanisms, volatile and allkeys provides is a set expiration time of data collection or data out of the data from the entire data set out of the back of the lru, ttl and random three different phase-out strategy, coupled with a kind of no-enviction never recovered strategy.

Use policy rules:

  1. If the data is presented power-law distribution, which is part of the data access to high-frequency, low-frequency part of the data access, use allkeys-lru

  2. If the data is rendered equal distribution, that is, all data access frequency are the same, using allkeys-random

6, mySQL there 2000w data

redis only 20w of data stored, how to ensure that data is hot data in redis 

Knowledge: redis-memory data set size up to a certain size and they will perform data elimination strategy. redis offers six data out of a strategy see above

7, if there are 100 million Redis key

, Which has a key 10w begins with a fixed known prefix, if all of them to find out? 

Use keys instructions may sweep out a list of the specified pattern key.

对方接着追问:如果这个redis正在给线上的业务提供服务,那使用keys指令会有什么问题?

这个时候你要回答redis关键的一个特性:redis的单线程的。keys指令会导致线程阻塞一段时间,线上服务会停顿,直到指令执行完毕,服务才能恢复。这个时候可以使用scan指令,scan指令可以无阻塞的提取出指定模式的key列表,但是会有一定的重复概率,在客户端做一次去重就可以了,但是整体所花费的时间会比直接用keys指令长。

8、Redis 常见的性能问题都有哪些?如何解决?

  1. Master写内存快照,save命令调度rdbSave函数,会阻塞主线程的工作,当快照比较大时对性能影响是非常大的,会间断性暂停服务,所以Master最好不要写内存快照。

  2. Master AOF持久化,如果不重写AOF文件,这个持久化方式对性能的影响是最小的,但是AOF文件会不断增大,AOF文件过大会影响Master重启的恢复速度。Master最好不要做任何持久化工作,包括内存快照和AOF日志文件,特别是不要启用内存快照做持久化,如果数据比较关键,某个Slave开启AOF备份数据,策略为每秒同步一次。

  3. Master调用BGREWRITEAOF重写AOF文件,AOF在重写的时候会占大量的CPU和内存资源,导致服务load过高,出现短暂服务暂停现象。

  4. Redis主从复制的性能问题,为了主从复制的速度和连接的稳定性,Slave和Master最好在同一个局域网内

9、Redis有哪些数据结构?

字符串String、字典Hash、列表List、集合Set、有序集合SortedSet。

如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog、Geo、Pub/Sub。

如果你说还玩过Redis Module,像BloomFilter,RedisSearch,Redis-ML,面试官得眼睛就开始发亮了。

10、使用过Redis分布式锁么,它是什么回事?

先拿setnx来争抢锁,抢到之后,再用expire给锁加一个过期时间防止锁忘记了释放。

这时候对方会告诉你说你回答得不错,然后接着问如果在setnx之后执行expire之前进程意外crash或者要重启维护了,那会怎么样?

这时候你要给予惊讶的反馈:唉,是喔,这个锁就永远得不到释放了。紧接着你需要抓一抓自己得脑袋,故作思考片刻,好像接下来的结果是你主动思考出来的,然后回答:我记得set指令有非常复杂的参数,这个应该是可以同时把setnx和expire合成一条指令来用的!对方这时会显露笑容,心里开始默念:摁,这小子还不错。

11、使用过Redis做异步队列么,你是怎么用的?

一般使用list结构作为队列,rpush生产消息,lpop消费消息。当lpop没有消息的时候,要适当sleep一会再重试。

如果对方追问可不可以不用sleep呢?list还有个指令叫blpop,在没有消息的时候,它会阻塞住直到消息到来。

如果对方追问能不能生产一次消费多次呢?使用pub/sub主题订阅者模式,可以实现1:N的消息队列。

如果对方追问pub/sub有什么缺点?在消费者下线的情况下,生产的消息会丢失,得使用专业的消息队列如rabbitmq等。

如果对方追问redis如何实现延时队列?我估计现在你很想把面试官一棒打死如果你手上有一根棒球棍的话,怎么问的这么详细。但是你很克制,然后神态自若的回答道:使用sortedset,拿时间戳作为score,消息内容作为key调用zadd来生产消息,消费者用zrangebyscore指令获取N秒之前的数据轮询进行处理。

到这里,面试官暗地里已经对你竖起了大拇指。但是他不知道的是此刻你却竖起了中指,在椅子背后。

12、如果有大量的key需要设置同一时间过期,一般需要注意什么?

如果大量的key过期时间设置的过于集中,到过期的那个时间点,redis可能会出现短暂的卡顿现象。一般需要在时间上加一个随机值,使得过期时间分散一些。

13、为什么Redis需要把所有数据放到内存中?

Redis为了达到最快的读写速度将数据都读到内存中,并通过异步的方式将数据写入磁盘。所以redis具有快速和数据持久化的特征。如果不将数据放在内存中,磁盘I/O速度为严重影响redis的性能。在内存越来越便宜的今天,redis将会越来越受欢迎。如果设置了最大使用的内存,则数据已有记录数达到内存限值后不能继续插入新值。

14、Redis 持久化机制

bgsave做镜像全量持久化,aof做增量持久化。因为bgsave会耗费较长时间,不够实时,在停机的时候会导致大量丢失数据,所以需要aof来配合使用。在redis实例重启时,会使用bgsave持久化文件重新构建内存,再使用aof重放近期的操作指令来实现完整恢复重启之前的状态。

对方追问那如果突然机器掉电会怎样?取决于aof日志sync属性的配置,如果不要求性能,在每条写指令时都sync一下磁盘,就不会丢失数据。但是在高性能的要求下每次都sync是不现实的,一般都使用定时sync,比如1s1次,这个时候最多就会丢失1s的数据。

对方追问bgsave的原理是什么?你给出两个词汇就可以了,fork和cow。fork是指redis通过创建子进程来进行bgsave操作,cow指的是copy on write,子进程创建后,父子进程共享数据段,父进程继续提供读写服务,写脏的页面数据会逐渐和子进程分离开来。

15、Redis提供了哪几种持久化方式?

  1. RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储。

  2. AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据,AOF命令以redis协议追加保存每次写的操作到文件末尾。Redis还能对AOF文件进行后台重写,使得AOF文件的体积不至于过大。

  3. 如果你只希望你的数据在服务器运行的时候存在,你也可以不使用任何持久化方式。

  4. 你也可以同时开启两种持久化方式, 在这种情况下, 当redis重启的时候会优先载入AOF文件来恢复原始的数据,因为在通常情况下AOF文件保存的数据集要比RDB文件保存的数据集要完整。

  5. 最重要的事情是了解RDB和AOF持久化方式的不同,让我们以RDB持久化方式开始。

16、如何选择合适的持久化方式?

一般来说, 如果想达到足以媲美PostgreSQL的数据安全性, 你应该同时使用两种持久化功能。如果你非常关心你的数据, 但仍然可以承受数分钟以内的数据丢失,那么你可以只使用RDB持久化。

有很多用户都只使用AOF持久化,但并不推荐这种方式:因为定时生成RDB快照(snapshot)非常便于进行数据库备份, 并且 RDB 恢复数据集的速度也要比AOF恢复的速度要快,除此之外, 使用RDB还可以避免之前提到的AOF程序的bug。郑州不孕不育医院:http://jbk.39.net/yiyuanzaixian/zztjyy/

17、Pipeline有什么好处,为什么要用pipeline?

可以将多次IO往返的时间缩减为一次,前提是pipeline执行的指令之间没有因果相关性。使用redis-benchmark进行压测的时候可以发现影响redis的QPS峰值的一个重要因素是pipeline批次指令的数目。http://www.renrendoc.com/p-21948241.html

18、Redis的同步机制了解么?

Redis可以使用主从同步,从从同步。第一次同步时,主节点做一次bgsave,并同时将后续修改操作记录到内存buffer,待完成后将rdb文件全量同步到复制节点,复制节点接受完成后将rdb镜像加载到内存。加载完成后,再通知主节点将期间修改的操作记录同步到复制节点进行重放就完成了同步过程。

19、Redis 集群方案与实现

Redis Sentinal着眼于高可用,在master宕机时会自动将slave提升为master,继续提供服务。

Redis Cluster着眼于扩展性,在单个redis内存不足时,使用Cluster进行分片存储。

20、一个Redis实例最多能存放多少的keys?

List、Set、Sorted Set他们最多能存放多少元素? 

理论上Redis可以处理多达232的keys,并且在实际中进行了测试,每个实例至少存放了2亿5千万的keys。我们正在测试一些较大的值。

任何list、set、和sorted set都可以放232个元素。

换句话说,Redis的存储极限是系统中的可用内存值。

21、Redis持久化数据和缓存怎么做扩容?

  • 如果Redis被当做缓存使用,使用一致性哈希实现动态扩容缩容。

  • 如果Redis被当做一个持久化存储使用,必须使用固定的keys-to-nodes映射关系,节点的数量一旦确定不能变化。否则的话(即Redis节点需要动态变化的情况),必须使用可以在运行时进行数据再平衡的一套系统,而当前只有Redis集群可以做到这样。


Guess you like

Origin blog.51cto.com/14510269/2438328