A single onion service runs on a single tor process, and a single tor process uses roughly one CPU core for its crypto. Past a certain amount of traffic, that core is the bottleneck, and no amount of web server tuning will help. Onionbalance is the Tor Project's answer: run the service on several machines, each with its own tor, and publish one address that spreads clients across all of them.
It does this without sitting in the traffic path, which is the clever part.
How it works
Recall that a client reaches an onion service by fetching its descriptor and connecting to one of the introduction points listed there. The introduction point forwards the request to whichever tor process set it up. Nothing in the protocol requires every introduction point in a descriptor to belong to the same process.
Onionbalance exploits that. You have:
- A frontend, which holds the private key for the public "master" onion address. It doesn't serve any content.
- Several backends, each running a normal onion service with its own separate address and keys, and its own web server.
The frontend periodically fetches the backends' descriptors from the network, pulls out their introduction points, and publishes a new descriptor for the master address containing a mix of them, signed with the master key. A client fetches the master descriptor, picks an introduction point, and ends up talking to whichever backend owns it. The frontend never sees a byte of user traffic.
The Onionbalance v3 rewrite landed around 2020, after v2 support was on its way out. If you find guides mentioning .key files in RSA PEM format or the old v2 config layout, they're out of date.
Setting it up
I'll assume three machines: one frontend and two backends. They can be VMs. They should not share the frontend's master key; only the frontend needs it.
Install
Onionbalance is Python. On the frontend:
sudo apt install tor python3-pip
pip install onionbalance
(Use a virtualenv, or your distribution's package if it's recent enough. Current source and docs live on gitlab.torproject.org.)
Generate the config
Run the interactive helper on the frontend:
onionbalance-config
It asks how many backends you have and where to put things, generates a new master key (or imports one), and writes a config.yaml plus a small file for each backend. The resulting config looks like this, with placeholders where the real addresses go:
services:
- key: /etc/onionbalance/masteraddressxxxxxxxx.key
instances:
- address: backendonexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.onion
name: backend1
- address: backendtwoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.onion
name: backend2
Back up that master key the same way you'd back up any onion key. If you already have an established onion address you want to scale up, you can import its existing hs_ed25519_secret_key as the master instead of generating a new one, so users don't have to learn a new address.
Frontend tor
The frontend's tor needs a control port for Onionbalance to talk to, and no HiddenServiceDir for the master address (Onionbalance publishes it, not tor). In /etc/tor/torrc:
ControlPort 127.0.0.1:6666
CookieAuthentication 1
DataDirectory /var/lib/tor
Then run Onionbalance against it:
onionbalance -v info -c /etc/onionbalance/config.yaml -i 127.0.0.1 -p 6666
In production you'll want a systemd unit for that. The control port must stay on localhost; anything that can talk to it can reconfigure tor.
Backends
Each backend is an ordinary onion service with one extra line:
HiddenServiceDir /var/lib/tor/backend/
HiddenServicePort 80 127.0.0.1:8080
HiddenServiceOnionbalanceInstance 1
And in /var/lib/tor/backend/, a file called ob_config with the master address:
MasterOnionAddress masteraddressxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.onion
This matters more than it looks. In v3, introduction points are bound to the service's blinded key, which differs between the backend address and the master address. Without HiddenServiceOnionbalanceInstance, a backend only accepts introductions addressed to its own key, and clients arriving via the master descriptor get rejected. With it, the backend accepts both.
Restart tor on each backend, wait for them to publish descriptors, and watch the frontend log. It should report fetching both backends and publishing a master descriptor. Then load the master address in Tor Browser a few times with New Identity in between; you should land on different backends. Putting the hostname in a response header or an HTML comment makes this easy to check (just remove it before you go live).
What it doesn't do
This is where expectations usually need adjusting.
It isn't load-aware. Clients pick introduction points from the descriptor more or less at random. If one backend is a beefy server and the other a Raspberry Pi, they still get roughly comparable shares. You can skew things by running more backends on bigger hardware, but there's no weighting knob.
It doesn't scale indefinitely. A descriptor can only hold so many introduction points, so a master descriptor can only represent a limited number of backends at once. Plan for a handful of backends, not fifty.
Sessions aren't shared for you. A client stays on one backend for the life of its rendezvous circuit, but a new circuit may land elsewhere. If your app keeps sessions in local memory or files, users get logged out at random. Put sessions in a shared store (Redis, the database) like any other multi-server setup.
Health checking is crude. The frontend notices a backend has gone away when its descriptor stops being fresh, which takes a while. In the meantime, some clients get sent to a dead introduction point and have to retry. It's high availability in the "fewer total outages" sense, not the "instant failover" sense.
Feature support lags behind plain tor. Things like client authorization and the newer proof-of-work parameters have historically been missing or partial in Onionbalance, so check the release notes for the version you're installing before you design around them.
Is it worth it?
For most onion sites, no. One tor process on a reasonable machine handles a surprising amount of normal traffic. Onionbalance makes sense when you've measured tor itself pegging a core, when you need to survive one machine dying without a long outage, or when you're under sustained abuse and want more capacity to absorb it while the DoS defences do their work. Large public deployments, the kind described in why big sites run onions, are where it earns its keep.
One security note. Each backend still has its own working onion address, and anyone who learns one can reach that backend directly, bypassing the master. Don't treat backend addresses as secret, and make sure each backend is hardened as if it were the public face. And every backend is a separate server whose real IP has to stay hidden, which multiplies the chances of a leak. Three machines are three times the places to misconfigure something.