Home  >  Article  >  Database  >  Some common redis interview questions (with answers)

Some common redis interview questions (with answers)

尚
forward
2019-12-09 17:41:126754browse

Some common redis interview questions (with answers)

1. What is redis?

Redis is a high-performance key-value database based on memory.

Special topic recommendation: 2020 redis interview questions (latest)

2. Characteristics of Reids

Redis is essentially a Key-Value type in-memory database, much like memcached. The entire database is loaded into the memory for operation, and the database data is flushed to the hard disk for storage through asynchronous operations on a regular basis. Because it is a pure memory operation, Redis has excellent performance and can handle more than 100,000 read and write operations per second. It is the fastest Key-Value DB known to perform.

The excellence of Redis is not only its performance. The biggest charm of Redis is that it supports saving a variety of data structures. In addition, the maximum limit of a single value is 1GB. Unlike memcached, which can only save 1MB of data, Redis can It is used to implement many useful functions, such as using its List to make a FIFO two-way linked list to implement a lightweight high-performance message queue service, and using its Set to make a high-performance tag system. etc. In addition, Redis can also set the expire time for the stored Key-Value, so it can also be used as an enhanced version of memcached.

The main disadvantage of Redis is that the database capacity is limited by physical memory and cannot be used for high-performance reading and writing of massive data. Therefore, the scenarios suitable for Redis are mainly limited to high-performance operations and calculations of smaller amounts of data.

Related learning recommendations:
redis video tutorial

3. What are the benefits of using redis?

 (1) It is fast because the data is stored in memory, similar to HashMap. The advantage of HashMap is that the time complexity of search and operation is O(1)

(2 ) Support rich data types, support string, list, set, sorted set, hash

(3) Support transactions, operations are atomic, the so-called atomicity means that all changes to the data are executed or all Not executed

(4) Rich features: can be used for cache, messages, set expiration time by key, and will be automatically deleted after expiration

4. What are the advantages of redis compared to memcached? Advantage?

 (1) All values ​​in memcached are simple strings. As its replacement, redis supports richer data types

(2) Redis is faster than memcached Much faster

(3) redis can persist its data

5. What are the differences between Memcache and Redis?

1). Storage method Memecache stores all data in the memory. It will hang up after a power outage. The data cannot exceed the memory size. Part of Redis is stored on the hard disk, which ensures data persistence.

2), Data support type Memcache supports relatively simple data types. Redis has complex data types.

3), the underlying models used are different, the underlying implementation methods and the application protocols for communication with the client are different. Redis directly built its own VM mechanism, because if the general system calls system functions, it will waste a certain amount of time to move and request.

6. Common redis performance problems and solutions:

1). Master writes memory snapshots and the save command schedules the rdbSave function, which will block the work of the main thread. When When the snapshot is relatively large, it will have a great impact on performance, and the service will be suspended intermittently, so it is best not for the Master to write memory snapshots.

2). Master AOF persistence. If the AOF file is not rewritten, this persistence method will have the smallest impact on performance, but the AOF file will continue to grow. If the AOF file is too large, it will affect the recovery of the Master restart. speed. It is best not to do any persistence work on the Master, including memory snapshots and AOF log files. In particular, do not enable memory snapshots for persistence. If the data is critical, a Slave should enable AOF backup data, and the policy is to synchronize once per second.

3). Master calls BGREWRITEAOF to rewrite the AOF file. AOF will occupy a large amount of CPU and memory resources during rewriting, causing the service load to be too high and temporary service suspension.

4), Redis master-slave replication performance issues, for the speed of master-slave replication and the stability of the connection, it is best for Slave and Master to be in the same LAN

7, There are 20 million data in mySQL, but only 200,000 data are stored in redis. How to ensure that the data in redis are hot data

Related knowledge: When the size of the redis memory data set increases to a certain size, it will Implement a data obsolescence strategy (recycling strategy). redis provides 6 data elimination strategies:

volatile-lru: Select the least recently used data from the data set (server.db[i].expires) with an expiration time set to eliminate it

volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) with an expiration time set for elimination

volatile-random: Select the data set with an expiration time (server. db[i].expires) to eliminate any data

allkeys-lru: Select the least recently used data from the data set (server.db[i].dict) to eliminate

allkeys -random: Randomly select data from the data set (server.db[i].dict) for elimination

no-enviction (eviction): prohibiting eviction of data

8. Please use Redis and any language to implement a code for malicious login protection. Limit each user ID to a maximum of 5 logins within 1 hour. Second-rate. For specific login functions or functions, just use an empty function and there is no need to write them out in detail.

Implemented using a list: Each element in the list represents the login time. As long as the difference between the last 5th login time and the current time does not exceed 1 hour, login is prohibited. The code written in Python is as follows:

#!/usr/bin/env python3
import redis  
import sys  
import time  
 
r = redis.StrictRedis(host=’127.0.0.1′, port=6379, db=0)  
try:       
    id = sys.argv[1]
except:      
    print(‘input argument error’)    
    sys.exit(0)  
if r.llen(id) >= 5 and time.time() – float(r.lindex(id, 4)) <= 3600:      
    print(“you are forbidden logining”)
else:       
    print(‘you are allowed to login’)    
    r.lpush(id, time.time())    
    # login_func()

9. Why does redis need to put all data in memory?

Redis stores data in order to achieve the fastest reading and writing speed All are read into memory and written to disk asynchronously. So redis has the characteristics of fast speed and data persistence. If the data is not placed in memory, disk I/O speed will seriously affect the performance of redis. Today, when memory is getting cheaper and cheaper, redis will become more and more popular.

If the maximum memory used is set, new values ​​cannot be inserted after the number of data records reaches the memory limit.

10.Redis is a single process and single thread

redis uses queue technology to turn concurrent access into serial access, eliminating the overhead of traditional database serial control

11. How to solve the concurrency competition problem of redis?

Redis is a single-process single-thread mode and uses queue mode to turn concurrent access into serial access. Redis itself has no concept of locks. Redis does not compete for multiple client connections. However, when the Jedis client concurrently accesses Redis, problems such as connection timeout, data conversion errors, blocking, and client closing the connection may occur. These problems These are all caused by client connection confusion.

There are two solutions to this:

1. From the client perspective, in order to ensure that each client communicates with Redis in a normal and orderly manner, the connection is pooled and the client End-to-end read and write Redis operations use internal lock synchronized.

2. From the server perspective, use setnx to implement locking.

Note: For the first type, the application needs to handle the synchronization of resources by itself. The method that can be used is relatively popular, you can use synchronized or lock; the second type requires the use of Redis's setnx command, but it requires Pay attention to some issues.

12. Understanding of redis things CAS (check-and-set operation to implement optimistic locking)?

Like many other databases, Redis also provides a NoSQL database transaction mechanism. In Redis, the four commands MULTI/EXEC/DISCARD/WATCH are the cornerstone of our transaction implementation.

I believe this concept is not unfamiliar to developers with relational database development experience. Even so, we will briefly list the implementation characteristics of Redis transactions:

1) . All commands in the transaction will be executed serially and sequentially. During the execution of the transaction, Redis will no longer provide any services for other client requests, thus ensuring that all commands in the transaction are executed atomically.

2). Compared with transactions in relational databases, if a command fails to execute in a Redis transaction, subsequent commands will continue to be executed.

3). We can start a transaction through the MULTI command, which people with experience in relational database development can understand as the "BEGIN TRANSACTION" statement. The commands executed after this statement will be regarded as operations within the transaction. Finally, we can commit/rollback all operations within the transaction by executing the EXEC/DISCARD command. These two Redis commands can be regarded as equivalent to the COMMIT/ROLLBACK statement in a relational database.

4). Before the transaction is started, if there is a communication failure between the client and the server and the network is disconnected, all subsequent statements to be executed will not be executed by the server. However, if the network interruption event occurs after the client executes the EXEC command, then all commands in the transaction will be executed by the server.

5). When using the Append-Only mode, Redis will write all write operations in the transaction to the disk in this call by calling the system function write. However, if a system crash occurs during the writing process, such as a downtime caused by a power failure, then only part of the data may be written to the disk at this time, while other part of the data has been lost.

The Redis server will perform a series of necessary consistency checks when restarting. Once a similar problem is found, it will exit immediately and give a corresponding error prompt. At this time, we must make full use of the redis-check-aof tool provided in the Redis toolkit. This tool can help us locate data inconsistency errors and roll back some of the data that has been written. After the repair, we can restart the Redis server again.

13.WATCH command and CAS-based optimistic locking:

In Redis transactions, the WATCH command can be used to provide CAS (check-and-set) functionality. Assume that we monitor multiple Keys through the WATCH command before the transaction is executed. If the value of any Key changes after WATCH, the transaction executed by the EXEC command will be abandoned and a Null multi-bulk response will be returned to notify the caller of the transaction.

Execution failed. For example, we assume again that the incr command is not provided in Redis to complete the atomic increment of key values. If we want to implement this function, we can only write the corresponding code ourselves. The pseudo code is as follows:

val = GET mykey
val = val + 1
SET mykey $val

以上代码只有在单连接的情况下才可以保证执行结果是正确的,因为如果在同一时刻有多个客户端在同时执行该段代码,那么就会出现多线程程序中经常出现的一种错误场景--竞态争用(race condition)。

比如,客户端A和B都在同一时刻读取了mykey的原有值,假设该值为10,此后两个客户端又均将该值加一后set回Redis服务器,这样就会导致mykey的结果为11,而不是我们认为的12。为了解决类似的问题,我们需要借助WATCH命令的帮助,见如下代码:

WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC

和此前代码不同的是,新代码在获取mykey的值之前先通过WATCH命令监控了该键,此后又将set命令包围在事务中,这样就可以有效的保证每个连接在执行EXEC之前,如果当前连接获取的mykey的值被其它连接的客户端修改,那么当前连接的EXEC命令将执行失败。这样调用者在判断返回值后就可以获悉val是否被重新设置成功。

14.redis持久化的几种方式

1、快照(snapshots)
缺省情况情况下,Redis把数据快照存放在磁盘上的二进制文件中,文件名为dump.rdb。你可以配置Redis的持久化策略,例如数据集中每N秒钟有超过M次更新,就将数据写入磁盘;或者你可以手工调用命令SAVE或BGSAVE。

工作原理
 . Redis forks.
 . 子进程开始将数据写到临时RDB文件中。
 . 当子进程完成写RDB文件,用新文件替换老文件。
 . 这种方式可以使Redis使用copy-on-write技术。
2、AOF
快照模式并不十分健壮,当系统停止,或者无意中Redis被kill掉,最后写入Redis的数据就会丢失。这对某些应用也许不是大问题,但对于要求高可靠性的应用来说,Redis就不是一个合适的选择。Append-only文件模式是另一种选择。你可以在配置文件中打开AOF模式。

3、虚拟内存方式

当你的key很小而value很大时,使用VM的效果会比较好.因为这样节约的内存比较大.

当你的key不小时,可以考虑使用一些非常方法将很大的key变成很大的value,比如你可以考虑将key,value组合成一个新的value.

vm-max-threads这个参数,可以设置访问swap文件的线程数,设置最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的.可能会造成比较长时间的延迟,但是对数据完整性有很好的保证.

自己测试的时候发现用虚拟内存性能也不错。如果数据量很大,可以考虑分布式或者其他数据库

15.redis的缓存失效策略和主键失效机制

作为缓存系统都要定期清理无效数据,就需要一个主键失效和淘汰策略.

在Redis当中,有生存期的key被称为volatile。在创建缓存时,要为给定的key设置生存期,当key过期的时候(生存期为0),它可能会被删除。

1、影响生存时间的一些操作

生存时间可以通过使用 DEL 命令来删除整个 key 来移除,或者被 SET 和 GETSET 命令覆盖原来的数据,也就是说,修改key对应的value和使用另外相同的key和value来覆盖以后,当前数据的生存时间不同。

比如说,对一个 key 执行INCR命令,对一个列表进行LPUSH命令,或者对一个哈希表执行HSET命令,这类操作都不会修改 key 本身的生存时间。另一方面,如果使用RENAME对一个 key 进行改名,那么改名后的 key的生存时间和改名前一样。

RENAME命令的另一种可能是,尝试将一个带生存时间的 key 改名成另一个带生存时间的 another_key ,这时旧的 another_key (以及它的生存时间)会被删除,然后旧的 key 会改名为 another_key ,因此,新的 another_key 的生存时间也和原本的 key 一样。使用PERSIST命令可以在不删除 key 的情况下,移除 key 的生存时间,让 key 重新成为一个persistent key 。

2、如何更新生存时间

可以对一个已经带有生存时间的 key 执行EXPIRE命令,新指定的生存时间会取代旧的生存时间。过期时间的精度已经被控制在1ms之内,主键失效的时间复杂度是O(1),EXPIRE和TTL命令搭配使用,TTL可以查看key的当前生存时间。设置成功返回 1;当 key 不存在或者不能为 key 设置生存时间时,返回 0 。

最大缓存配置

在 redis 中,允许用户设置最大使用内存大小

server.maxmemory

默认为0,没有指定最大缓存,如果有新的数据添加,超过最大内存,则会使redis崩溃,所以一定要设置。redis 内存数据集大小上升到一定大小的时候,就会实行数据淘汰策略。

redis 提供 6种数据淘汰策略:

 . volatile-lru:从已设置过期时间的数据集(server.db[i].expires)中挑选最近最少使用的数据淘汰

 . volatile-ttl:从已设置过期时间的数据集(server.db[i].expires)中挑选将要过期的数据淘汰

 . volatile-random:从已设置过期时间的数据集(server.db[i].expires)中任意选择数据淘汰

 . allkeys-lru:从数据集(server.db[i].dict)中挑选最近最少使用的数据淘汰

 . allkeys-random:从数据集(server.db[i].dict)中任意选择数据淘汰

 . no-enviction(驱逐):禁止驱逐数据

注意这里的6种机制,volatile和allkeys规定了是对已设置过期时间的数据集淘汰数据还是从全部数据集淘汰数据,后面的lru、ttl以及random是三种不同的淘汰策略,再加上一种no-enviction永不回收的策略。

使用策略规则:

1、如果数据呈现幂律分布,也就是一部分数据访问频率高,一部分数据访问频率低,则使用allkeys-lru

2、如果数据呈现平等分布,也就是所有的数据访问频率都相同,则使用allkeys-random

三种数据淘汰策略:

ttl和random比较容易理解,实现也会比较简单。主要是Lru最近最少使用淘汰策略,设计上会对key 按失效时间排序,然后取最先失效的key进行淘汰

16.redis 最适合的场景  

Redis最适合所有数据in-momory的场景,虽然Redis也提供持久化功能,但实际更多的是一个disk-backed的功能,跟传统意义上的持久化有比较大的差别,那么可能大家就会有疑问,似乎Redis更像一个加强版的Memcached,那么何时使用Memcached,何时使用Redis呢?

如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:

1 、Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储。

2 、Redis支持数据的备份,即master-slave模式的数据备份。

3 、Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。

(1)、会话缓存(Session Cache)

最常用的一种使用Redis的情景是会话缓存(session cache)。用Redis缓存会话比其他存储(如Memcached)的优势在于:Redis提供持久化。当维护一个不是严格要求一致性的缓存时,如果用户的购物车信息全部丢失,大部分人都会不高兴的,现在,

他们还会这样吗?

幸运的是,随着 Redis 这些年的改进,很容易找到怎么恰当的使用Redis来缓存会话的文档。甚至广为人知的商业平台Magento也提供Redis的插件。

(2)、全页缓存(FPC)

除基本的会话token之外,Redis还提供很简便的FPC平台。回到一致性问题,即使重启了Redis实例,因为有磁盘的持久化,用户也不会看到页面加载速度的下降,这是一个极大改进,类似PHP本地FPC。

再次以Magento为例,Magento提供一个插件来使用Redis作为全页缓存后端。

此外,对WordPress的用户来说,Pantheon有一个非常好的插件 wp-redis,这个插件能帮助你以最快速度加载你曾浏览过的页面。
(3)、队列

Reids在内存存储引擎领域的一大优点是提供 list 和 set 操作,这使得Redis能作为一个很好的消息队列平台来使用。Redis作为队列使用的操作,就类似于本地程序语言(如Python)对 list 的 push/pop 操作。

如果你快速的在Google中搜索“Redis queues”,你马上就能找到大量的开源项目,这些项目的目的就是利用Redis创建非常好的后端工具,以满足各种队列需求。例如,Celery有一个后台就是使用Redis作为broker,你可以从这里去查看。

(4),排行榜/计数器

Redis在内存中对数字进行递增或递减的操作实现的非常好。集合(Set)和有序集合(Sorted Set)也使得我们在执行这些操作的时候变的非常简单,Redis只是正好提供了这两种数据结构。所以,我们要从排序集合中获取到排名最靠前的10个用户–我们称之为“user_scores”,我们只需要像下面一样执行即可:

当然,这是假定你是根据你用户的分数做递增的排序。如果你想返回用户及用户的分数,你需要这样执行:

ZRANGE user_scores 0 10 WITHSCORES

Agora Games就是一个很好的例子,用Ruby实现的,它的排行榜就是使用Redis来存储数据的,你可以在这里看到。

(5)、发布/订阅

Last (but certainly not least) is the publish/subscribe functionality of Redis. There are indeed many use cases for publish/subscribe. I've seen people use it in social network connections, as triggers for publish/subscribe based scripts, and even to build chat systems using Redis' publish/subscribe functionality! (No, this is true, you can check it out).

Of all the features provided by Redis, I feel that this is the one that people like the least, although it provides users with this multi-function.

Recommended: redis introductory tutorial

The above is the detailed content of Some common redis interview questions (with answers). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete