Lock screen interview questions – redis (I)


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> set hello world
OK> get hello
“world”> del hello
(integer) 1> get hello
(nil)> get counter
“2”> incr counter
(integer) 3> get counter
“3”> incrby counter 100
(integer) 103> get counter
“103”> decr counter
(integer) 102> get counter
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> hset user name1 hao
(integer) 1> hset user email1 mailto:[email protected]
(integer) 1> hgetall user
1) “name1”
2) “hao”
3) “email1”
4) “hmailto:[email protected]m”> hget user user
(nil)> hget user name1
“hao”> hset user name2 xiaohao
(integer) 1> hset user email2 mailto:[email protected]
(integer) 1> 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.
Lpush + lpop = stack
Lpush + rpop = queue (queue)
Lpush + ltrim = capped collection
Lpush + brpop = message queue> lpush mylist 1 2 ll ls mem
(integer) 5> 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.> sadd myset hao hao1 xiaohao hao
(integer) 3> SMEMBERS myset
1) “xiaohao”
2) “hao1”
3) “hao”> 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> zadd myscoreset 100 hao 90 xiaohao
(integer) 2> ZRANGE myscoreset 0 -1
1) “xiaohao”
2) “hao”> ZSCORE myscoreset hao

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)