Case · Backend · Rust

rate-gate: a request rate limiter

An HTTP service in Rust: one POST /v1/check call says whether an action may pass. Two algorithms: a token bucket on integer milli-tokens (bursts) and a sliding window over two counters (strict limits). JSON rules chosen by key prefix, Prometheus metrics, a state snapshot, a demo page. State lives in the memory of a single instance: replicas do not share it.

  • Rust
  • axum
  • tokio
  • proptest
  • musl
  • distroless
  • Prometheus
  • Docker
  • GitHub Actions

01 · How it looks

Screenshots

Screenshot of the project "rate-gate: a request rate limiter" at 1280 pixels wide
Desktop, 1280 px
Screenshot of the project "rate-gate: a request rate limiter" on a phone screen, 390 pixels wide
Phone, 390 px

02 · About

What it is and why

An HTTP service in Rust: one POST /v1/check call says whether an action may pass. Two algorithms: a token bucket on integer milli-tokens (bursts) and a sliding window over two counters (strict limits). JSON rules chosen by key prefix, Prometheus metrics, a state snapshot, a demo page. State lives in the memory of a single instance: replicas do not share it.

Limitations

A single instance: state is not shared between replicas.

The sliding window is approximate (a weighted sum of two windows).

The snapshot is not transactional: a crash loses up to 30 s of counters.

The project runs on a small VPS with demo data: no redundancy or backups.

What is inside

  • A token bucket without floats: milli-tokens, cost above 1, retry_after_ms and reset_ms computed
  • Property tests (proptest): over any period the bucket grants at most capacity + refill * elapsed
  • 20 tests; an 8.2 MB image, 3.8 MiB RAM idle and 3.9 MiB after 200 requests
  • Container uid 10001, read-only, cap-drop ALL, CSP script-src 'self'

03 · How to run

Run it locally

git clone <repository on request> rate-gate
cd rate-gate
cargo run --release

04 · Code

The repository is private

The source is in a private repository. Message me on Telegram and I will open access.

Request the code