Java spike system solution optimizes high-performance and high-concurrency combat, learning notes (10)

Hello everyone, I am 方圆
to make a summary of the project


1. How is inventory pre-loading implemented in Redis?

I am through the realization InitializingBean接口, rewrite afterPropertiesSet()方法the pre-loading

1.1 How to add a spike product afterwards?

Add through background management, modify the value in redis cache and database


2. When deducting inventory in Redis, how to ensure thread safety and prevent oversold?

There is a decr()method in redis , which implements the decrement operation, and can保证原子性


3. If Redis cache avalanche or penetration occurs, how to solve it?

An avalanche means that all the values ​​I stored in the cache are invalid, and the request is directly sent to the database. The request is too large and the database cannot hold it. You can set these hot data to never expire, or set a random expiration time, so as to prevent it from failing at the same time.

Cache penetration is data that is not in the cache or in the database. If someone uses these data for high concurrent access, the pressure on the database is also great. You can perform a check on data such as its id value to prevent these non-existent values ​​from accessing the database or use bloom filters. Its principle is to query whether the value exists in the database through an efficient data structure, and it does not exist. When it is, it will return directly, and it will access the database if it exists.


4. How is current limiting and anti-brushing achieved?

I implement current limiting and anti-brushing through an interceptor. I customized an annotation. Its function is to mark the method and specify the number of visits per unit time. If it exceeds the requirement, it will be intercepted.

The interceptor I inherited from HandlerInterceptorAdapter is the preHandle method. In this method, the number of visits is synchronized to Redis. This key-value pair has a validity period. Finally, configure the interceptor into the project, inherit WebMvcConfigurerAdapter, and override the addInterceptors()method


5. For the user's malicious order, he knows your URL address and keeps refreshing, what should I do?

I avoid this problem by hiding the URL address. When accessing the spike interface, a random string will be generated from the backend, then saved in redis, and spliced ​​to the URL address, so that I can access the spike The interface of, through the address of RestFul style, get the random string in it, compare it with the one in redis, if it is consistent, you can continue to visit


6. How is it synchronized to the database after the spike is successful?

Through two steps, one step is to reduce commodity inventory, and the second step is to create a spike order.

6.1 What should I do if I succeeded in reducing inventory but failed to create a spike order?

The two-step process is executed in one transaction, and then the inventory is reduced first. It has a sign of success, and the inventory reduction is successful before the order creation operation is executed

6.2 Spring's default transaction isolation level

By default, Spring uses the default isolation level set by the database, which should be可重复读


7. How does RabbitMQ improve the high availability of messages?

When I created the queue instance, I created it as persistent. It has a durable property set to true, so that even if the RabbitMQ service restarts, the message will not be lost.


8. Talk about the volatile keyword

The most important thing is to ensure the visibility of variables. I want to talk about JMM (java memory model). Each thread has its own working memory. In addition, there is a main memory. The thread gets the value from the main memory and stores it in its own working memory. When the variable is modified, it It will not be synchronized to the main memory immediately. At this time, if other threads get the variable from the main memory, dirty reading will occur. If it is marked by volatile, the visibility of the variable can be guaranteed. When the variable is modified, he will immediately synchronize it to the main memory.


9. The difference between TCP and UDP

  1. TCP requires a three-way handshake to establish a connection; UDP is connectionless
  2. TCP provides high reliability; UDP does not guarantee reliability, and is generally used for live broadcast or voice calls
  3. TCP is a transport layer protocol based on byte stream, it is slower; UDP is faster

10. ArrayList

  • The bottom layer is an array, fast query, slow addition and deletion
  • Its default size is 10. When adding a value, the current array size and total size will be judged first. If the maximum capacity is exceeded, it must be expanded. The expanded size is 1.5 times the original size (right shift operator, Shift 1 bit to the right), and copy the previous data to the new array.

Come on!

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/107446185