Skip to content

· 9 min read · Engineering

Designed for Flexibility, Part 4: Surviving the Interruption

Designed for Flexibility, Part 4: Surviving the Interruption

Say you run a batch job on Spot VMs (discounted spare capacity Google can take back at short notice), and Google takes one of the machines back halfway through the job. What happens to the work that machine was doing? The task a queue worker was holding might vanish. A GPU job turning 50,000 chunks of text into embeddings could lose all the chunks done so far, if it has to start over. I did this to two workloads on purpose and counted what each one lost. Neither lost any work.

Part 3 ranks machine types and regions so a cluster knows which to try first. Getting the machine is half of running on Spot. This post is about the other half, the one Part 1 calls interruption, where Google takes the machine back in the middle of a job. Both workloads got through it on standard distributed-systems patterns, an idempotent receiver and checkpoint/restart, and you can build the same patterns into your own jobs.

Two Workloads, One Preemption Each, Zero Lost Work

Google calls taking a Spot VM back a preemption. I forced one on each workload in the middle of a run. The CPU queue worker had 5,000 tasks and passed only if all 5,000 got done. The GPU embedding job passed only if it picked up from its last saved progress and got through all the chunks. Dropping a few tasks would have been a fail, and so would sending the GPU job back to chunk one. This is an older Spot problem, and Google’s docs cover it well. I tested it anyway because every later part of this series counts on it.

I ran both workloads on July 24, 2026, from cwest/gke-spot-playbook, this series’ companion GitHub repo (you won’t need it to follow along). I forced each preemption by running gcloud compute instances simulate-maintenance-event on one node. Google documents that command for making a Spot VM preempt on demand.

The Queue Worker Lost None of Its 5,000 Tasks

The first workload pulls tasks off a Pub/Sub queue (Google’s managed messaging service) on t2d-standard-8 Spot VMs in us-south1-b. Each task is a placeholder. All the worker does with one is wait briefly, then post a receipt to a second queue. I published 5,000 tasks. KEDA, an autoscaler that adds workers as the queue grows, ran 50 copies of the worker on four nodes. With all 50 up, I preempted one node. Running workers dropped to 45 while GKE started a fifth Spot node, and about a minute later there were 50 again. The queue never stopped shrinking.

The worker won’t tell Pub/Sub it’s done with a task until it has posted the receipt. When its node starts shutting down partway through a task, it puts the task back on the queue and a worker on another node picks it up. That’s how nothing got lost.

Pub/Sub’s promise is to deliver each task at least once. If a node dies between posting a receipt and telling Pub/Sub it’s done, the same task goes out again and gets done twice. My tasks don’t charge anybody or send email, and doing one twice did no harm. If yours do, a blind retry after a preemption can charge a customer twice or send the same email twice.

Some tasks were going to show up twice. So when the queue was empty, a check script in the repo read all the receipts back and counted distinct task IDs, ignoring the extra receipts. It found 5,000 out of 5,000.

The GPU Job Resumed with 2,562 Chunks Already Done

The second workload was harder. It’s an embedding job on L4 accelerators (NVIDIA GPUs) that turns 50,000 chunks of Simple English Wikipedia into vectors, split into eight shards that each run in their own pod and work through their chunks in batches. Starting over after a preemption would throw away everything it had done.

It doesn’t start over. Each time it finishes a batch, the embedding worker saves the embeddings to Cloud Storage and records the finished chunks in Google’s document database, Firestore, keyed by chunk hash and model version. I preempted a GPU node after 38 batches, with two of the eight shards finished. Kubernetes started a replacement pod. It read Firestore, logged resume: 2562 of my chunks already done, skipped those chunks and finished its shard. Saved batches went from 38 to 104 and never went down, and all eight shards completed. Any batch that was partway through when the node died got run again from its first chunk. Redoing it cost only time. Writing the same chunk twice just leaves the same result in place. For GPU work on Spot, most of what can go wrong is a job that can’t resume from its last checkpoint.

flowchart TD
    subgraph cpu["CPU queue worker (t2d-standard-8)"]
        p1["Preemption mid-task"] --> r1["Task handed back to the queue"]
        r1 --> i1["Another worker runs it (safe to run twice)"]
        i1 --> z1["5,000 of 5,000 task IDs counted"]
    end
    subgraph gpu["GPU embedding Job (L4)"]
        p2["Preemption mid-run"] --> c2["Last checkpoint"]
        c2 --> resume["Resume from checkpoint"]
        resume --> done["Run completes, no finished chunk redone"]
    end

The Scarce GPU Should Pick the Region

Because L4s are hard to get and CPUs aren’t, I let the GPU pick the region first on the GPU run. The capacity advisor from Part 3 put g2-standard-4 in us-east1-c at the top of its list, with an obtainability of 0.90 (its estimate, from zero to one, that you’ll get the machine), and skipped four US regions that don’t offer that machine. I ranked the CPU options after that, in us-east1. If a job needs a GPU and CPUs, let the GPU pick the region and put the CPU work wherever it lands.

Both Results Come from Well-Known Patterns

Neither workload needed a new idea. The queue worker uses a pattern from Gregor Hohpe and Bobby Woolf’s book Enterprise Integration Patterns. For the GPU job’s checkpoint/restart, John Daly’s 2006 paper derives the best interval between saves. If you want a system designed for flexibility, build these patterns in on purpose so you don’t have to find them again after your first preemption.

The queue worker is an Idempotent Receiver. In Gregor Hohpe and Bobby Woolf’s Enterprise Integration Patterns, that’s a receiver that can get the same message twice and end up with the same result. It pairs with at-least-once delivery, where Pub/Sub sends a task again until some worker acknowledges it. My worker never looked for repeats. It didn’t need to. Its placeholder tasks did no harm when they ran twice, and the check script dropped the extra receipts by task ID after the run. A worker that charges a card or sends an email has to look up the task ID itself and skip any task it’s seen.

Pub/Sub’s ack deadline works like a lease. My tasks subscription sets it to 60 seconds, but the Go client extends it for as long as the worker is still working on the task. If the worker dies, nothing extends it, and when the last extension runs out, Pub/Sub sends the task to another worker. Mine doesn’t wait for that. As soon as its node starts to shut down, it gives the task back to Pub/Sub.

The GPU job uses checkpoint/restart, an idea from supercomputing. Jobs there ran for days and had to outlast hardware failures, and John Daly’s 2006 paper works out how often to save by weighing the cost of each save against the work a failure would throw away. Saved progress is only half of it. Something has to notice the dead pod and start a new one, and in Kubernetes that’s a retry with exponential backoff. The Job controller recreates a failed pod after 10 seconds, then 20, then 40, capped at ten minutes. My embedding Job lets each shard fail up to 10 times (backoffLimitPerIndex: 10).

The last one is a fallback, for the times the machine you wanted doesn’t come back at all. This series builds one, called the Compute Fallback Ladder, and Parts 5 and 6 put it to work.

Four More Patterns Worth Knowing

My two workloads didn’t need the next four. Yours might, if your job talks to other services or writes to a database.

Start with the circuit breaker, which Michael Nygard made famous in his book Release It! and Martin Fowler wrote up with short Ruby examples. When the service you call has failed enough times in a row, the breaker quits calling it and returns an error straight away. My worker calls nothing but Pub/Sub. A job that calls a backend running on Spot VMs should have one.

The bulkhead splits resources into separate pools. When one pool is used up, the others still work. On Spot, give preemptible work its own node pool and its own connection pool, apart from the on-demand work that has to stay up.

Backoff isn’t enough on its own. Retries also need timeouts and jitter (a random delay added to each wait), and Marc Brooker’s piece in the Amazon Builders’ Library explains why. Timeouts cap how long a call can wait on a service that may never answer, say one whose node just got preempted. If all your clients retry on the same schedule, they hit the struggling service at the same moment. That’s what a preemption does when it takes out a few nodes and a whole batch of pods restart at the same time.

Then there’s the transactional outbox, for a worker that writes to a database and then sends a message. If the node dies between the two, you get one and not the other. With an outbox the message goes into the same database transaction as the write, and a separate process sends it later. Either both happen or neither does.

Checkpoints Have to Outlive the Node, and Resume Has to Be Cheap

How often does the job save? If it only saves now and then, a preemption still takes everything since the last save, even with resume working. Where it saves is just as important. Local disk goes away with the node. Progress has to go somewhere that outlives the machine that wrote it. Here, that was Cloud Storage and Firestore. For the queue worker the same rule means each task has to be safe to run twice. Check the cost too. Surviving a preemption at triple the price of an uninterrupted run means your resume mechanism costs too much. At Spot VM prices, a 2,000-task re-run of the queue worker cost 84.8% less than an always-on pool of regular on-demand VMs. The GPU run cost 93.1% less by the same comparison.

Google’s Spot docs give the same interruption advice I followed here. Part 5 is about a stockout, where the advisor ranks a machine as the best one to get and the capacity isn’t there when I request it.