Flux is a carrier-grade, strongly consistent distributed rate-limiting service. It implements a replicated state machine via the Raft Consensus Algorithm to ensure that rate limits are enforced with high precision across a distributed cluster, surviving node failures without losing state.
- Strong Consistency: Transactions are only committed once a quorum of nodes acknowledges the log entry.
- Dynamic Reconfiguration: Modify
Rate(TPS) andBurstlimits globally via gRPC without service interruption. - Persistent Storage: Uses BoltDB for stable log storage and state snapshots.
- High Performance: Optimized bidirectional gRPC streaming for low-latency, high-throughput batch operations.
- Observability: Native Prometheus integration tracking request counts, latency histograms, and Raft leadership status.
- Leader Redirection: Intelligent error handling that informs clients of the current cluster leader.
Flux uses a Token Bucket algorithm where the state (token count, last refill time) is synchronized via Raft logs.
- Client sends an
Allowrequest to any node. - If the node is a Follower, it returns the Leader's address.
- Leader proposes a log entry containing the request timestamp.
- Once committed, the FSM (Finite State Machine) updates the local token bucket.
- The result is returned to the client.
- Go 1.23+ +* Docker & Docker Compose
- buf
- Clone the repository:
git clone https://github.com/codemaestro64/flux.git cd flux - Generate Protobuf code (using buf):
buf generate
- Run the cluster:
make docker-up
Method: SetLimit
Updates the rate limit for a specific key.
grpcurl -plaintext -d '{
"key": "tenant_1",
"rate": 100.0,
"burst": 200
}' localhost:50052 flux.RateLimiter/SetLimitMethod: Allow
Consumes tokens for a specific key.
grpcurl -plaintext -d '{
"key": "tenant_1",
"tokens": 1
}' localhost:50052 flux.RateLimiter/Allowmake proto: Runsbuf generateto build gRPC stubs.make test: Runs unit and integration tests with race detection.make lint: Validates code quality using golangci-lint.make build: Compiles the server binary intobin/flux.
Metrics are exposed at http://:9090/metrics.
ratelimit_allow_requests_total: Request counts (allowed/denied).raft_is_leader: Current leadership status.
- Always deploy an odd number of nodes (3, 5, or 7) to avoid split-brain.
- Nodes must have static network identities (static IPs or DNS). Raft peers are identified by address in stable storage.
- Raft I/O is bound by fsync latency. Use SSDs for BoltDB logs. High disk wait times will degrade cluster stability.
- Initialize Bootstrap node with
--bootstrap=true. - Join additional nodes via the
JoinRPC against the current leader.
We welcome contributions! Please follow these steps:
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- All code must pass
make lint. - New features must include unit tests for the FSM logic.
- Protobuf changes must maintain backward compatibility.
Distributed under the MIT License. See LICENSE for more information.