Awesome Go

ghoti

CategoryDistributed Systems
SubcategoryDistributed Systems
Stars32

Centralized server for shared state across distributed instances: rate limiters, leases, atomic counters and broadcast signals

About ghoti

Logo Ghoti

Go Reference FOSSA Status Codecov OpenSSF Scorecard

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:

ConnectionsRequests per secondp50p99p99.9Server CPU
13,3590.290 ms0.372 ms0.597 ms6%
100134,3430.600 ms3.541 ms7.123 ms82%
1,000112,6478.376 ms18.694 ms26.231 ms89%
  • 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 typeGhoti answersWhat happened
w000hellov000helloStored hello in slot 000
r000v000helloRead it back
r001v0011The counter was incremented to 1
r001v0012And to 2
r002v0021The rate limiter granted a token
r002v0021And a second one
r002v0020The 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

AreaStatus
Slot kinds: simple memory, timeout memory, token bucket, leaky bucket, ticker, atomic counter, broadcast, multicastImplemented
Transports: plain TCP (standard), telnet, and http with Server-Sent EventsImplemented
Users and per-slot read/write permissionsImplemented. Passwords travel in cleartext, see below.
Prometheus metrics endpoint, connection limitsImplemented
Cluster mode: leader election for availability, no replicationExperimental
Random signal propagation slotPlanned

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/:

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 like GHOTI_CLUSTER_PASS can 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 to text to read them by hand.
  • There is no shell. The image is distroless and runs as a non-root user, so a Docker HEALTHCHECK command cannot run inside it. Use a TCP check on port 9090, like a Kubernetes tcpSocket probe.

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.

← Back to Distributed Systems