Home > headlines > body text

Summary of Redis interview questions, let's learn about it

Release: 2020-08-05 15:54:02
forward
17319 people have browsed it

It’s the peak season for spring recruitment and job hopping again. Are all programmers unable to bear the thought of typing code and starting to move towards a salary increase? Today I will summarize the redis interview questions for you!

Topic recommendation: 2020 redis interview questions (latest)

Summary of Redis interview questions, let's learn about it

##1. What is redis?

Redis is a memory-based high-performance key-value database. (Recommendation 1:

Redis video tutorial) (Recommendation 2: mysql tutorial)

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 at regular intervals. 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 doubly linked list, implementing 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.

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 caching, messaging, setting expiration time by key, it 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’s support for data types is relatively simple. 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.Redis common performance problems and solutions:  

1).Master writes a memory snapshot, and the save command schedules the rdbSave function, which will block the work of the main thread. 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 strategy 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 that is about to expire from the data set (server.db[i].expires) with an expiration time set for elimination

volatile-random: Randomly select data for elimination from the data set (server.db[i].expires) with expiration time set

allkeys-lru: From the data set (server.db[i] .dict) to select the least recently used data for elimination

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

no-enviction (eviction ): Prohibit eviction of data

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

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

In order to achieve the fastest reading and writing speed, Redis reads all data into memory. , and writes data 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 They 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 connections are pooled, and at the same time, the client reads and writes Redis operations. The internal lock is 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 transactions in Redis:

1). In transactions All commands will be executed serially and sequentially. During the execution of the transaction, Redis will not 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
Copy after login

The above code can only guarantee that the execution result is correct in the case of a single connection, because if there are multiple clients executing this code at the same time, then An error scenario that often occurs in multi-threaded programs - race condition (race condition) occurs. For example, both clients A and B read the original value of mykey at the same time. Assume that the value is 10. After that, both clients add one to the value and set it back to the Redis server. This will cause the value of mykey to be lost. The result is 11, not 12 as we thought. In order to solve similar problems, we need the help of the WATCH command, see the following code:

WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC
Copy after login

The difference from the previous code is that the new code monitors the key through the WATCH command before obtaining the value of mykey, and then The set command is also surrounded by a transaction, which can effectively ensure that before each connection executes EXEC, if the value of mykey obtained by the current connection is modified by other connected clients, the EXEC command of the current connection will fail to execute. In this way, the caller can know whether val has been successfully reset after judging the return value.

14. Several methods of redis persistence

1. Snapshots

By default, Redis stores data snapshots In the binary file on disk, the file name is dump.rdb. You can configure the persistence strategy of Redis. For example, if there are more than M updates in the data set every N seconds, the data will be written to the disk; or you can manually call the command SAVE or BGSAVE.

working principle

. Redis forks.

. The child process starts writing data to the temporary RDB file.

. When the child process finishes writing the RDB file, replace the old file with the new file.

. This method allows Redis to use copy-on-write technology.

2. AOF

The snapshot mode is not very robust. When the system stops or Redis is accidentally killed, the last data written to Redis will be lost. This may not be a big problem for some applications, but for applications that require high reliability,

Redis is not a suitable choice.

Append-only file mode is another option.

You can turn on the AOF mode in the configuration file

3. Virtual memory mode

When your key is small and the value is large, the effect of using VM will be better Good. Because the memory saved in this way is relatively large.

When your key is not small, you can consider using some extraordinary methods to turn a large key into a large value. For example, you can consider combining key and value. into a new value.

vm-max-threads This parameter can set the number of threads that access the swap file. It is best not to exceed the number of cores of the machine. If it is set to 0, then all access to the swap file will The operations are all serial. It may cause a long delay, but the data integrity is well guaranteed.

When I tested it myself, I found that the performance of using virtual memory is also good. If the amount of data is large, you can consider distributed or other databases

15. redis’s cache invalidation strategy and primary key invalidation mechanism

As a cache system, they must be regularly cleaned and invalidated Data requires a primary key invalidation and elimination strategy.

In Redis, keys with a lifetime are called volatile. When creating a cache, set the lifetime for a given key. When the key expires (the lifetime is 0), it may be deleted.

1. Some operations that affect the survival time

The survival time can be removed by using the DEL command to delete the entire key, or the original data can be overwritten by the SET and GETSET commands, that is, After modifying the value corresponding to the key and overwriting it with the same key and value, the survival time of the current data is different.

For example, executing the INCR command on a key, executing the LPUSH command on a list, or executing the HSET command on a hash table, these operations will not modify the survival time of the key itself. On the other hand, if you use RENAME to rename a key, the survival time of the renamed key will be the same as before the rename.

Another possibility of the RENAME command is to try to rename a key with a survival time to another_key with a survival time. At this time, the old another_key (and its survival time) will be deleted, and then The old key will be renamed another_key. Therefore, the survival time of the new another_key is the same as the original key. Use the PERSIST command to remove the key's lifetime without deleting the key, making the key a persistent key again.

2. How to update the survival time

You can execute the EXPIRE command on a key that already has a survival time, and the newly specified survival time will replace the old survival time. The accuracy of the expiration time has been controlled within 1ms, and the time complexity of primary key failure is O(1).

EXPIRE is used with the TTL command. TTL can view the current survival time of the key. Returns 1 if the setting is successful; returns 0 when the key does not exist or the survival time cannot be set for the key.

Maximum cache configuration

In redis, users are allowed to set the maximum memory size

server.maxmemory
Copy after login

默认为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
Copy after login

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

(5)、发布/订阅

最后(但肯定不是最不重要的)是Redis的发布/订阅功能。发布/订阅的使用场景确实非常多。我已看见人们在社交网络连接中使用,还可作为基于发布/订阅的脚本触发器,甚至用Redis的发布/订阅功能来建立聊天系统!(不,这是真的,你可以去核实)。

Redis提供的所有特性中,我感觉这个是喜欢的人最少的一个,虽然它为用户提供如果此多功能。

相关推荐:

Redis教程

2020年前端vue面试题大汇总(附答案)

Related labels:
source:PHP开源社区
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template