Choosing a throttling algorithm

Updated at:

Service throttling primarily uses two algorithms: the queries-per-second (QPS) throttling algorithm and the token bucket algorithm. This topic describes both of these algorithms.

QPS throttling algorithm

The QPS throttling algorithm limits traffic by restricting the number of requests allowed per unit of time.

Advantages:

  • The calculation is simple. Throttling depends only on the number of requests, so the number of allowed requests is predictable. This behavior is more intuitive and better meets user expectations than the token bucket algorithm, where the number of allowed requests also depends on traffic uniformity.

  • It handles burst traffic by extending the throttling epoch. For example, to allow a burst of 20 requests when the limit is 10 requests per second, you can change the configuration to 30 requests every 3 seconds. However, extending the epoch introduces some threats. You can decide the level of threat that is acceptable.

Disadvantages:

  • It does not handle time window boundaries well. For example, if the maximum number of requests is triggered in the last millisecond of one second and the first millisecond of the next, twice the configured QPS can occur within two milliseconds.

  • It allows requests unevenly. During a traffic burst, requests are always allowed at the beginning of the throttling epoch. For example, if the limit is 100 requests per 10 seconds, high traffic can cause all allowed requests to pass within the first second of the epoch.

Token bucket algorithm

In the token bucket algorithm, the system adds tokens to a bucket at a constant rate. To be processed, a request must first obtain a token from the bucket. If no tokens are available, the request is rejected.

Advantages:

  • It allows traffic more evenly, which helps protect the system.

  • Stored tokens can handle traffic bursts, which is often a desired behavior. For sustained high traffic, the algorithm then evenly allows requests up to the configured limit.

Disadvantages:

  • Stored tokens do not have a time-to-live (TTL). During a traffic burst, more requests are allowed in the first epoch, which reduces interpretability. By default, up to 2 times the configured limit of requests can be allowed during the first epoch of a burst.

  • The actual number of allowed requests is difficult to predict because it depends on the request volume and traffic distribution.

Stock bucket coefficient

In the token bucket algorithm, unused tokens are stored in the bucket. The bucket has a maximum capacity that is determined by the stock bucket coefficient. The default coefficient is 1.0. This means the stock bucket capacity is 1.0 times the configured limit. You can set the coefficient to a value between 0.6 and 1.5.

The stock bucket coefficient has two main effects:

  • It determines the number of requests allowed during the first epoch of a traffic burst. For example, if the stock bucket coefficient is 0.6, a maximum of 1.6 times the configured limit of requests are allowed in the first epoch.

  • It affects the false positive rate. A larger stock bucket coefficient provides better toleration for uneven traffic distribution.

    The false positive rate is a key consideration. Service throttling is applied to a single server. In online scenarios, single-server throttling is often used to simulate cluster-wide throttling. Because traffic is not evenly distributed across servers from second to second, false positives can easily occur. For example, consider two servers with a total limit of 20 requests and an individual limit of 10 requests each. If in a given second, the servers receive 5 and 15 requests respectively, one server will throttle 5 requests. You can reduce the false positive rate in two ways:

    • Extend the throttling epoch.

    • Use the token bucket algorithm and tune the stock bucket coefficient.

How to choose a throttling algorithm

Choose a throttling algorithm based on your needs. Consider the following recommendations:

  • If the QPS for a single server is less than 100, use the token bucket algorithm.

  • If the QPS for a single server is greater than 100, you can use either the QPS throttling algorithm or the token bucket algorithm.

  • If you cannot tolerate the number of allowed requests exceeding the configured limit within a single epoch, use the QPS throttling algorithm.