← All posts

Two services on one server without stepping on each other

Clic'n Done PDF shares a server with TeamLedger, which was already in production. The rules we follow so neither can take the other down, and the Docker trap that nearly caught us.

Hernán Díaz Rodríguez··4 min read

A small server can host more than one service, and paying for two when one is nearly idle makes no sense. But sharing carries an obvious risk: a mistake while deploying one can take the other offline. When I set up Clic'n Done PDF on the same server as TeamLedger, which was already in production, the requirement was clear: TeamLedger must not notice a thing.

Internet Caddy HTTPS · one file per site TeamLedger its containers, its database, its port Clic'n Done PDF API on its own local port conversions
A single Caddy receives all traffic and routes it by name. Each service listens only locally, on its own port, and each PDF conversion runs in an isolated container.

1. One doorman, one file per site

Only one program can listen on ports 80 and 443, so both services come in through the same Caddy, which also obtains and renews HTTPS certificates on its own. The main configuration imports one file per site, and each project only touches its own.

Two details matter more than they seem:

2. Each on its own port, and only locally

Each service listens on a different port and only on the server's local address, so from outside you can only reach them through Caddy. Nothing needs to be opened in the firewall, and the deploy checks that the port is free before it starts.

3. Limits so a heavy conversion can't eat the machine

Converting a hundred-page PDF keeps a CPU busy for several seconds. So that it never slows TeamLedger down, each conversion runs in its own container with strict limits:

We checked it with a load test: three 80-page documents converting at once used about one core and under 80 MB each, and TeamLedger's response time stayed at around two tenths of a second, the same as before.

4. The trap: the neighbour's image cleanup

This one nearly slipped through. TeamLedger's deploy ends with a Docker cleanup (docker image prune -a) that deletes every image not used by a container at that moment. It's a good habit to keep the disk from filling up… and a problem for us: the converter's image is only in use while a conversion is running. Any TeamLedger deploy during a quiet moment would have deleted it, and the next conversion would have failed.

The fix is small: a sleeping container that uses that image and does nothing else. It has no network, 32 MB of memory and at most 5% of one core, and its only job is to keep the image marked "in use". The neighbour's cleanup keeps working for everything else.

What is forbidden, on the other hand, are global cleanups: no deleting volumes, which is where TeamLedger's database lives, and no restarting Docker.

5. Check, don't assume

After the deploy it isn't enough that our site responds. We compared the neighbour before and after: its containers hadn't restarted, its volumes and images were unchanged, its Caddy files hadn't been touched, Caddy and Docker hadn't restarted, and the firewall had the same rules.

The short list

  1. One proxy in front of everything and one file per site; validate and reload, never restart.
  2. Each service on its own port, and only locally.
  3. CPU, memory and log limits for anything that can grow.
  4. Find out which automatic cleanups the others run and protect yourself from them.
  5. Nothing global: no general cleanups, no restarts, no firewall or SSH changes.
  6. Afterwards, check that the other service is exactly as it was.

None of this is sophisticated, but written down as a list it prevents the silly mistake that takes someone else's service down on a Friday afternoon.

Comments or ideas? Message me on LinkedIn. LinkedIn →