If you still don’t understand redis during the interview, take a look at these 40 interview questions (Part 1)

If you still don’t understand redis during the interview, take a look at these 40 interview questions (Part 1)

1. What is Redis?

Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database. Redis and other key-value caching products have the following three characteristics:

(1) Redis supports data persistence. It can save the data in the memory to the disk and load it again for use when restarting.

(2) Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.

(3) Redis supports data backup, that is, data backup in master-slave mode.

Redis advantages

(1) Extremely high performance – Redis can read at a speed of 110,000 times/s and write at a speed of 81,000 times/s.

(2) Rich data types – Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.

(3) Atomic - All operations of Redis are atomic, which means that they are either executed successfully or not executed at all. Individual operations are atomic. Multiple operations also support transactions, that is, atomicity, wrapped by the MULTI and EXEC instructions.

(4) Rich features – Redis also supports publish/subscribe, notifications, key expiration and other features.

How is Redis different from other key-value stores?

(1) Redis has more complex data structures and provides atomic operations on them. This is an evolutionary path different from other databases. Redis's data types are based on basic data structures and are transparent to programmers, without the need for additional abstractions.

(2) Redis runs in memory but can be persisted to disk, so memory needs to be weighed when performing high-speed reading and writing of different data sets, because the amount of data cannot be larger than the hardware memory. Another advantage of in-memory databases is that compared to the same complex data structures on disk, operating in memory is very simple, so Redis can do a lot of things with strong internal complexity. Also, in terms of disk format they are compact append-generated since they do not require random access.

2. What are the data types of Redis?

Answer: Redis supports five data types: string (string), hash (hash), list (list), set (set) and zsetsorted set: ordered set). The more commonly used ones in our actual projects are string and hash. If you are an advanced Redis user, you also need to add the following data structures: HyperLogLog, Geo, and Pub/Sub. If you say that you have played with Redis Module, such as BloomFilter, RedisSearch, and Redis-ML, the interviewer's eyes will start to shine.

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 O1) (2) It supports rich data types, including string, list, set, Zset, hash, etc. (3) Support transactions, operations are all atomic. The so-called atomicity means that all changes to the data are either executed or not executed at all. (4) Rich features: can be used for caching, messages, and setting expiration time by key. After expiration will be automatically deleted

4. What are the advantages of Redis compared to Memcached?

(1) All values ​​in Memcached are simple strings, and redis, as its replacement, supports richer data types (2) Redis is much faster than Memcached (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 power failure. The data cannot exceed the memory size. Redis is partially stored on the hard disk, which ensures data persistence. (2) Data support types 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 builds 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. Is Redis single-process and single-threaded?

Answer: 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.

7. What is the maximum capacity that a string type value can be stored in?

Answer: 512M

8. What is the persistence mechanism of Redis? What are the advantages and disadvantages of each?

Redis provides two persistence mechanisms: RDB and AOF mechanisms: 1), RDBRedis DataBase) persistence mode: refers to the use of data set snapshots (semi-persistent mode) to record all key-value pairs of the redis database, and at a certain point in time The data is written to a temporary file. After persistence is completed, this temporary file is used to replace the last persisted file to achieve data recovery. Advantages: (1) There is only one file dump.rdb, which is convenient for persistence. (2) Good disaster tolerance, a file can be saved to a safe disk. (3) To maximize performance, fork the child process to complete the write operation and let the main process continue to process commands, so IO is maximized. Use a separate sub-process for persistence, and the main process will not perform any IO operations, ensuring the high performance of redis) (4) When the data set is large, the startup efficiency is higher than AOF. Disadvantages: low data security. RDB is persisted at intervals. If redis fails between persistence, data loss will occur. Therefore, this method is more suitable when the data requirements are not strict. 2) AOFAppend-only file) persistence method: means that all command line records are fully persistently stored in the format of the redis command request protocol and are saved as aof files. Advantages: (1) Data security, aof persistence can be configured with the appendfsync attribute, with always, each command operation is recorded to the aof file. (2) Write files through append mode. Even if the server goes down in the middle, you can use the redis-check-aof tool to solve the data consistency problem. (3) Rewrite mode of AOF mechanism. Before the AOF file is rewritten (commands will be merged and rewritten when the file is too large), you can delete some of the commands (such as flushall by mistake)) Disadvantages: (1) AOF files are larger than RDB files, and recovery speed is slow. (2) When the data set is large, the startup efficiency is lower than rdb.

9. Redis common performance problems and solutions:

(1) Master is best not to write memory snapshots. If Master writes memory snapshots, the save command schedules the rdbSave function, which will block the work of the main thread. When the snapshot is relatively large, the impact on performance will be very large, and the service will be suspended intermittently (2 ) If the data is important, a Slave should enable AOF backup data, and the policy should be set to synchronize once per second (3) For the speed of master-slave replication and the stability of the connection, it is best for the Master and Slave to be on the same LAN (4) Try to avoid Add slaves to the master library under great pressure (5) Do not use a graph structure for master-slave replication. It is more stable to use a one-way linked list structure, that is: Master <- Slave1<- Slave2 <- Slave3... This structure is convenient Solve the single point of failure problem and realize the replacement of the Master by the Slave. If the Master hangs up, you can immediately enable Slave1 as the Master, leaving everything else unchanged.

10. What is the deletion strategy for redis expired keys?

(1) Scheduled deletion: While setting the expiration time of the key, create a timer (timer). Let the timer immediately perform the deletion operation on the key when the expiration time of the key comes.

(2) Lazy deletion: Let the key expire regardless, but every time you obtain a key from the key space, check whether the obtained key has expired. If it has expired, delete the key; if it has not expired, return the key.

(3) Periodic deletion: The program checks the database every once in a while and deletes the expired keys in it. It's up to the algorithm to decide how many expired keys to delete and how many databases to check.

11. Redis recycling strategy (elimination strategy)?

volatile-lru: Select the least recently used data from the data set with expiration time set (server.db[i].expires) to eliminate volatile-ttl: Eliminate the data set with expiration time set (server.db[i]. expires) to eliminate the data that will expire volatile-random: Select any data from the data set (server.db[i].expires) that has set the expiration time to eliminate allkeys-lru: From the data set (server.db[i] .dict) select the least recently used data to eliminate allkeys-random: randomly select data from the data set (server.db[i].dict) to eliminate no-enviction (eviction): prohibit eviction of data. Pay attention to the 6 mechanisms here. volatile and allkeys stipulate whether to eliminate data for data sets with set expiration times or to eliminate data from all data sets. The following lru, ttl and random are three different elimination strategies, plus a no-enviction that will never be recycled strategy.

Use policy rules:

(1) If the data shows a power law distribution, that is, some data have high access frequency and some data have low access frequency, use allkeys-lru

(2) If the data is equally distributed, that is, all data access frequencies are the same, use allkeys-random

12. Why does edis need to put all data in memory?

Answer: In order to achieve the fastest reading and writing speed, Redis reads all the data into the memory and writes the data to the 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 existing data records reaches the memory limit.

13. Do you understand the synchronization mechanism of Redis?

Answer: Redis can use master-slave synchronization and slave-slave synchronization. During the first synchronization, the primary node performs a bgsave and records subsequent modification operations to the memory buffer. After completion, the entire rdb file will be synchronized to the replica node. After the replica node accepts the data, it will load the rdb image into the memory. After the loading is completed, the master node is notified to synchronize the operation records modified during the period to the replica node for replay, and the synchronization process is completed.

14. What are the benefits of Pipeline? Why use pipeline?

Answer: The time of multiple IO round-trips can be reduced to one, provided there is no causal correlation between instructions executed by the pipeline. When using redis-benchmark for stress testing, it can be found that an important factor affecting the QPS peak value of redis is the number of pipeline batch instructions.

15. Have you ever used Redis cluster? What is the principle of cluster?

(1) Redis Sentinal focuses on high availability. When the master goes down, it will automatically promote the slave to the master and continue to provide services.

(2) Redis Cluster focuses on scalability. When a single redis memory is insufficient, Cluster is used for sharded storage.

16. Under what circumstances will the Redis cluster solution cause the entire cluster to become unavailable?

Answer: In a cluster with three nodes A, B, and C, without a replication model, if node B fails, the entire cluster will think that there is a lack of slots in the range of 5501-11000 and will be unavailable.

17. What Java clients are supported by Redis? Which one is officially recommended?

Answer: Redisson, Jedis, lettuce, etc. Redisson is officially recommended.

18. What are the advantages and disadvantages of Jedis and Redisson?

Answer: Jedis is a client implemented by Redis in Java, and its API provides relatively comprehensive support for Redis commands; Redisson implements distributed and scalable Java data structures. Compared with Jedis, its functions are simpler and does not support strings. Operations, Redis features such as sorting, transactions, pipelines, and partitions are not supported. The purpose of Redisson is to promote the separation of users' concerns from Redis, so that users can focus more on processing business logic.

19. How to set password and verify password in Redis?

Set password: config set requirepass 123456 Authorization password: auth 123456

20. Talk about the concept of Redis hash slot?

Answer: Redis cluster does not use consistent hashing, but introduces the concept of hash slots. Redis cluster has 16384 hash slots. Each key is checked by CRC16 and modulo 16384 is used to determine which slot to place. Each key in the cluster Each node is responsible for a part of the hash slot.

This article is published by the blog post platform OpenWrite!

Tang Xiaoou, founder of SenseTime, passed away at the age of 55 In 2023, PHP stagnated Wi-Fi 7 will be fully available in early 2024 Debut, 5 times faster than Wi-Fi 6 Hongmeng system is about to become independent, and many universities have set up “Hongmeng classes” Zhihui Jun’s startup company refinances , the amount exceeds 600 million yuan, and the pre-money valuation is 3.5 billion yuan Quark Browser PC version starts internal testing AI code assistant is popular, and programming language rankings are all There's nothing you can do Mate 60 Pro's 5G modem and radio frequency technology are far ahead MariaDB splits SkySQL and is established as an independent company Xiaomi responds to Yu Chengdong’s “keel pivot” plagiarism statement from Huawei
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/5587102/blog/10149561