ghoti
Centralized server for shared state across distributed instances: rate limiters, leases, atomic counters and broadcast signals
About ghoti
Ghoti
How much of your code exists only to make your servers agree with each other?
Shared rate limits, a single active worker, counters, "deploy now" signals: Ghoti keeps that state in one small server you can talk to with nc. It serves over 134,000 reads per second on a 2-core t4g.micro, and forgets everything on restart, by design.
Examples and the full documentation are at ghoti.miocevic.com.
What you can use it for
- One rate limit shared by many instances. Every API server takes tokens from the same token bucket or leaky bucket slot, so the limit holds no matter how many instances are running.
- Keeping one worker in charge. A timeout memory slot only accepts writes from the connection that holds it. The holder renews it by writing again; once it stops, another client can take over after the timeout. Ownership is forgotten if Ghoti restarts and there is no fencing token, so treat it as a best-effort lease, not a lock that protects data.
- Shared sequence numbers. Every read of an atomic counter increments it and returns the new value, which works for sequence numbers, job IDs or shared counts. The counter starts again from zero after a restart.
- Pushing a signal to running processes. A write to a broadcast slot is sent to every connected client. A multicast slot sends it only to clients that subscribed. Browsers can receive broadcasts over Server-Sent Events. Delivery is at most once.
There are also a simple memory slot for short shared values and a ticker slot that counts down, which works as a watchdog.
Performance
Ghoti v0.3.0 benchmarked with ghoti-bench, reading a 36-byte value from a simple memory slot:
| Connections | Requests per second | p50 | p99 | p99.9 | Server CPU |
|---|---|---|---|---|---|
| 1 | 3,359 | 0.290 ms | 0.372 ms | 0.597 ms | 6% |
| 100 | 134,343 | 0.600 ms | 3.541 ms | 7.123 ms | 82% |
| 1,000 | 112,647 | 8.376 ms | 18.694 ms | 26.231 ms | 89% |
- Hardware. The server is an AWS t4g.micro (2 Graviton2 cores, 1 GiB). The load comes from a c7g.xlarge in the same subnet in us-west-2.
- Workload. Every connection stays open and keeps exactly one request in flight, as the protocol requires. The 100 and 1,000 connection rows are the median of 3 runs of 5 minutes each.
- What the latency includes. Latency is the round trip measured on the client, including the network between the two instances. Ghoti's own histogram, which excludes the network, averages 7.6 µs per memory read.
- 1,000 connections. At 1,000 connections the server CPU is close to saturated, so most of the latency is time spent waiting for a turn.
- Other slot kinds. Atomic counters and token buckets sustain the same load, even when every connection uses the same slot: 127,594 to 139,552 requests per second at 100 connections, and 110,026 to 113,173 at 1,000.
- Correctness. All 21 runs finished with no errors, and every counter and token bucket returned exactly what it should have.
The full report has every scenario, the correctness checks and the commands to reproduce the runs. Expect a different round trip on other networks; the time Ghoti spends inside the server does not depend on the network.
Quick start
Download a release, shown here for Linux on x86-64. For other platforms, pick the matching archive on the releases page, or see Installation.
curl -LO https://github.com/dankomiocevic/ghoti/releases/download/v0.4.0/ghoti_0.4.0_linux_amd64.tar.gz
tar -xzf ghoti_0.4.0_linux_amd64.tar.gz
Ghoti needs a config.yaml in the current directory, in $HOME/.ghoti or in /etc/ghoti, or a file passed with --config. Create one with four slots:
cat > config.yaml <<'EOF'
slot_000:
kind: simple_memory
slot_001:
kind: atomic
slot_002:
kind: token_bucket
bucket_size: 2
refresh_rate: 2
period: minute
slot_003:
kind: broadcast
EOF
Start the server. It listens on localhost:9090 by default:
./ghoti run
In a second terminal, open a connection and type one command per line, waiting for each answer:
nc localhost 9090
| You type | Ghoti answers | What happened |
|---|---|---|
w000hello | v000hello | Stored hello in slot 000 |
r000 | v000hello | Read it back |
r001 | v0011 | The counter was incremented to 1 |
r001 | v0012 | And to 2 |
r002 | v0021 | The rate limiter granted a token |
r002 | v0021 | And a second one |
r002 | v0020 | The bucket is empty: denied until the next minute |
To see a signal, open a third terminal with nc localhost 9090 and leave it idle. Then type w003deploy-42 in the second terminal. Both connections receive the async event a003deploy-42, and the writer also gets v0032/2/0: the event was sent to 2 connections, 2 confirmed it and none failed. Press Ctrl+C to close nc, and stop the server the same way. Its state is gone with it.
Scope and maturity
| Area | Status |
|---|---|
| Slot kinds: simple memory, timeout memory, token bucket, leaky bucket, ticker, atomic counter, broadcast, multicast | Implemented |
Transports: plain TCP (standard), telnet, and http with Server-Sent Events | Implemented |
| Users and per-slot read/write permissions | Implemented. Passwords travel in cleartext, see below. |
| Prometheus metrics endpoint, connection limits | Implemented |
| Cluster mode: leader election for availability, no replication | Experimental |
| Random signal propagation slot | Planned |
More info in Ghoti overview
Documentation
The website at ghoti.miocevic.com has examples and the documentation in one place. The same reference documentation also lives in this repository, in reference/:
- Protocol: framing and limits, which commands are safe to retry, commands, the command summary and the transports:
standard,telnetandhttpwith Server-Sent Events. - Configuration: server settings and environment variables, every slot kind (simple memory, timeout memory, token bucket, leaky bucket, broadcast, multicast, ticker, atomic counter) and connection limits.
- Authentication: users, the login commands and per-slot read/write permissions.
- Metrics: the Prometheus endpoint and the metrics it exposes.
- Cluster (experimental): leader election, configuration, routing traffic to the leader and failure scenarios.
- Error codes, the security model and the changelog.
To study how distributed coordination breaks, the cluster failure scenarios walk through leader crashes, network partitions and split-brain, and explain what each one costs a client.
Installation
Every release on the releases page has one archive per platform, ghoti_<version>_<os>_<arch>.tar.gz for Linux and macOS and ghoti_<version>_windows_<arch>.zip for Windows. The archive has the ghoti binary at its root together with the README, the license and the changelog.
curl -LO https://github.com/dankomiocevic/ghoti/releases/download/v0.4.0/ghoti_0.4.0_linux_amd64.tar.gz
tar -xzf ghoti_0.4.0_linux_amd64.tar.gz
./ghoti version
Building from source needs the Go version listed in go.mod:
go install github.com/dankomiocevic/ghoti/cmd/ghoti@latest
Docker
Starting with v0.4.0, every release also publishes an image for linux/amd64 and linux/arm64 to the GitHub Container Registry. It comes with the four slots of the quick start, so it answers right away:
docker run --rm -p 9090:9090 ghcr.io/dankomiocevic/ghoti
The image is configured for containers:
- Slots come from a mounted file. Mount your configuration over
/etc/ghoti/config.yaml, in Kubernetes from a ConfigMap. Slots can only be configured in the file.docker run -p 9090:9090 -v ./config.yaml:/etc/ghoti/config.yaml:ro ghcr.io/dankomiocevic/ghoti - Other settings can come from the environment. Any
GHOTI_*variable overrides the file, so credentials likeGHOTI_CLUSTER_PASScan come from a Docker or Kubernetes secret instead of the ConfigMap. - It listens on all interfaces. The image sets
GHOTI_ADDR=:9090. The server default,localhost:9090, cannot be reached from outside a container. - Logs are JSON. The image sets
GHOTI_LOG_FORMAT=json. Set it totextto read them by hand. - There is no shell. The image is distroless and runs as a non-root user, so a Docker
HEALTHCHECKcommand cannot run inside it. Use a TCP check on port 9090, like a KubernetestcpSocketprobe.
A restart clears every slot, and Docker and Kubernetes restart containers on their own. Run a single replica unless you use cluster mode. make docker builds the image locally as ghoti:dev.
More info on Getting started.
Contributing
Issues and pull requests are welcome on GitHub. Building needs the Go version listed in go.mod. make help lists every target, the main ones are:
make build # binary in .dist/
make test # all tests
make lint # golangci-lint with .golangci.yaml
Pull requests run the same lint and test checks in CI. Report security issues privately as described in SECURITY.md, not as public issues. Planned work is listed in Scope and maturity.
License
Ghoti is licensed under the Apache License 2.0, see LICENSE.
Frequently Asked Questions
What is ghoti?
ghoti is a Distributed Systems library for the Go programming language. Centralized server for shared state across distributed instances: rate limiters, leases, atomic counters and broadcast signals
How do I install ghoti?
Install ghoti with the Go module system using `go get dankomiocevic/ghoti`. Check the repository for the current installation instructions.
What category does ghoti belong to?
ghoti is listed under Distributed Systems, specifically Distributed Systems.