Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to ensure the idempotency of the interface under high concurrency of Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to ensure the idempotency of the interface under Java high concurrency". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The method of ensuring idempotency at the front end

The button can only be clicked once

When the user clicks the button, the button is grayed out, or the loading status is displayed.

RPG mode

That is, Post-Redirect-Get, when the customer submits the form, performs a client-side redirection and goes to the successful submission page. Avoid repeated submission caused by users pressing F5 refresh, and also eliminate the problem of repeated submission caused by pressing the back key of the browser. At present, most companies do this, such as Taobao, JD.com and so on.

The method of ensuring idempotency at the back end

Use a unique index

Add a unique index to the fields that are unique to the business, so that inserting the database will throw an exception when the data is duplicated

Idempotent state machine

If the business needs to modify the order status, for example, the order status needs to be paid, the payment is successful and the payment fails. It is best to support only one-way changes in state at design time. In this way, conditions can be added when the update is made, and multiple calls will only be executed once. For example, if you want to update the order status to support success, the previous status must be in payment

Update table_name set status = payment succeeded where status = payment in progress

Optimistic lock to realize idempotence

Query the data to get the version number

Update through the version number, update if the version number matches, and not update if the version number does not match

-- if the query version is 1

-- if the queried version is 1 select version from table_name where userid = 10;-- add 10 update table_name set money = money-10 to the user's account, version = version + 1 where userid = 10 and version = 1

Optimistic locks can also be achieved through conditions, such as inventory cannot be oversold and the quantity cannot be less than 0

Update table_name set num = num-10 where num-10 > = 0

Anti-weight meter

Add an anti-repetition table, and the unique id of the business is used as the unique index, such as the order number. When you want to do a series of operations on the order, you can insert a record into the anti-repetition table. If the insertion succeeds, and the insertion fails, no subsequent operations are performed. In essence, it can be regarded as a distributed lock based on MySQL implementation. Decide whether to delete the corresponding data in the anti-repetition table after successful execution according to the business scenario.

Idempotent realization of distributed lock

When executing the method, the distributed lock is first acquired according to the business's unique id. If the lock is acquired successfully, it will be executed, but if it fails, it will not be executed. Distributed locks can be implemented based on redis,zookeeper,mysql, but the details of distributed locks are not covered.

Select+insert

First check to see if there is any data that meets the requirements, and if the insert is not performed again. Idempotency can be guaranteed in a system without concurrency, and do not use this method in high concurrency, which will also lead to repeated insertion of data. When I do idempotent messages, I usually select first, return data directly, and perform insert operations without data and distributed locks.

Global unique number to realize idempotency

Whether the request is repeated is judged by source (source) + seq (serial number). If the request is repeated, it will be returned to the request for repeated submission, otherwise it will be executed. For example, when multiple three-party systems invoke services, you can use this approach.

This is the end of the content of "how to ensure the idempotency of the interface under Java high concurrency". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report