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

What is distributed idempotency?

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

Share

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

This article shows you what is distributed idempotence, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Idempotent problem

The idempotency problem means that the result of multiple execution of an interface should be the same as that of one execution (that is, repeated operations will not affect the accuracy of the data). When the data is unchanged, query and delete operations are inherently idempotent, while new and modified operations cannot be idempotent by default.

The vernacular is: how many times I click the button, just generate it for me.

Submitting once is the same as submitting 100 times.

1. Token mechanism

The server provides an interface to send token. When we analyze the business, which business has the idempotent problem. You must obtain the token before executing the business, and the server will save the token to redis.

Then when the business interface is called for the request, the token is carried over, which is usually placed in the request header.

The server determines whether the token exists in the redis, which indicates the first request, then deletes the token and continues to execute the business.

Danger

two。 Using redis set to prevent weight

A lot of data needs to be processed and can only be processed once. For example, we can calculate the MD5 of the data and add it to the set of the redis. Every time we process the data, we first see whether the MD5 already exists, and if it exists, it will not be processed.

3. Anti-weight meter

Use the order number orderNo as the unique index of the anti-duplicate table, insert the unique index into the deduplicated table, and then conduct the business operation, and they are under the same transaction. This ensures that when the request is repeated, the request fails because the unique constraint of the table is removed, and the idempotent problem is avoided. Note: the deduplicated table and the business table should be in the same library, which ensures that the data of the deduplicated table will be rolled back even if the business operation fails because of the same transaction. This ensures the consistency of the data.

4. Database optimistic lock

This approach is suitable for update scenarios.

For example: update t_goods set count = count-1, version = version + 1 where g_id = 2 and version

According to the version version number, that is, get the version version number of the current product before operating the database, and then take this version number when operating. When we sort out the inventory for the first time, we get a version of 1 and a call to the inventory service version of 2; but there is a problem returning to the order service, which once initiates a call to the inventory service. When the version passed in by the order service is still 1, the above SQL will not be executed. Because the version has become 2, the where condition does not hold. No matter how many times it is called, it will only be handled once.

Optimistic locks are mainly used for problems of reading more and writing less.

5. Business layer distributed lock

When the interface is called, a unique id,redis is generated to save the data to the collection (deduplicated), which is processed as soon as it exists. You can use ngxin to set a unique id for each request.

Proxy_set_header X-Request-Id $request_id

6. Generate a unique token scheme

The token is obtained from the service before the data is submitted, and the validity period is set. After submission, the service verifies the token, and the verification generates a new value by deleting the old value, waiting for the next acquisition.

For example, the payment interface provided by UnionPay: when you need to access the merchant to submit the payment request, it is accompanied by: source source, seq serial number. Source+seq does a unique index in the database to prevent multiple payments (concurrently, only one request can be processed)

What is distributed idempotence? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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