The key points of distributed interviews, if you don’t understand, I suggest you learn more! Question collection with detailed answers (16)

2020 latest Java collection of common interview questions + detailed answers (16)

Continue to update Java related materials. Recently, I have spent a few days consulting the latest interview news of the big bosses, trying to collect more comprehensive interview information. If you want to see the first few collections, you can go to my homepage to find them.

Some of the answers are summarized by myself, and some are collected on the Internet. Don't panic after watching these interviews! If you have more experience, you can share it in the comments. If you have any mistakes, you are welcome to point out. Please feel free to enlighten me, thank you

Redis

157. What is redis? What are the usage scenarios?

Redis is an open source log-based, Key-Value database written in ANSI C, supporting the network, memory-based or persistent, and provides APIs in multiple languages.

 

Redis usage scenarios:

  • Highly concurrent reading and writing of data

  • Mass data read and write

  • Data with high scalability requirements

158. What are the functions of redis?

  • Data caching function

  • Function of distributed lock

  • Support data persistence

  • Support affairs

  • Support message queue

159. What is the difference between redis and memecache?

  • All values ​​of memcached are simple strings, and redis, as its replacement, supports richer data types

  • redis is much faster than memcached

  • redis can persist its data

160. Why is redis single-threaded?

Because cpu is not the bottleneck of Redis, the bottleneck of Redis is most likely to be machine memory or network bandwidth. Since single-threaded is easy to implement, and cpu will not become a bottleneck, it is logical to adopt a single-threaded solution.

 

Regarding the performance of Redis, the official website also has it, and ordinary notebooks can easily handle hundreds of thousands of requests per second.

 

And single thread does not mean slow nginx and nodejs are also representatives of high performance single thread.

161. What is cache penetration? How to deal with it?

Cache penetration: refers to querying a certain non-existent data. When the cache is missed, it needs to be queried from the database. If the data is not found, it will not be written to the cache. This will cause the non-existent data to go to the database every time a request is made Query, causing cache penetration.

 

Solution: The simplest and rude method. If the data returned by a query is empty (whether the data does not exist or the system is faulty), we will cache the empty result, but its expiration time will be very short, and the longest will not exceed five minutes.

162. What are the data types supported by redis?

string、list、hash、set、zset。

163. What are the java clients supported by redis?

Redisson, Jedis, lettuce, etc. Officially recommend using Redisson.

It will be more effective if you look at the self-check of the question and then the detailed answer.

164. What is the difference between jedis and redisson?

Jedis is a client of Redis's Java implementation, and its API provides comprehensive Redis command support.

Redisson implements a distributed and scalable Java data structure. Compared with Jedis, it has simpler functions. It does not support string operations, and does not support Redis features such as sorting, transactions, pipes, and partitions. The purpose of Redisson is to promote the separation of concerns from users to Redis, so that users can focus more on processing business logic.

165. How to ensure the consistency of cache and database data?

  • Set the expiration time of the cache reasonably.

  • When adding, changing, and deleting database operations, Redis is updated synchronously, and the transaction mechanism can be used to ensure data consistency.

166. There are several ways to persist redis?

There are two ways to persist Redis, or two strategies:

 

  • RDB (Redis Database): A snapshot of your data can be stored at a specified time interval.

  • AOF (Append Only File): Every write command received is appended to the file through the write function.

167. How does redis implement distributed locks?

Redis distributed lock actually occupies a "pit" in the system, and when other programs also occupy the "pit", the execution can be continued after the occupation is successful, and it can only be given up or retry later if it fails.

 

Occupying a pit generally uses the setnx (set if not exists) instruction, which is only allowed to be occupied by one program. After using it, call del to release the lock.

168. What are the defects of redis distributed lock?

Redis distributed locks cannot solve the timeout problem. Distributed locks have a timeout period. If the program execution exceeds the lock timeout period, problems will occur.

169. How does redis optimize memory?

Use hash tables (hashes) as much as possible. The memory used by hash tables (which means that the number stored in the hash table is small) is very small, so you should abstract your data model into a hash table 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, but store all the user's information in a hash table.

170. What are the redis elimination strategies?

  • Volatile-lru: Select the least recently used data from the data set (server. db[i]. expires) that has set expiration time.

  • Volatile-ttl: select the data that will expire from the data set (server. db[i]. expires) that has set expiration time.

  • Volatile-random: arbitrarily select data to be eliminated from the data set (server. db[i]. expires) for which the expiration time has been set.

  • allkeys-lru: Select the least recently used data from the data set (server. db[i]. dict) and eliminate it.

  • allkeys-random: Randomly select data from the data set (server. db[i]. dict) and eliminate it.

  • no-enviction (eviction): prohibit eviction of data.

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

  • The main server writes memory snapshots, which will block the work of the main thread. When the snapshot is relatively large, the performance impact will be very large, and the service will be suspended intermittently, so the main server is best not to write memory snapshots.

  • For the performance problem of Redis master-slave replication, for the speed of master-slave replication and the stability of the connection, the master-slave library is best in the same local area network.

    At last

    The content of the interview questions is over here, there will be more updates in the follow-up, I hope it will be helpful to everyone.

    Finally, I want to say something to you. I have worked for so many years and have interviewed some people for others. Whether it is from the perspective of the interviewer or the leader, in addition to interview skills and experience, great technology and project experience are also their trump cards and confidence. Core technology sharing of first-tier manufacturers

     It took me a long time to sort out some learning materials. What I posted above is the tip of the iceberg in the materials. I hope I can help you! Click to learn the secret code together: csdn

                             

      I will share more pure dry goods articles in the follow-up, and hope to really help you. Your support is my biggest motivation! Welcome to follow and like!

                                                           

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/109036174