【Problem Description】 When users use the APP, the page is very stuck, and they will click randomly, because the interface does not do repeated submissions, there will be several identical requests, in the service layer, one thread does not insert complete, and the other thread checks, empty. So I also inserted one. Originally, everyone had one, but a salesman had three, resulting in business logic errors. For the processing of malicious submissions, it can be done in the front-end part, and there can also be mature solutions in the back-end part.
【Solution】1. Use uniqueness constraints to solve the idempotency problem of transactions, set uniqueness constraints, and if there is a scenario of repeated commits, exceptions to uniqueness constraints will be thrown at the database level, and the business logic will not be destroyed. Uniqueness constraints on the composition of multiple fields are also acceptable.
The hyperlink login is visible.
The above is to make anti-duplicate settings at the database level.
2. Realize anti-duplication settings at the code level. Many times, it is said that the uniqueness constraint of the database will affect the efficiency of data insertion, because each insertion requires a judgment at the database level. Therefore, judging from the code level, the common practice at the code level is to select first and then insert, but if there is a high concurrency scenario, there will still be repeated commits. You can add synchronized to the logic code, so that in high concurrency scenarios, select first and then insert will take effect. But the efficiency is not high, and the parallel becomes serial. DCL lock mechanism can be used. (Have you found that the method of creating a single object in the copying case mode is very similar, first judge whether the object exists, if it does not exist, create it, otherwise do not create it), the natural DCL lock mechanism is more efficient.
#分布式锁 Distributed locks can also be used to solve the problem, commonly used by Redis and Zookeeper. This section explains how to implement distributed locks using Redis. There is a setNx command operation in Redis, if it doesn't exist, it is a set value, and 1 is returned. If it exists, it does not set, and returns 0. Utilizing the single-threading feature of Redis, the high-concurrency scene is turned into a serial through the message queue. However, there are pitfalls in distributed locks, so you need to pay attention.
The hyperlink login is visible. 3. MVCC mechanism?
3.1 What is the MVCC mechanism? MVCC is a multi-version concurrency control mechanism.
3.2 What problems can be solved? The locking mechanism can control concurrent operations, but its system overhead is large, and MVCC can replace row-level locks in most cases, which can reduce its system overhead and improve performance.
The hyperlink login is visible.
4. There is also a problem of idempotency in messages
For example, how to prevent repeated consumption of messages?
In the message middleware in MQ, these must be understood and understood.
|