Skip to content
buddylindseyPublic

About

Rate Limiter as a Service - Self hosted rate limiting server

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

RLaaS

Rate Limiting as a Service

A standalone service for consistent, shared rate limiting across applications.

Go Tests Status

Warning

RLaaS is in active, early-stage development. It is not production-ready. Limiter state currently lives only in memory, is lost when the server restarts, and is not coordinated across server instances.

📌 Table of Contents

💡 What is RLaaS?

RLaaS is a rate-limiting server written in Go. Applications connect over TCP, create named limiters, and ask the service to acquire permits. This gives multiple clients one shared place to enforce request budgets instead of implementing and coordinating independent rate limiters in every application.

RLaaS currently supports fixed-window and token-bucket algorithms. A fixed window such as “300 calls per minute” allows the first 300 acquisitions in its window and denies later requests until the next window begins. A token bucket uses the same budget and window configuration as a continuous refill rate, allowing unused permits to accumulate for bursts up to the configured budget.

🎯 Why does this project exist?

Rate limiting is commonly embedded inside individual applications. That works for local protection, but it becomes harder to maintain when multiple processes or services need to share the same budget. RLaaS exists to move that concern behind a dedicated service with a consistent protocol and a single source of truth.

The project is being built from a focused single-node core toward a durable, distributed service. Its design emphasizes:

  • TCP connections and message framing
  • Protocol boundaries and typed request handling
  • Correct concurrent state updates
  • Swappable storage abstractions
  • Persistence and distributed coordination
  • Predictable client behavior under concurrent load

The current in-memory implementation establishes those boundaries while the storage, coordination, reliability, and operational capabilities needed for production are developed incrementally.

✅ What works today?

  • Concurrent TCP server with persistent client connections
  • Bounded connections with idle, frame-read, and response-write timeouts
  • Graceful shutdown for active requests
  • Per-connection panic containment with correlated stack traces
  • Structured JSON logs with request lifecycle correlation
  • Four-byte, big-endian, length-prefixed message frames
  • JSON request and response codec
  • Strict JSON input validation with bounded request IDs and limiter names
  • Named fixed-window and token-bucket limiters
  • Create, acquire, and delete limiter operations
  • Atomic permit acquisition with per-limiter locking
  • Idempotent creation for identical limiter configurations
  • Configuration conflict detection for duplicate names with different settings
  • Thread-safe in-memory limiter storage behind a storage interface
  • Race-tested Go test suite
  • Runnable concurrent Python example

🚀 Quick start

Prerequisites

  • Go 1.24 or newer
  • Python 3 for the optional client example

Clone the repository and start the server:

git clone https://github.com/buddylindsey/rlaas.git
cd rlaas
go run ./src/cmd/server

RLaaS listens on 0.0.0.0:6342.

Create a fixed-window limiter

Requests use JSON inside a four-byte length-prefixed TCP frame. A create request has this payload:

{
  "request_id": "create-1",
  "operation": "create_limiter",
  "body": {
    "name": "github-api",
    "type": "fixed_window",
    "time_window_ms": 60000,
    "budget": 300
  }
}

The easiest way to send correctly framed requests is the included Python example.

To create a token bucket instead, set type to token_bucket; no additional algorithm-specific fields are required. For example, a budget of 200 and a time_window_ms of 3600000 creates a bucket with capacity 200 that refills at 200 permits per hour.

🐍 Try the concurrent Python example

With the server running, open another terminal and run:

python3 examples/python/fixed_window.py

The example:

  1. Opens a pool of three persistent TCP connections.
  2. Creates a fixed-window limiter with a budget of 300 calls per minute.
  3. Sends 375 acquisition requests from three threads.
  4. Reports allowed requests, denied requests, total request time, and average round-trip latency.

A first run against a fresh server should finish with:

Requests: 375
Allowed: 300 (expected 300)
Denied: 75 (expected 75)
Result: PASSED

The example deliberately reuses the same limiter name. Run it again before the minute expires to see the server retain the exhausted window and deny all 375 requests. See the Python example documentation for details.

🪣 Try the token-bucket example

Run the token-bucket demonstration against the running server:

python3 examples/python/token_bucket.py

The script exhausts a bucket, observes the server's retry_after_ms, and waits for only one token to refill. It then proves another request succeeds before the full configured window elapses, which makes the continuous token_bucket behavior directly visible. The script resets its limiter on each run.

🧭 How it works

TCP connection
  → length-prefixed frame
  → JSON codec
  → typed service request
  → service handler
  → limiter store
  → selected limiter algorithm
  → typed service response
  → JSON codec
  → length-prefixed frame

The layers are intentionally separated:

  • Transport owns TCP connections and framing, but treats payloads as bytes.
  • Protocol translates JSON payloads into typed service requests and responses.
  • Service validates operations and coordinates limiter behavior.
  • Storage owns limiter lookup and atomic acquisition behind an interface.
  • Limiter implementation owns its mutable algorithm state behind its own mutex. The store uses an explicit factory to select the configured algorithm.

See the architecture notes for more detail.

Client connection lifecycle

RLaaS is designed for clients that maintain a small pool of persistent connections. The server may close an idle or unhealthy socket, and clients should discard and lazily replace that socket while preserving the configured pool size.

Reconnecting is safe, but retry behavior depends on the operation. Creating an identical limiter is idempotent and may be retried. An acquire must not be automatically retried after an ambiguous connection failure because the server may have consumed the permit before the response was lost.

Structured logging

RLaaS writes structured JSON logs to standard output for collection by systems such as Datadog, ELK, and CloudWatch. Request lifecycle records include the request_id, operation, connection ID, remote address, outcome, and elapsed time where available. Search for a request ID to follow it from decoding through handling and response delivery. Request bodies are not logged.

The default log level is info. Set RLAAS_LOG_LEVEL to debug, warn, or error to change it:

RLAAS_LOG_LEVEL=warn go run ./src/cmd/server

🧪 Project status and direction

RLaaS currently provides a functional single-process path from a network request through atomic fixed-window and token-bucket acquisitions. It does not yet provide:

  • Persistent limiter configuration or state
  • Coordination between multiple RLaaS instances
  • Authentication, authorization, or transport encryption
  • Operational metrics, tracing, or service-level guarantees
  • Additional algorithms such as sliding windows

The goal is to evolve RLaaS into a service that teams can confidently operate in production. Planned work includes persistent storage, distributed coordination, additional limiter implementations, structured errors for every failure path, resilient connection handling, and production-grade observability.

🛠 Development

Format and test the project from the repository root:

go fmt ./...
go vet ./...
go test -race ./...

Contributions should remain focused and include tests when behavior changes. See CONTRIBUTING.md for project conventions.

License

A license has not yet been selected.

About

Rate Limiter as a Service - Self hosted rate limiting server

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages