Databases that start when something connects

Socket activation for MySQL and Elasticsearch on a Linux dev machine: nothing runs until a client connects, and Elasticsearch hands its heap back after 15 idle minutes.

Four boxes in a row joined by arrows: a client, a socket listening on 127.0.0.1:9200, a proxy, and the database on 127.0.0.1:9201. A dashed arrow runs from the proxy to a note: after 15 minutes with no connection the proxy exits and the database stops, and the socket keeps listening. A line underneath says the first connection waits in the socket until the database is ready. Four boxes in a row joined by arrows: a client, a socket listening on 127.0.0.1:9200, a proxy, and the database on 127.0.0.1:9201. A dashed arrow runs from the proxy to a note: after 15 minutes with no connection the proxy exits and the database stops, and the socket keeps listening. A line underneath says the first connection waits in the socket until the database is ready.

Elasticsearch is set up with a 4 GB heap on my Linux boxes, and it holds on to it whether or not I’ve run a search all day. So it doesn’t run all day. Neither does MySQL. Both are socket-activated: systemd owns the port, the first connection starts the real service and is passed straight through, and Elasticsearch is stopped again after 15 minutes with nobody talking to it. I never start a database by hand.

It’s stock systemd, with no daemon of mine in the middle. The alternative was a watcher that polls for activity and starts the database when it sees some, which is one more daemon of mine to look after when systemd already does the job. The units below are for Elasticsearch. MySQL is the same trick with one wrinkle, which is at the end.

A socket that is always there

systemd can hold a listening socket on behalf of a service that isn’t running, and start the service when the first connection arrives. Normally the service then takes over the socket, but most databases don’t know how. They open their own port.

So the database moves aside. I give Elasticsearch port 9201 and put the usual port, 9200, under systemd. Nothing that talks to Elasticsearch needs to know. In /etc/elasticsearch/elasticsearch.yml:

http.port: 9201

The socket unit, /etc/systemd/system/elasticsearch-wake.socket:

[Unit]
Description=Start Elasticsearch on the first connection

[Socket]
ListenStream=127.0.0.1:9200

[Install]
WantedBy=sockets.target

A proxy that passes it on

systemd starts a service with the same name as the socket, so elasticsearch-wake.service is the proxy. systemd-socket-proxyd ships with systemd and does one job: it takes the connection the socket accepted and forwards it to another address.

[Unit]
Description=Forward port 9200 to Elasticsearch on 9201
BindsTo=elasticsearch.service
After=elasticsearch.service

[Service]
ExecStartPre=-/usr/bin/curl -s -o /dev/null -m 40 --retry 20 --retry-delay 2 --retry-connrefused "http://127.0.0.1:9201/_cluster/health?wait_for_status=yellow&timeout=30s"
ExecStart=/usr/lib/systemd/systemd-socket-proxyd --exit-idle-time=15min 127.0.0.1:9201
TimeoutStartSec=150

BindsTo and After make starting the proxy start Elasticsearch first, and make the proxy go if Elasticsearch is stopped by hand, so the socket is ready to start it afresh. The curl line holds the proxy back until the cluster is yellow. Elasticsearch accepts connections a few seconds before it has its shards, and a search in that gap comes back empty. The leading - lets the start carry on if the wait runs out.

Stopping it when it goes quiet

--exit-idle-time=15min makes the proxy exit after a quarter of an hour with no connection. To take Elasticsearch with it, one setting on Elasticsearch’s own unit, through sudo systemctl edit elasticsearch:

[Unit]
StopWhenUnneeded=yes

Nothing is holding Elasticsearch once the proxy has gone, so systemd stops it. The socket keeps listening, and the next connection starts the chain again. Then switch over:

sudo systemctl disable --now elasticsearch
sudo systemctl enable --now elasticsearch-wake.socket

One thing to know about the idle clock: it only sees traffic through the proxy. A client that goes straight to port 9201 is invisible to it.

Check it works

Stop both, ask for the cluster’s health, and see what is running afterwards:

sudo systemctl stop elasticsearch-wake.service
curl -s "http://127.0.0.1:9200/_cluster/health?pretty"
systemctl is-active elasticsearch

The curl sits there for a while and then answers, and is-active says active. Stopping the proxy leaves Elasticsearch unneeded, so StopWhenUnneeded stops it too, and the socket carries on listening. While Elasticsearch is down, ss -ltn still shows 9200 and no longer shows 9201.

The same goes for a command-line job. A script that needs the database starts it by connecting, and I don’t type a start command first.

The first connection waits

The first connection after a stop waits. A stopped Elasticsearch takes about 27 seconds and MySQL about 5. After that it’s an ordinary connection, passed through a proxy on the same machine.

It waits, and it’s never refused. The connection that wakes the service sits in the socket’s backlog until the proxy is ready, so the worst a client sees is a slow answer. A client that gives up after five seconds will give up on a cold Elasticsearch, though, so anything that talks to a sleeping database gets a timeout of a minute or so.

MySQL

The same shape works, with MySQL moved to port 3307 and a second socket file, and one socket unit for the usual port and one for the usual socket path. One difference matters. MySQL accounts that log in by the Unix socket, root by default, are trusted by the user at the other end of it, and through a proxy that user is the proxy. I run the MySQL proxy as an unprivileged user (DynamicUser=yes), so that nobody gets a root login through the public socket, and root goes to MySQL’s real socket.

Both databases are disabled at boot and the sockets are enabled in their place, so a restarted box comes up with every port answering and no database running.

Elasticsearch gets the 15-minute idle stop. MySQL doesn’t: once something has woken it I leave it up for the working day. The same units would stop it, but I want it there while I’m working.

Featured projects

All projects

Latest writing

All writing