What data types does redis support?
- String: the most basic data type, binary safe string, maximum 512M
- List: a list of strings in order to be added
- Set: an unordered collection of strings with no duplicate elements
- Set: sorted strings
- A set of hash: key / value pairs
Is redis single process or single thread?
Redis is a single process and single thread. Redis uses queue technology to change concurrent access into serial access, eliminating the overhead of traditional database serial control.
Why is redis single threaded?
Multithreading will be designed to lock, and multithreading will be designed to switch threads and consume CPU. Because CPU will not be the bottleneck of redis, the most likely bottleneck of redis is machine memory or network bandwidth. Single thread can’t play multi-core CPU performance, but it can be solved by opening redis instance on a single machine.
Advantages of redis
- Fast. Because data is stored in memory, similar to HashMap, the advantage of HashMap is that the time complexity of search and operation is O (1)
- Support rich data types, support string, list, set, sorted set, hash
- Transactions are supported, and operations are atomic. The so-called atomicity means that all changes to data are executed or not
- Rich features: can be used for caching, messages, press key to set the expiration time, after expiration will be automatically deleted
What are the advantages of redis and memcached
- All the values of memcached are simple strings, and reids, as its substitute, supports richer data types
- Redis is much faster than memcached
- Redis can persist its data
- Redis supports data backup, that is, data backup in master / slave mode
What kind of data elimination strategies does redis have
In redis, users are allowed to set the maximum used memory size server.maxmemory When the size of redis memory dataset increases to a certain size, the data elimination strategy will be implemented
- Volatile LRU: pick the least recently used obsolescence from data sets that have been set to expire
- Volatile TTL: select the data to be expired from the set of expired data
- Volatile random: randomly select data from the data set that has been set to be expired
- Allkeys LRU: culling the least recently used data from a dataset
- All keys random
- Noenviction: prohibit data elimination
What persistence methods does redis support
- RDB persistence
The principle is to dump the data records of redis in memory to the RDB file on the disk
In the specified time interval, the snapshot of data set in memory is written to disk. The actual operation process is fork. First, the data set is written to the temporary file, and then the previous file is replaced and stored by binary compression.
- Aof (append only file) persistence
The principle is to write the redis operation log to the file by appending.
Every write and delete operation processed by the server is recorded in the form of log. The query operation is not recorded. It is recorded in the form of text. You can open the file to see the detailed operation record. When the server restarts, these commands are re executed to restore the original data. The AOF command appends and saves each write operation to the end of the file with reids protocol. Redis can also rewrite AOF files in the background, so that the volume of AOF files is not too large.
Advantages and disadvantages of two persistence methods of redis?
Advantages: RDB file compact, small size, fast network transmission, suitable for full replication; recovery speed is much faster than AOF. Of course, one of the most important advantages of RDB over AOF is that it has a relatively small impact on performance
Disadvantages: the fatal drawback of RDB file is that the persistence mode of RDB file and its data snapshot determine that it is impossible to do real-time persistence. However, with the increasing importance of data today, a large amount of data loss is often unacceptable, so AOF persistence is called the mainstream. In addition, RDB files need to meet the specific format and have poor compatibility.
Corresponding to RDB persistence, AOF has the advantages of supporting second level persistence and good compatibility, but its disadvantages are large file size, slow recovery speed and great impact on performance
How to choose redis persistence strategy?
Before introducing the persistence strategy, it is important to understand that whether it is RDB or AOF, the starting of persistence has to pay the cost of performance. Compared with RDB persistence, on the one hand, when bdsave performs fork operation, the redis main process will block; on the other hand, the subprocess writing data to the hard disk will also bring IO pressure; for AOF persistence, the frequency of writing data to the hard disk is greatly increased (seconds under everysec Policy), and the IO pressure is greater, which may cause AOF to append blocking files. In addition, the rewriting of AOF file is similar to basave of RDB, which will cause blocking of fork and IO pressure of subprocesses. Relatively speaking, because AOF writes data to the hard disk more frequently, it will have a greater impact on the performance of redis main process.
In the actual production environment, there are various persistence strategies according to the amount of data, the security requirements of applications for data, budget constraints, etc.; for example, do not use any persistence at all, use one of RDB or AOF, or enable RDB and AOF persistence by colleagues. In addition, the choice of persistence must be considered together with the master-slave strategy of redis, because master-slave replication and persistence have the same function of data backup, and the master and Slave Slave can choose the persistence scheme independently.
What is the master-slave replication model of redis cluster?
In order to make the cluster still available when some nodes fail or most nodes fail to communicate, the cluster uses the master-slave replication model, and each node will have n-1 replicas
Will any write operations be lost in redis cluster? Why?
Redis does not guarantee strong data consistency, which means that in practice, the cluster may lose write operations under specific conditions
How are redis clusters replicated
How does redis optimize memory
Use hashes as much as possible. The memory of hash table (that is, the number of stored in the list) is very small. Therefore, you should abstract your data model into a hash table as much as possible. For example, there is a user object in your web system. Do not set a separate key for the user’s name, surname, mailbox, password, but should set this user All information is stored in a hash table
How does the redis recycle process work?
When a client runs a new command and adds new data, redis will check the memory usage. If it is greater than the maxmemory limit, it will recycle according to the set policy
Common usage scenarios of redis
- Session sharing (single sign on)
- Page caching
- Leaderboard / calculator
The above is the detailed content of the interview questions of redis. For more information about redis interview questions, please pay attention to other related articles of developeppaer!