TL;DR

  • A setup backfill booted up to 100 containers per pass. Status checks fell behind and the start deadlines expired.
  • Auto-recovery only runs when work is waiting, so containers with no work stayed failed.
  • Now: waiting work gets a backed-off restart, leftover running work is closed first, and a failure with no work goes back to stopped. The backfill boots 5 per pass.

One day the admin screen showed many workspace containers as failed. Just before, a backfill had run to set up containers for every workspace that didn’t have one yet.

The backfill booted 100 containers at once

The setup is simple. Each workspace has one Durable Object, which starts one Cloudflare Containers container. A backend with the database (the core API below) decides when to start it.

Each start has a 30-minute deadline. The core API’s status check extends it every time it sees the container come up.

The backfill reserved up to 100 setups per pass. A hundred containers came up together and the status check queue backed up. Deadlines expired before anyone extended them, and most failures were exactly that. A few containers exited right after start or failed to start at all.

Auto-recovery walked past them

Auto-recovery already existed. The well-known model is Kubernetes CrashLoopBackOff: the kubelet restarts with a doubling delay, whether or not there is work.

Cloudflare Containers bill only while running. Restarting a container nobody uses only costs money, so I had added conditions.

def recovery_due(*, waiting_work, active_work, attempts, last_requested_at, now) -> bool:
    if not waiting_work or active_work:
        return False
    if last_requested_at is None:
        return True
    return now - last_requested_at >= retry_after(attempts)  # 5, 10, 20, 40 min, then hourly

Containers booted by the backfill have no work yet. waiting_work is false, so this returns False forever. The failures stayed until someone pressed retry.

Leftover “running” work blocked recovery

The other half of the condition, active_work, had a hole too. I had found it around the same time in another environment.

Work a container had picked up before it died stays “running” in the database. The container that should report its end is gone. So active_work stays true, and even with work waiting, the instance went hours without a restart.

Now, when the core API observes a failed container, it closes that boot’s run records. Every minute it fails work picked up by a failed boot, with a reason. Failed work is not rerun automatically, so the same job never runs twice.

Three cases for a failed container

With both holes in view, failed containers are now handled by case.

  • Work is waiting: restart with the backoff above.
  • Running work is left over: stay failed until the per-minute sweep closes it, then fall into one of the other cases.
  • Neither: go back to stopped. A failure with no work is just a stopped container.

To stop, the core API locks the instance row, confirms it is still failed with no waiting or running work, and saves the stop intent. The stop call to the Durable Object runs outside the transaction, so the row lock isn’t held while waiting on an external call.

Recovery is checked before stopping, so an instance with incoming work is never stopped.

The backfill now boots 5 per pass

The mass boot itself is gone too. The backfill runs periodically, so reserving 5 per pass still finishes the setup, just in order.

INITIAL_SETUP_PER_RESYNC = 5  # booting all at once outruns the status checks

It’s the same idea as maxSurge on a Kubernetes Deployment, which limits how many Pods are added at once. If status checks have a limited rate, match the number of starts to it.

Containers already left failed hit the “neither” case on the next check after the change and go back to stopped. Auto-recovery that waits for work also needs a destination for when there is no work, or it leaves failures in place.