Sobes.tech
Senior

How to control load on handles (rate limiting)?

sobes.tech AI

Answer from AI

Various approaches are used to control load on API endpoints (rate limiting) to restrict the number of requests from a single client within a certain period. This helps protect the service from overloads and abuse.

Main methods:

  • Token Bucket — a certain number of tokens are issued to the client, each request 'spends' a token. Tokens are replenished over time.
  • Leaky Bucket — requests are processed at a fixed rate, excess requests are queued or discarded.
  • Fixed Window — counts requests over a fixed interval (e.g., per minute).
  • Sliding Window — provides a more precise count of requests over a sliding time window.

In Kotlin, rate limiting can be implemented using libraries such as resilience4j or bucket4j. Example with bucket4j:

import io.github.bucket4j.Bandwidth
import io.github.bucket4j.Bucket
import io.github.bucket4j.Refill
import java.time.Duration

val limit = Bandwidth.classic(10, Refill.greedy(10, Duration.ofMinutes(1)))
val bucket = Bucket.builder().addLimit(limit).build()

fun handleRequest() {
    if (bucket.tryConsume(1)) {
        // Process request
    } else {
        // Return 429 Too Many Requests error
    }
}

You can also use proxy servers (Nginx, Envoy) or API Gateways with built-in rate limiting for centralized control.