preface

As a back-end developer, not only developers need to master Redis, but also operations personnel need to understand Redis. Since Redis is widely used, we know its importance and are often asked in interviews. In the 80 Redis interview questions, the topics examined include basics, data structure instructions, high concurrency, persistence, clustering, replication, Redis applications, etc. If you are currently preparing for an interview or studying Redis, I believe this editor will meet your interview needs. I also hope that I spent half a month sorting out these questions, the answer to help you.

Java Oop, Java Collections containers, Java exceptions, concurrent programming, Java reflection, Java serialization, JVM, Redis, Spring MVC, MyBatis, MySQL database, messaging middleware MQ, Dubbo, Linux, ZooKeeper, distributed & data structure and algorithm, etc. 25 thematic technical points, are all small editor in each big factory summary of the interview real questions, there have been many fans with this PDF to win many big factory offer. Today, here is a summary to share to everyone! [Finished]

The full version of the Java interview questions address: 2021 latest interview questions collection collection.

The serial number project content link
1 The middleware Java Middleware (2021) Juejin. Cn/post / 694870…
2 Micro service Java Microservices (2021) Juejin. Cn/post / 694906…
3 Concurrent programming Concurrent Programming in Java (2021 latest Edition) Juejin. Cn/post / 695053…
4 Java based Java Basics (2021) Juejin. Cn/post / 695062…
5 Spring Boot Spring Boot Interview Questions (2021 Latest edition) Juejin. Cn/post / 695137…
6 Redis Redis Interview Questions (2021 Latest edition) Juejin. Cn/post / 695166…
7 Spring MVC Spring MVC (2021) Juejin. Cn/post / 695166…
8 Spring Cloud Spring Cloud Interview Questions (2021) Juejin. Cn/post / 695245…
9 MySQL optimization MySQL optimize interview questions (2021 latest edition) Juejin. Cn/post / 695246…
10 JVM JVM Performance Tuning Interview questions (2021 Latest Edition) Juejin. Cn/post / 695246…
11 Linux Linux Interview Questions (2021 latest edition) Juejin. Cn/post / 695287…
12 Mybatis Mybatis (2021 latest Edition) Juejin. Cn/post / 695287…
13 Network programming TCP, UDP, Socket, Http Network programming interview (2021 latest edition) Juejin. Cn/post / 695287…
14 Design patterns Design Mode Interview Questions (2021 Latest edition) Juejin. Cn/post / 695544…
15 Big data 100 Big Data Interview Questions (2021 latest edition) Juejin. Cn/post / 695544…
16 Tomcat Tomcat Interview Questions (2021 Latest edition) Juejin. Cn/post / 695570…
17 multithreading Multithreaded Interview Questions (2021 Latest edition) Juejin. Cn/editor/draf…
18 Nginx Nginx_BIO_NIO_AIO interview Questions (2021 Latest edition) Juejin. Cn/editor/draf…
19 memcache Memcache Interview Questions (2021 latest edition) Juejin. Cn/post / 695608…
20 Java exception Java Exception Interview Questions (2021 Latest edition) Juejin. Cn/post / 695644…
21 The Java virtual machine Java Virtual Machine Interview (2021 latest edition) Juejin. Cn/post / 695658…
22 Java collection Java Set Interview Questions (2021 Latest edition) Juejin. Cn/post / 695684…
23 Git Git Git Command (2021) Juejin. Cn/post / 695692…
24 Elasticsearch Elasticsearch (2021 Latest Edition) Juejin. Cn/post / 695840…
25 Dubbo Dubbo Interview Questions (2021 Latest edition) Juejin. Cn/post / 695842…

1. What is Redis?

Redis is a C language, open source, high performance key-value non-relational cache database. It supports storing a relatively larger number of value types, including string, list, set, zset, sorted set, and hash. Redis data is cache-based, so it can handle more than 100,000 read and write operations per second, making it the fastest key-value DB known. Redis also allows data to be written to disks, ensuring data security. Redis operations are atomic.

2. Redis data type?

Redis mainly has five data types, including String, List, Set, Zset and Hash, which meet most of the requirements

3. What are the benefits of using Redis?

(1) Fast speed, because the data is stored in memory, similar to HashMap. The advantage of HashMap is the time repetition of search and operation

They’re all very low in impurity

(2) Support rich data types, support string, list, set, sorted set, hash

(3) Support transactions, operations are atomicity, the so-called atomicity of the data is either all executed, or not all executed

(4) Rich features: can be used for cache, message, by key set expiration time, expiration will be automatically deleted

4. What advantages does Redis have over Memcached?

  • Redis is an alternative to Memcached, where all values are simple strings
  • Redis is faster than Memcached. 3. Redis can persist its data

5. What are the differences between Memcache and Redis?

1. Memecache Stores all data in memory. It will hang up after power failure. Redis is partially stored on hard disk to ensure data persistence

Memcache’s support for data types is relatively simple. Redis has complex data types.

3. Different underlying models are used. The underlying implementation modes between them and the application protocols for communication with clients are different. Redis directly built the VM mechanism itself, because normal system calls to system functions would waste a certain amount of time moving and requesting.

Redis is single-process single-thread?

  • A: Redis is single-process, 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 of a string value?

  • A: 512 m

8. What is the persistence mechanism of Redis? What are their strengths and weaknesses?

Redis provides two persistence mechanisms, RDB (default) and AOF:

RDB: Redis DataBase snapshot

  • RDB is the default Redis persistence mode. The memory data is saved as a snapshot to the hard disk at a certain time. The generated data file is dump. RDB. The snapshot period is defined by the save parameter in the configuration file.

Advantages:

  1. There is only one file dump. RDB for persistence.
  2. Good DISASTER recovery, a file can be saved to a secure disk.
  3. To maximize performance, fork the child process to complete the write operation and let the main process continue processing the command, so IO maximization. Using a single child process to persist, the main process will not do any IO operations, to ensure the high performance of Redis
  4. When the data set is large, the startup efficiency is higher than that of AOF.

Disadvantages:

  1. Data security is low. RDB is persisted for a period of time. If REDis fails during the persistence, data will be lost. So this approach is more suitable when data requirements are not strict.)
  2. AOF (append-only fifile) persistence mode: all command line records are fully persisted in the redis command request protocol format) and stored as AOF files.

AOF: persistence:

  • AOF persistence (Append Only File persistence) records each write command executed by Redis to a separate log File
  • When Redis restarts, it will restore data from persistent log files. When both methods are enabled, Redis preferentially selects AOF for data recovery

Advantages:

  1. Data security, aOF persistence can be configured with appendfsync property, always, every command operation is recorded to the AOF file once.
  2. If you write files in Append mode, you can use the redis-check-aof tool to solve data consistency problems even if the server goes down during the process.
  3. Rewrite pattern of THE AOF mechanism. Rewrite (‘ AOF ‘); rewrite (‘ flflushall ‘);

Disadvantages:

  1. AOF files are larger than RDB files, and the recovery speed is slow.
  2. When the data set is large, it is less efficient than RDB startup.

What are the pros and cons of both types of persistence?

  • AOF files are updated more frequently than RDB files. AOF files are used to restore data preferentially.
  • AOF is more secure and larger than RDB
  • RDB has better performance than AOF
  • If both are configured with priority loading AOF

9. Redis Common performance Problems and solutions:

Delete Redis expiration key?

11. Redis recycling strategy (elimination strategy)?

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

  • A: Redis reads all data to memory for the fastest read/write speed and writes data to disk asynchronously. So Redis is characterized by fast and persistent data. If data is placed in memory, disk I/O speed severely affects Redis performance. As memory gets cheaper and cheaper, Redis will become more and more popular. If the maximum memory usage is set, new values cannot be inserted after the number of existing records reaches the memory limit.

13. Do you know the synchronization mechanism of Redis?

  • A: Redis can use master slave synchronization and slave slave synchronization. During the first synchronization, the master node performs a BGSave and records subsequent modification operations into memory buffffer. After completion, the RDB file is fully synchronized to the replication node. After completion, the replication node loads the RDB image into memory. After the loading is complete, the master node is notified to synchronize the modified operation records to the replication node for replay.

14. What are the benefits of Pipeline? Why do we use Pipeline?

  • A: Multiple IO round trips can be reduced to one, provided that there is no causal correlation between the instructions executed by the pipeline. When using Redis-Benchmark for pressure measurement, it can be found that an important factor affecting the PEAK QPS of Redis is the number of pipeline batch instructions.

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

  1. Redis Sentinal focuses on high availability and will automatically upgrade slave to Master if master goes down.
  2. Redis Cluster focuses on scalability. When a single Redis memory is insufficient, Cluster is used to fragment storage.

16. When will Redis cluster solution make the whole cluster unusable?

  • A: In A cluster with three nodes A, B, and C, if node B fails without the replication model, the whole cluster will be unavailable as it lacks slots in the range 5501-11000.

17, What are the Java clients supported by Redis? Which is the official recommendation?

  • A: Redisson, Jedis, lettuce, etc., are officially recommended.

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

  • A: Jedis is the client of Java implementation of Redis. Its API provides comprehensive support for Redis commands. Redisson implements distributed and extensible Java data structures. Compared with Jedis, Redisson has relatively simple functions. It does not support string manipulation, sorting, transaction, pipeline, partitioning and other Redis features. The goal of Redisson is to promote a separation of focus from Redisso that users can focus more on processing business logic.

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

  • Set password: confifig set RequirePass 123456 Authorization password: auth 123456

20, What about the Redis hash slot?

  • A: The Redis cluster does not use consistent hash, but introduces the hash slot concept. The Redis cluster has 16384 hash slots. After each key is verified by CRC16, the model of 16384 is taken to determine which slot to place.

21. What is the master/slave replication model of Redis cluster?

  • A: The cluster uses a master-slave replication model, with n-1 replicas per node, in order to make the cluster usable even if some nodes fail or most of them fail to communicate.

Will Redis write operations be lost? Why is that?

  • A: Redis does not guarantee strong data consistency, which means that in practice the cluster may lose writes under certain conditions.

23. How are Redis clusters replicated?

  • A: Asynchronous replication

24. What is the maximum number of nodes in Redis cluster?

  • Answer: 16,384.

How to select database for Redis cluster?

  • A: The Redis cluster cannot make database selection at present, default is 0 database.

26, How to test the connectivity of Redis?

  • A: Run the ping command.

27, How to understand Redis transaction?

A:

1) A transaction is a single isolated operation: all commands in the transaction are serialized and executed sequentially. The transaction will not be interrupted by command requests from other clients during execution.

2) A transaction is an atomic operation: all or none of the commands in a transaction are executed.

What are the Redis transaction related commands?

  • A: MULTI, EXEC, DISCARD, WATCH

29. How to set the expiration time and permanent validity of Redis key respectively?

  • Answer: EXPIRE and PERSIST commands.

30, How does Redis do memory optimization?

  • A: Use hashes whenever possible. Hashes use very little memory, so you should abstract your data model into a hash as much as possible. For example, if you have a user object in your Web system, do not set a separate key for the user’s name, last name, email address, and password. Instead, store all of the user’s information in a hash table.

How does the Redis recycle process work?

A: A client runs a new command and adds new data. Redi checks the memory usage, and if it exceeds the maxMemory limit, reclaims it according to the preset policy. A new command is executed, etc. So we keep crossing the boundary of the memory limit, by constantly reaching the boundary and then constantly reclaiming back below the boundary. If the result of a command is that a large amount of memory is used (for example, the intersection of a large set is saved to a new key), it does not take long for the memory limit to be exceeded by this memory usage.

32, What are some ways to reduce Redis memory usage?

  • A: If you are using a 32-bit Redis instance, you can make good use of the Hash,list,sorted set,set, etc., because many small key-values can often be grouped together in a more compact way.

What happens when Redis runs out of memory?

  • A: Redis write commands will return an error message if the upper limit is reached (but read commands will return normally). Or you can use the configuration flush mechanism by using Redis as a cache, flushing out old content when Redis reaches its memory limit.

34, How many keys can a Redis instance hold? List, Set, Sorted Set How many elements can they store at most?

  • A: Redis can theoretically handle up to 232 keys, and has been tested in practice with at least 250 million keys stored in each instance. We are testing some larger values. Any list, set, and sorted set can have 232 elements. In other words, the storage limit of Redis is the amount of available memory in the system.

MySQL has 2000W data, redis only 20W data, how to ensure that the data in Redis is hot data?

  • A: When Redis memory data sets grow to a certain size, a data obsolescence strategy is implemented. What to know: Redis offers 6 data elimination strategies:
  • Volatile – LRU: Selects the least recently used expires data from a set with an expiration date (server.dbi.expires)
  • Volatile – TTL: Selects expired data from a set (server.dbi.expires) to be discarded
  • Volatile -random: Selects any data from a set (server.dbi.expires) that has been set to expire
  • Allkeys-lru: Culls the least recently used data from the dataset (server.dbi.dict)
  • Allkeys-random: Random selection of data from a dataset (server.dbi.dict)
  • No-enviction: Data expulsion is prohibited

What is the most suitable scene for Redis?

Session Cache

One of the most common scenarios for using Redis is session cache. The advantage of caching sessions with Redis over other stores, such as Memcached, is that Redis provides persistence. When maintaining a cache that is not strictly consistent, most people would be unhappy if all of their shopping cart information was lost. Now, would they be? Fortunately, as Redis has improved over the years, it’s easy to figure out how to use Redis appropriately to cache documents for sessions. Even Magento, a popular commercial platform, offers plug-ins for Redis.

2. Full page Cache (FPC)

In addition to the basic session token, Redis also provides a very simple FPC platform. Returning to the consistency issue, even if the Redis instance is restarted, users will not see a drop in page loading speed due to disk persistence, which is a huge improvement, similar to PHP native FPC. Again, Magento provides a plug-in to use Redis as a full-page caching back end. Also, for WordPress users, Pantheon has a great plugin called WP-Redis that will help you load pages you’ve viewed as quickly as possible.

3, the queue

One advantage of Reids in the memory storage engine world is that it provides list and set operations, which makes Redis a good message queue platform to use. The operation Redis uses as a queue is similar to the push/pop operation of a list in a native programming language such as Python. If you do a quick Google search for “Redis Queues”, you will immediately find a number of open source projects designed to use Redis to build great back-end tools for queues. For example, Celery has a background that uses Redis as a broker, which you can view from here.

4. Leaderboards/counters

Redis does a great job of incrementing or decrementing numbers in memory. Set and Sorted Set also make it very easy to perform these operations. Redis just provides these two data structures. So, to get the top 10 users from the sort set — we call it “user_scores” — we simply do as follows: this assumes, of course, that you sort incrementally by your users’ scores. If you want to return users and their scores, you need to do this: ZRANGEuser_scores 0 10 WITHSCORES Agora Games is a good example of this. It is implemented in Ruby, and its leaderboards are stored in Redis, as you can see here.

Publish/subscribe

Last (but certainly not least) is Redis’s publish/subscribe capabilities. There are a lot of publish/subscribe usage scenarios. I’ve seen people use it for social networking connections, as publish/subscribe based script triggers, and even Redis’s publish/subscribe feature for chat systems!

37. Suppose there are 100 million keys in Redis and 10W of them start with a fixed known prefix. What if you could find all of them?

38. If a large number of keys need to be set to expire at the same time, what should I pay attention to?

  • A: If a large number of key expiration times are set too centrally, redis may experience a temporary lag at that point in time. It is generally necessary to add a random value to the time to spread out the expiration time.

Have you ever used Redis for asynchronous queues? How do you use it?

Have you ever used Redis distributed lock? What is it?

41. How to implement session shared storage in a cluster?

42. What is the difference between memcached and Redis?

What command is used to view the usage and status information of Redis?

  • info

What happens when Redis runs out of memory?

  • If the upper limit is reached, Redis write commands will return an error message (but read commands will return normally). Alternatively, you can use the configuration flush mechanism by caching Redis, flushing out old content when Redis reaches its memory limit.

Redis is a single thread, how to improve the utilization of multi-core CPU?

  • It is possible to deploy multiple instances of Redis on the same server and use them as different servers. At some point, one server is not enough anyway, so if you want to use multiple cpus, you can consider shard.

46, How many keys can a Redis instance hold? List, Set, Sorted Set How many elements can they store at most?

Redis common performance problems and solutions?

(1) It is best for the Master not to do any persistent work, such as RDB memory snapshots and AOF log files

(2) If the data is important, a Slave enables AOF backup, and the policy is set to synchronize data once per second

(3) For the speed of Master/Slave replication and connection stability, it is better for Master and Slave to reside in the same LAN

(4) Try to avoid adding slave libraries to the master library under great pressure

Master < -slave1 < -slave2 < -slave3…

In this way, the single point of failure can be easily solved and the Slave can replace the Master. If the Master fails, you can immediately enable Slave1 as Master.

48. What persistence methods does Redis provide?

49, How to choose the appropriate persistence mode?

50. Does Redis take effect in real time without restarting the configuration?

Redis interview questions 【 attached answer analysis 】

The last

Xiaobian article to share here is over, tidy up is not easy, welcome everyone to exchange, like xiaobian article to share remember to pay attention to me like yo, thank you for your support!