Lock screen interview questions – redis (I)

Time:2021-10-27

Lock screen interview questions - redis (I)
Lock screen interview questions will be brushed every day, and the interview questions will be updated every working day. The lock screen interview question app and applet are now online. The official website address is:https://www.demosoftware.cc/#…。 It has included all the contents of the interview questions updated every day, as well as the features of unlocking the screen to review the interview questions, daily programming questions, e-mail push and so on. Let you take the lead in the interview and hang the interviewer! Next is today’s interview question:

====What are the characteristics of redis?

Redis is a memory cache database developed by Italian Salvatore Sanfilippo (net name: antirez). The full name of redis is remote dictionary server (remote data service). The software is written in C language. It is a typical NoSQL database server. Redis is a key value storage system that supports rich data types, such as string, list, set, Zset (sorted set) and hash.
Redis is essentially a key value type memory database, much like memcached. The entire database is loaded into memory for operation, and the database data is periodically flushed to the hard disk for storage through asynchronous operation. Because it is a pure memory operation, redis has excellent performance. It can handle more than 100000 read and write operations per second. It is the fastest known key value dB.
Redis’s excellence lies not only in its performance, but also in its support of saving multiple data structures. In addition, the maximum limit of a single value is 1GB, unlike memcached, which can only save 1MB of data. In addition, redis can set the expiration time for the stored key values.
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 suitable scenarios for redis are mainly limited to high-performance operations and operations with a small amount of data.

====Why does redis need to put all data in memory?

In order to achieve the fastest reading and writing speed, redis reads data into memory and writes data to disk asynchronously. Therefore, redis has the characteristics of fast and data persistence. If the data is not placed in memory, the disk I / O speed will be slow, which will seriously affect the performance of redis. With memory getting cheaper and cheaper today, redis will become more and more popular. If the maximum memory used is set, the new value cannot be inserted after the number of existing data records reaches the memory limit.

====What are the common performance problems of redis? How to solve it?

(1) The 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 large, it will have a great impact on the performance, and the service will be suspended intermittently. Therefore, the master should not write a memory snapshot.
(2) Master AOF persistence. If the AOF file is not rewritten, this persistence method will have the least impact on performance, but the AOF file will continue to grow. Too many AOF files will affect the recovery speed of master restart. The master should not do any persistence work, including memory snapshot and AOF log files. In particular, do not enable memory snapshot for persistence. If the data is critical, a slave starts AOF backup data, and the strategy is to synchronize once per second.
(3) . the master calls bgrewriteaof to rewrite the AOF file. The AOF will occupy a lot of CPU and memory resources during rewriting, resulting in excessive service load and temporary service suspension.
(4) The performance of redis master-slave replication. For the speed of master-slave replication and the stability of connection, slave and master should be in the same LAN.

====What are the most suitable scenarios for redis?
(1) , session cache
(2) , full page cache (FPC)
(3) , queue
(4) , leaderboard / counter
(5) , publish / subscribe

====What are the differences between Memcache and redis?
(1) The storage methods are different. Memcache stores all data in memory. The data cannot exceed the size of memory. The database will hang up after power failure. Some redis are stored on the hard disk, which can ensure the persistence of data.
(2) The types of data supported are different. Memcahe supports relatively simple data types, and redis has complex data types.
(3) Different underlying models are used, and their underlying implementation methods and application protocols for communication with clients are different. Redis has directly built its own VM mechanism, because the general system will waste some time to move and request when calling system functions.
(4) . the supported value sizes are different. The maximum size of redis can reach 1GB, while Memcache is only 1MB.

====What data structures does redis have? And how to use it?

Redis has five data structures:
String — string
The string data structure is a simple key value type. Value can be either a string or a number (when the number type can be represented by long, encoding is an integer, and others are stored in sdshdr as strings).
Usage: get, set, del, incr, decr, etc
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
“world”
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> get hello
(nil)
127.0.0.1:6379> get counter
“2”
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> get counter
“3”
127.0.0.1:6379> incrby counter 100
(integer) 103
127.0.0.1:6379> get counter
“103”
127.0.0.1:6379> decr counter
(integer) 102
127.0.0.1:6379> get counter
“102”
Hash – Dictionary
In memcached, we often package some structured information into HashMap, which is serialized on the client and stored as a string value (usually JSON format), such as the user’s nickname, age, gender, points, etc.
Use: all hash commands are hget, hset, HDEL, etc. starting with H
127.0.0.1:6379> hset user name1 hao
(integer) 1
127.0.0.1:6379> hset user email1 mailto:[email protected]
(integer) 1
127.0.0.1:6379> hgetall user
1) “name1”
2) “hao”
3) “email1”
4) “hmailto:[email protected]m”
127.0.0.1:6379> hget user user
(nil)
127.0.0.1:6379> hget user name1
“hao”
127.0.0.1:6379> hset user name2 xiaohao
(integer) 1
127.0.0.1:6379> hset user email2 mailto:[email protected]
(integer) 1
127.0.0.1:6379> hgetall user
1) “name1”
2) “hao”
3) “email1”
4) “hmailto:[email protected]m”
5) “name2”
6) “xiaohao”
7) “email2”
8) “xmailto:[email protected]m”
List – List
List is simply a linked list (a list implemented by redis using a double ended linked list). I believe anyone who has learned about data structure should be able to understand its structure.
use:
Lpush + lpop = stack
Lpush + rpop = queue (queue)
Lpush + ltrim = capped collection
Lpush + brpop = message queue
127.0.0.1:6379> lpush mylist 1 2 ll ls mem
(integer) 5
127.0.0.1:6379> lrange mylist 0 -1
1) “mem”
2) “ls”
3) “ll”
4) “2”
5) “1”
Set — Set
A set is a set. The concept of a set is a combination of non repeating values. Using the set data structure provided by redis, some sets can be stored
Usage: all commands are Sadd, SREM, scard, smembers, and sismber data starting with S.
127.0.0.1:6379> sadd myset hao hao1 xiaohao hao
(integer) 3
127.0.0.1:6379> SMEMBERS myset
1) “xiaohao”
2) “hao1”
3) “hao”
127.0.0.1:6379> SISMEMBER myset hao
(integer) 1
Zset — ordered set
Compared with set, Zset adds a weight parameter score to the elements in set, so that the elements in the set can be orderly arranged according to score,
Use: the commands for ordered sets start with Z. zadd, zrange, zscore
127.0.0.1:6379> zadd myscoreset 100 hao 90 xiaohao
(integer) 2
127.0.0.1:6379> ZRANGE myscoreset 0 -1
1) “xiaohao”
2) “hao”
127.0.0.1:6379> ZSCORE myscoreset hao
“100”

More interview questions can be concerned about “demo lock screen interview questions” official account through small programs or App to obtain interview questions and learning resources.
Lock screen interview questions - redis (I)