About two current-limiting modes

Source: Internet
Author: User

In the traffic warning and current limiting schemes, there are two common methods. The first sliding window mode is controlled by counting the number of accesses over a period of time, and a current limit is reached when the number of accesses reaches a certain peak. The second is the number of concurrent users mode, by controlling the maximum number of concurrent users, to achieve the purpose of traffic control. The following two simple analysis of the pros and cons.


1. sliding window Mode

Pattern Analysis:

Each time a visit comes in, we determine whether the total amount of traffic in the first n units of time exceeds the set threshold, and the number of requests on the current time slice is +1.



Each format represents a fixed time (such as 1s), a counter for each grid, we want to obtain the first 5s of the request volume, is the current time slice I ~ i-4 time on-chip counter to accumulate.

This mode is implemented in a way that is more closely aligned with the essential meaning of flow control. Understanding is simple. However, due to the unpredictability of traffic, a large number of requests for the first half of the unit time are poured in, and the second half rejects all requests. (Usually, you need to be able to reduce the unit time to a small enough to alleviate) Secondly, it is difficult to determine how much of this threshold setting is appropriate, only through experience or simulation (such as pressure measurement) to estimate, even the pressure measurement is difficult to estimate the accuracy. The hardware parameters of each machine in the cluster deployment are different, which may cause us to set the threshold value for each machine differently. The same machine at different time points of the system pressure is not the same (such as the evening there are some tasks, or the impact of some other business operations), can withstand the maximum threshold value is not the same, we can not consider the comprehensive.

Therefore, the sliding window mode is usually applicable to the need for protection of a resource (or the promise is more appropriate: I have committed to the provider of an interface, the maximum amount is not more than XX), such as the protection of the DB, the control of a call to a service.

Code Implementation ideas:

Each time slice (unit time) is a separate counter that is used to save the array. Maps the current time in some way (for example, modulo) to an item in an array. Each access to the current time slice on the counter +1, and then calculate the sum of the first n time slices of traffic, exceeding the threshold is limited to flow.

Java code
  1. /**
  2. * The realization of sliding window
  3. * @author Shimig
  4. *
  5. */
  6. Public class Slidingwindow {
  7. / * Loop queue * /
  8. private volatile atomicinteger[] timeslices;
  9. / * Total length of queue * /
  10. Private volatile int timeslicesize;
  11. / * Duration of each time slice * /
  12. Private volatile int timemillisperslice;
  13. / * Window length * /
  14. Private volatile int windowsize;
  15. / * Current time slice position * /
  16. private Atomicinteger cursor = new Atomicinteger (0);
  17. Public Slidingwindow (int timemillisperslice, int windowsize) {
  18. this.timemillisperslice = Timemillisperslice;
  19. this.windowsize = windowsize;
  20. //guaranteed to be stored at least two windows
  21. this.timeslicesize = windowsize * 2 + 1;
  22. }
  23. /** 
  24. * Initialize the queue, because this initialization will request some content space, in order to save space, deferred initialization
  25. */
  26. private void Inittimeslices () {
  27. if (timeslices! = null) {
  28. return;
  29. }
  30. //In multi-threaded case, there will be multiple initialization situation, it's okay
  31. ///We only need to ensure that the obtained value must be a stable, all of which are initialized first, the last assignment method
  32. atomicinteger[] localtimeslices = new Atomicinteger[timeslicesize];
  33. For (int i = 0; i < timeslicesize; i++) {
  34. Localtimeslices[i] = new Atomicinteger (0);
  35. }
  36. Timeslices = localtimeslices;
  37. }
  38. private int Locationindex () {
  39. Long time = System.currenttimemillis ();
  40. return (int) ((time/timemillisperslice)% timeslicesize);
  41. }
  42. /** 
  43. * <p> Count +1 on the time slice and return the sum of all the counts in the window
  44. * <p> This method will always be +1 for a time slice if it is called
  45. *
  46. * @return
  47. */
  48. public int incrementandsum () {
  49. Inittimeslices ();
  50. int index = Locationindex ();
  51. int sum = 0;
  52. //cursor equals index, returns True
  53. //cursor is not equal to index, returns FALSE, and the cursor is set to index
  54. int oldcursor = cursor.getandset (index);
  55. if (oldcursor = = index) {
  56. //Continue in the current time slice +1
  57. Sum + = Timeslices[index].incrementandget ();
  58. } Else {
  59. //There may be other thread has been placed 1, the problem is not very
  60. Timeslices[index].set (1);
  61. //clear, there will be time slice jumping when the traffic is not big
  62. Clearbetween (oldcursor, index);
  63. //Sum + = 0;
  64. }
  65. For (int i = 1; i < windowsize; i++) {
  66. Sum + = timeslices[(index-i + timeslicesize)% timeslicesize].get ();
  67. }
  68. return sum;
  69. }
  70. /** 
  71. * To determine whether access is allowed, not exceeding the threshold, will be a time slice +1
  72. *
  73. * @param threshold
  74. * @return
  75. */
  76. public boolean allow (int threshold) {
  77. Inittimeslices ();
  78. int index = Locationindex ();
  79. int sum = 0;
  80. //cursor is not equal to index, cursor is set to index
  81. int oldcursor = cursor.getandset (index);
  82. if (oldcursor! = index) {
  83. //There may be other thread has been placed 1, the problem is not very
  84. Timeslices[index].set (0);
  85. //clear, there will be time slice jumping when the traffic is not big
  86. Clearbetween (oldcursor, index);
  87. }
  88. For (int i = 1; i < windowsize; i++) {
  89. Sum + = timeslices[(index-i + timeslicesize)% timeslicesize].get ();
  90. }
  91. //Threshold value judgment
  92. if (sum <= threshold) {
  93. //does not exceed threshold value only +1
  94. Sum + = Timeslices[index].incrementandget ();
  95. return true;
  96. }
  97. return false;
  98. }
  99. /** 
  100. * <p> zeroing the time slice count between fromindex~toindex
  101. * <p> in extreme cases, when the loop queue has gone more than 1 timeslicesize above, the Qing 0 here cannot be carried out as expected
  102. *
  103. * @param fromIndex not included
  104. * @param toindex not included
  105. */
  106. private void Clearbetween (int fromIndex, int toindex) {
  107. For (int index = (fromIndex + 1)% Timeslicesize; Index! = toindex; index = (index + 1)% timeslicesize) {
  108. Timeslices[index].set (0);
  109. }
  110. }
  111. }


2, concurrent user number mode

Pattern Analysis:

Each time the operation executes, we determine whether to limit the flow by judging whether the number of accesses currently executing exceeds a certain threshold.





This mode looks at the alternative of thinking comparison, but it has its unique. In fact, the root of our current limit is to protect the resources, to prevent the system to accept too many requests, overwhelmed, slowing down the service of other interfaces in the system, causing avalanches. What we really need to be concerned about is the requests that are running, and those that have already been completed are past and no longer need to be cared for.

Let's take a look at how the threshold is calculated, for a request, the response time RT, QPS is a relatively easy to obtain parameters, then we calculate: Qps/1000*rt.

In addition, an application is often a complex system that provides more than one service or exposed requests and resources. Internal GC, execution of timed tasks, surges in other service accesses, external relying parties, DB jitter, or inadvertently a bug in the code. Can lead to changes in response time, resulting in changes in system performance capacity. This mode, the appropriate automatic adjustment, when the system is not, RT increases, will automatically adapt to the QPS.

Code Implementation ideas:

When the access starts, we are on the current counter (atomic counter) +1, when finished,-1. The counter is the number of requests that are currently executing. Just determine if the counter exceeds the threshold value.

About two current-limiting modes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.