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

The concept of java idempotency

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "the concept of java idempotency". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the concept of java idempotence".

I. the concept of idempotency

In programming. The characteristic of an idempotent operation is that the effect of arbitrary execution is the same as that of one execution. Idempotent functions, or idempotent methods, are functions that can be executed repeatedly with the same parameters and get the same results. These functions do not affect the state of the system, and there is no need to worry that repeated execution will change the system. For example, the getUsername () and setTrue () functions are idempotent functions. The more complex operation idempotent guarantee is realized by using a unique transaction number (serial number).

My understanding: idempotency is an operation, no matter how many times it is executed, the effect is the same as the result returned.

2. Idempotent scenario

1. Query operation: query once and query many times. When the data is unchanged, the query result is the same. Select is a natural idempotent operation.

2, delete operation: delete operation is also idempotent, delete once and many times is to delete the data. (note that different results may be returned, deleted data does not exist, 0 is returned, multiple items of deleted data are returned, and multiple results are returned)

3. Unique index: prevent new dirty data. For example: Alipay's capital account, Alipay also has a user account, each user can only have one capital account, how to prevent users from creating multiple capital accounts, then add a unique index to the user ID in the capital account table, so a user successfully adds a capital account record. Key points: unique index or unique combined index to prevent dirty data from new data (when there is a unique index in the table and a new error is reported at the same time, it can be queried again, the data should already exist and the result can be returned)

4. Token mechanism: prevent page from being submitted repeatedly.

In principle, it is achieved through session token (it can also be achieved through redis). When the client requests a page, the server generates a random number Token, places the Token in the session, and then sends the Token to the client (usually by constructing the hidden form).

The next time the client submits the request, the Token will be submitted to the server along with the form.

After the server verifies the same for the first time, the token value in the session will be updated. If the user submits it repeatedly, the second verification will fail, because the Token in the form submitted by the user has not changed, but the Token in the server-side session has changed.

5. Pessimistic lock

Lock it when you get the data. Select * from table_xxx where id='xxx' for update; Note: the id field must be a primary key or a unique index, otherwise it will kill people if it is a locked table. Pessimistic locks are usually used with transactions, and the data locking time may be very long, so choose them according to the actual situation.

6. Optimistic lock-optimistic lock only locks the table at the moment the data is updated, but does not lock the table the rest of the time, so it is more efficient than pessimistic locking. Optimistic locks can be implemented in a variety of ways through version or other status conditions:

1. Update table_xxx set name=#name#,version=version+1 where version=#version# is implemented by version number

two。 Through the condition limit update table_xxx set avai_amount=avai_amount-#subAmount# where avai_amount-#subAmount# > = 0 requirement: quality-#subQuality# > =, this scenario is suitable for not using version number, only updating is for data security check, suitable for inventory model, deducting share and rolling back share, and has higher performance.

7. Distributed lock

If it is a distributed system, it is difficult to build a globally unique index, for example, unique fields cannot be determined. At this time, a distributed lock can be introduced. Through a third-party system (redis or zookeeper), the data is inserted or updated in the business system, the distributed lock is acquired, then the operation is done, and then the lock is released. In fact, the idea of multi-thread concurrent lock is introduced into multiple systems. That is, it has to be solved in the distributed system. Key points: a long process requires that it cannot be executed concurrently. A distributed lock can be acquired according to a flag (user ID+ suffix, etc.) before the process is executed, and the lock will fail when other processes are executed, that is, only one of the processes can be executed successfully at a time. After the execution is completed, release the distributed lock (the distributed lock is provided by the third party system).

8. Select + insert

For background systems with low concurrency, or some tasks JOB, in order to support idempotence and repeated execution, the simple processing method is to first query some key data to determine whether it has been executed or not, and then conduct business processing. Note: do not use this method for core high concurrency processes

9. Idempotent state machine

In the business related to the design document, or the business related to the task, it will definitely involve the state machine (state change diagram), that is, there is a state on the business document, and the state will change under different circumstances. in general, there is a finite state machine, at this time, if the state machine is already in the next state, there is a change of the previous state, which can not be changed in theory. The idempotency of the finite state machine is guaranteed. Note: orders and other documents, there is a long state flow, it is necessary to have a deep understanding of the state machine, which is of great help to improve the design ability of the business system.

10. How to ensure idempotency in api that provides interfaces

For example, the payment interface provided by UnionPay: when you need to connect merchants to submit payment requests, it comes with: source source, seq serial number; source+seq makes a unique index in the database to prevent multiple payments (only one request can be processed concurrently).

Important: in order to support idempotent calls, two fields must be passed in the API, one is the source source and the other is the source serial number seq. These two fields are jointly and uniquely indexed in the provider system, so when a third party is called, check in the local system to see whether it has been processed and return the corresponding processing result; if it has not been processed, the corresponding processing is carried out and the result is returned. Note that in order to be idempotent and friendly, you must first inquire whether the business has been processed, and if you do not query and insert it directly into the business system, you will report an error, but it has actually been handled.

III. Summary

Idempotency has nothing to do with whether you are distributed with high concurrency or JavaEE. The key is whether your operation is idempotent. A typical idempotent operation such as setting the A field of a record numbered 5 to 0 is idempotent no matter how many times it is performed. A typical non-idempotent operation, such as adding 1 to the A field of a record numbered 5, is obviously not idempotent. In order to achieve idempotency, we can design no non-idempotent operation in terms of interface design. For example, the demand is: when the user clicks yes, the number of approvals of the answer will be + 1. Change to: when the user clicks yes, make sure that there is a record in the answer approval table, user, answer. The number of approvals is counted by the answer approval table. When designing a system, it is the primary consideration, especially in systems such as Alipay, banks, and Internet finance companies, which all involve money, which should be efficient and accurate, so there can be no problems such as deduction and payment. This will be difficult to deal with, and the user experience will not be good.

Thank you for your reading, the above is the content of "the concept of java idempotency". After the study of this article, I believe you have a deeper understanding of the concept of java idempotence, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report