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 order of Interface calls in web distributed system

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

Share

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

This article introduces the knowledge of "how to ensure the order of interface calls in web distributed systems". Many people will encounter this dilemma in the operation of actual cases, 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!

Problem introduction

Generally speaking, we do not need to guarantee the order of multiple interface calls, but sometimes, some business scenarios may indeed need strict order to ensure the accuracy of the system.

For example, service An in a distributed architecture invokes service B and makes two requests, an insert operation and a delete operation, which is supposed to insert and then delete. However, it is very likely that the two requests have passed, and the cluster deployment falls on different machines, and the insert request may be slow to execute for some reason, resulting in the deletion request being executed first. at this time, there is no effect because there is no data, and then the insertion is finished. OK, insert the data into it, which is completely wrong.

It was supposed to be insert-> delete, but eventually the data should be gone, but now it's delete-> insert, so that the final data still exists, and then you can't figure out what's going on. You can only look at the logs on different machines, take a long time to check, and finally compare the execution time of the two operations, and you may finally be able to find out what the problem is.

This is a very common problem in distributed systems, so how do we ensure the calling order of interfaces?

Solution analysis 1. Avoid introducing sequency as much as possible

First of all, generally speaking, my personal advice to you is that the system you are best designed from business logic does not need this kind of sequence guarantee, because once the sequence guarantee is introduced, we need to introduce some other and complex technologies (such as distributed locks) to ensure that this will lead to an increase in system complexity, system performance degradation, throughput reduction, excessive hot data pressure, and so on.

2. Consistent hash+ memory queue

Second, if you have to ensure sequence, here's a solution we've used.

To put it simply, first of all, you have to use a consistent hash load balancing strategy to distribute requests that correspond to an order id to the same machine. Then on that machine, because it may still be executed concurrently with multiple threads, you have to throw the request corresponding to the order id into a memory queue and force the queue to ensure their sequence.

As shown in the following figure:

3. Distributed lock

To be more complex, use zookeeper-based distributed locks to achieve strong ordering of interface calls.

First, three ordered request requests 1, 2, and 3 sent by service An are sent to the message queue in turn, and then multiple instances of service B are consumed from the message queue. If three instances get the three requests on 1-2-3, then when the request is executed, the lock needs to be acquired from zookeeper before it can be executed. So at this time, our service An also needs to indicate the execution order of the three requests, that is, seq=1/2/3, so that service B can know the execution order.

At this time, all three requests come to acquire the lock. If request 3 acquires the lock first, then see if the list Redis has a sequence number smaller than its own, and release the lock. Then if request 1 gets the lock, go to Redis to determine whether there is a sequence number smaller than yourself, and execute request 1 as soon as you see it. Then delete the sequence number from the list of Redis. Get the lock-> judge-> delete the sequence number in the redis in this order. To ensure the order of the interface.

As shown in the following figure:

This is the end of the content of "how to ensure the sequence of interface calls in web distributed systems". Thank you for 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

Internet Technology

Wechat

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

12
Report