directory

  • preface
  • Flow control mode
      • directly
      • associated
      • link
  • Effect of flow control
      • Fail fast
      • Warm Up
      • Waiting in line
  • conclusion

preface

(4) Sentinel basic flow control Rules

In the previous article, we introduced the basic flow control rules of Sentinel. In this chapter, we will learn more advanced flow control.


Flow control mode

Open advanced options for flow control, and you can see two options: flow control mode and flow control effect.

directly

The default flow control mode is direct, and the flow control effect is fast failure. If you read the previous article is better to understand, direct is directly access to the above resources, fast failure is the effect of access failure directly appear limiting text.

associated

Let’s look at what association is. Microservices’ system resources generally have association relationships, such as:

The order interface will call the payment interface. If the traffic surge occurs on the payment interface, it will cause problems in calling the order interface. At this time, we can limit the flow of the order interface in addition to the payment interface directly.



To verify the effect, we write the controller

@restController Public Class OrderController {@getMapping ("/order") public String Order (){return "This is order interface! ; } @getMapping ("/payment") public String payment(){return" ; }}Copy the code

First visit the/ORDER interface, then add flow control rules to it, select association for flow control mode, and set the associated resource to /payment. That is, if the payment interface accesses more than one time per second, the order interface will be restricted.



Again, the test uses JMeter to configure thread groups and HTTP requests.



This is HTTP configured to access the Payment interface



Before starting the test, the ORDER interface access was normal.



After starting the test, the ORDER interface was restricted.



To sum up, association means that the number of associated resources exceeds the threshold and the current resources are restricted.

link

Link is a resource invocation that may have a hierarchy of upper and lower levels, as shown in the following figure:



A is the root node and uses A as the entry point to access B and C, which in turn can access D and E. Therefore, if we limit the flow of A, we can limit the flow of all resources under A.

Test: View the cluster link and select the tree view to see the relationship between the superior and the subordinate.





We see that the upper resource is /order and the lower resource is /order. Add flow control to the lower /order



Test /order, limiting traffic occurs

Effect of flow control

Above we know several modes of flow control, and now we know the effect of flow control:

Fail fast

Fast failure is the default, and when QPS exceeds the threshold of any rule, new requests are immediately rejected by throwing a FlowException.

Warm Up

Preheat/cold start mode. When the system has been at a low water level for a long time, when the flow suddenly increases, directly pulling the system to a high water level can suddenly overwhelm the system. Through the “cold start”, the flow slowly increases to the upper limit of the threshold in a certain period of time, giving the cold system a time to warm up, preventing the cold system from being overwhelmed. For example: in the Spring Festival grab hot regional train tickets, if the traffic directly put in, may directly kill the 12306 server, so we can set a warm-up time, give the server a buffer period, slowly put the traffic in, until reaching the maximum threshold.

Here, the threshold value is 10 and the preheating time is 5 seconds. As mentioned above, the flow rate gradually increases to the upper limit of the threshold value, so there is an initial threshold value: Initial threshold = Threshold upper limit/coldFactor. ColdFactor is the cold loading factor. The default value is 3, so the initial threshold is 10/3 = 3

The effect of the following configuration is that the threshold of traffic limiting is 3 at first and rises to 10 after 5 seconds.



When testing, quick clicks (more than 3 times per second) will appear limiting the current



Wait 5 seconds, click again quickly, there is no finite stream, because the threshold has gone up to 10

Waiting in line

Queuing mode strictly controls the interval between requests, that is, requests pass at an even speed, corresponding to the leak-bucket algorithm. It is also better to understand that no matter how much traffic, access to resources must queue up, one by one. This approach is mainly used to deal with intermittent bursts of traffic, such as message queues. Imagine a scenario where a large number of requests arrive in one second and then sit idle for a few seconds. We want the system to process these requests gradually during the rest of the idle period, rather than simply rejecting the extra requests in the first second.

To view the effect, add logs to the interface

@Slf4j @RestController public class OrderController { @GetMapping("/order") public String order(){ log.info("Thread:{},time:{}",Thread.currentThread().getName(),System.currentTimeMillis()); Return "This is the order interface! ; } @getMapping ("/payment") public String payment(){return" ; }}Copy the code

Modification of indecency control



Quick access to /order, find that the log output interval is controlled at 1 second, that is, the timeout time of queuing

conclusion

This paper introduces the flow control mode and flow control effect of Sentinel. A good command of them will enable us to choose the optimal flow control configuration for resources according to different business scenarios. Sentinel has a lot of content. These will be covered in a later article. Thank you for reading, and click “like” in the bottom left corner if you find it useful 🙂