All notes

Three Bugs That Forced Me to Look Inside Spluur

Every visitor looked like the same person, a verified domain didn't route, and my migrations stopped matching my database. None of them crashed anything.

5 min readspluurdebugginginfrastructure
On this page
  1. Bug 1: everyone had the same IP
  2. Bug 2: the domain was verified, but nothing routed
  3. Bug 3: migration history stopped matching the database
  4. What they had in common

For a while, every visitor to Spluur looked like the same person.

Nothing crashed and no alert fired. The API kept answering requests. But when I checked the address the API believed each request came from, it was always the same one, and it wasn't a user. It was one of my own services.

Those are the bugs I remember best, the ones where nothing is down and the system is just quietly wrong. Here are three of them. They look unrelated, but they taught me the same thing.

Bug 1: everyone had the same IP#

Spluur's API doesn't talk to browsers directly. Traefik sits in front:

Browser → Traefik → Fastify API

So from the API's point of view, the thing that connected is Traefik, not the visitor. If you read the socket address inside Fastify, you get Traefik's address on the Docker network, for every user. Five people, five connections, one apparent client.

Traefik passes the real address along in an X-Forwarded-For header, but Fastify only uses it if you tell it which proxies to trust. That's what trustProxy is for, and I had it wrong. So req.ip quietly fell back to the proxy's address.

The consequences were the quiet kind. Rate limiting would see the whole internet as one client. Logs couldn't tell users apart. Abuse detection was impossible.

The fix was one line:

import Fastify from "fastify";
 
// Trust exactly one hop: Traefik, directly in front of the API.
const app = Fastify({ trustProxy: 1 });

The line wasn't the useful part, though. I could have searched "Fastify get real client IP", pasted trustProxy: true and moved on. But that says "trust whatever is in front of me", and depending on how your proxy chain handles forwarded headers, you can end up trusting an address the client supplied. That isn't a security model. It's hope. Trusting exactly one hop means I have to remember it when a second proxy or load balancer shows up.

I also added a small debug route so I can see all three values at once:

app.get("/debug/ip", async (req) => ({
  ip: req.ip,
  forwarded: req.headers["x-forwarded-for"],
  socket: req.socket.remoteAddress,
}));

What Fastify thinks the IP is, what the proxy says it is, and who actually connected to the socket. Side by side, the problem takes ten seconds to reason about.

Bug 2: the domain was verified, but nothing routed#

Spluur lets you attach a custom domain. You add it, prove you own it, the platform verifies it, and it should start routing to your app. This time the domain verified and nothing happened.

I went through the usual list. DNS, the certificate, Traefik, the deployment, the hostname. All fine.

The actual problem was that Traefik can learn routes from two places: Docker labels, or YAML files on disk. Spluur's deploy worker writes YAML files, and that's what Traefik was watching. When I added custom-domain support, I updated Docker labels on the container instead. The labels were correct. Traefik just wasn't reading them.

My code → writes correct labels → Docker container
Traefik → reads YAML files → ignores the labels

Inspect the code that made the change and everything looks right. The running system was looking somewhere else.

The fix wasn't better labels. It was removing the second way of doing it. Custom-domain changes now go through the same file writer the deploy worker uses, so there's one writer and one source for Traefik to read:

http:
  routers:
    my-app-custom:
      rule: Host(`app.example.com`)
      service: my-app
      tls:
        certResolver: letsencrypt
  services:
    my-app:
      loadBalancer:
        servers:
          - url: http://my-app:3000

When there are two ways to configure the same thing, you have two things that can disagree. Now, whenever I change configuration, I ask what the running system actually reads, not what my code updated or whether the function returned successfully.

Bug 3: migration history stopped matching the database#

This one came from moving fast. Early on, npx prisma db push is very tempting. Change the schema, push, keep building, no migration files and no ceremony. I did that for too long.

Then I wanted real migrations, because at some point a database shouldn't just be "whatever shape I last pushed". You want history, and you want another environment to be able to reproduce it. But my database already had changes that existed in no migration file, so Prisma's history said one thing and the database said another. Prisma was right to complain.

The usual answer is to reset. On my laptop, fine. With real data inside, you can't just reset and hope. I had to compare what tables and columns existed against what Prisma thought should exist, then use Prisma's baselining to make the history line up with reality without destroying anything. It was mostly tedious comparison.

db push gave me speed, and the bill came later. So now migrations are the source of truth from day one, even for a small project where I'm the only developer and I'll change the schema ten more times this week. Future me won't remember what happened three weeks ago, and the database certainly doesn't remember why.

What they had in common#

In all three cases I trusted something I hadn't looked at closely. I assumed the proxy would hand me the real IP. I assumed the config I changed was the config Traefik read. I assumed the database and its migration history would stay aligned while I moved quickly. The system was wrong in all three cases, but not obviously wrong, and that's the dangerous part.

It changes the question you ask while debugging. Sometimes it isn't "why did this crash?" but "what does the system currently believe?"

It's also what owning the stack means to me. There's no platform team to message when Traefik doesn't route something. The mess is mine, so I have to understand it. Each bug made me learn another part of the machine: Traefik isn't just the thing in front of the app, it's part of the request path, and the deploy worker also writes the configuration that makes deployments reachable.

Three bugs, three different layers, one lesson: don't only look at the code you wrote. Look at what the running system is actually doing.