In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to prevent the submission of repeated requests". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn "how to prevent the submission of repeated requests"!
Background
In normal development, we are often faced with the problem of preventing duplicate requests. When the response of the server to the request involves the modification of data or the change of status, it may cause great harm. The consequences of repeated requests are particularly serious in trading systems, after-sale rights protection, and payment systems. But most of the time, we are counting on the front end to limit, such as the button diseabled after submission, which are unreliable. At critical times, it still needs to be verified by the back end.
Solution method
1. Verification based on cached data state
Redis storage query is light and fast. When request comes in, it can be recorded in the cache first. The subsequent incoming request is verified each time. The whole process is completed and the cache is cleared.
If (! CacheExtension.getInstance (). AddUnique ($"{key} _ unique", 1, DateTimeOffset.Now.AddDays (365)) {LogExtention.getInstance () .WriteCustomLogAsync (", true," previous batch is not finished yet "); return ResponseResult.FromError (" previous batch is not finished yet! ") ;} if (! string.IsNullOrEmpty (uniqueKey)) {CacheExtension.getInstance () .Remove (uniqueKey);} return ResponseResult.Ok ()
2. Verification using unique index mechanism
Atomic operations are required, and the unique index of the database is thought of. Create a new table, insert data into the table every time request comes in, and delete the record after the operation is completed.
3. Counter verification based on cache
Because the operation of the database consumes the performance, we know that the counter of redis is also an atomic operation. Use counters decisively. It not only improves performance without storage, but also increases the peak value of qps. Each time request comes in, create a new counter with orderId as key, and then + 1. If > 1 (lock cannot be obtained): it means there is an operation in progress. Delete. If = 1 (get the lock): it can be operated.
Redis > SET test 20 OK redis > INCR test (integer) 21 redis > GET test # numeric value saves "21" / / in Redis as a string to get all the specified counters HGETALL counter:user: {userID} / / get the specified counter HMGET counter:user: {userID} praiseCnt hostCnt / / specified point praise + 1 HINCRBY counter:user: {userID} praiseCnt Thank you for your reading, this is the content of "how to prevent duplicate request submission" After the study of this article, I believe you have a deeper understanding of how to prevent the submission of repeated requests, and the specific use needs to be verified in 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.