Terraform, GKE, and the 150-Millisecond Question

March 2026 · 10 min read

In the previous post I wrote about training a multi-task intent classifier for a large Dutch e-commerce platform. This one is about everything around it: the Terraform that stands the system up, and the load tests that told us whether it could actually take a punch.

The question the whole serving side had to answer was blunt: P90 under 150 milliseconds at 1000 requests per second. Latency budgets on the search path aren't up for negotiation, and every millisecond we ate was a millisecond stolen from ranking. Nobody upstream was volunteering to give it back.

Three stacks, three lifecycles

The infrastructure repo split into three independent Terraform stacks: training, serving, and stress-test. These were separate roots with separate state rather than modules within one stack, and that split turned out to be the most important design decision in the repo.

flowchart TB
    subgraph TRAIN["training stack (semi-permanent)"]
        C["Cloud Composer<br/>(Airflow)"] --> V["Vertex AI<br/>custom jobs (L4 GPU)"]
        V --> GCS["GCS bucket<br/>models + data"]
        V --> BQ["BigQuery<br/>experiment tracking"]
        AR["Artifact Registry"] --> V
    end
    subgraph SERVE["serving stack (lives with the model)"]
        LB["LoadBalancer"] --> POD["model server pods<br/>FastAPI + TorchScript"]
        INIT["init container<br/>gcloud storage cp model"] --> POD
        HPA["HPA @ 70% CPU"] -.scales.-> POD
        PROM["Prometheus + Grafana"] -.scrapes /metrics.-> POD
    end
    subgraph STRESS["stress-test stack (lives for an afternoon)"]
        LW["Locust workers<br/>(autoscaled)"] --> UI["Locust web UI :8089"]
    end
    GCS --> INIT
    LW ==>|1000 RPS| LB
  

The reason is lifecycle. Training infrastructure (Composer, the bucket, Artifact Registry, BigQuery) exists semi-permanently. Serving infrastructure lives as long as the model serves. And the stress-test stack exists for an afternoon at a time. Mixing resources with different lifespans in one state file means every terraform destroy becomes a nervous negotiation with the plan output. Separating them made destruction boring, and cheap: we tore down the stress-test stack after every session and the serving stack between test phases.

terraform destroy turned into a cost-control habit, something we ran after every session.

The workflow around the stacks stayed deliberately plain. Each root had a terraform.tfvars.example that got copied and filled in per environment, and the loop was plan, have an actual human review it, apply, work, destroy. We didn't bother with wrapper scripts or Terragrunt. With three small roots instead of one big one, plain Terraform stayed comfortable, and I suspect most IaC tooling complexity exists to compensate for state files that grew past what a person can read.

Learnings from the training stack

Cloud Composer, which is managed Airflow, was the piece with the most personality. Nobody warns you that provisioning takes 20 to 40 minutes, every single time. You learn to structure your workday around Composer applies the way you'd plan around an oven: start it, go do something else, come back when it beeps. If you iterate on Composer config by re-applying, the day is gone.

I've reused two patterns from that stack since. First, passing configuration to DAGs via software_config.env_variables in Terraform. Bucket names, project IDs and regions all flow from the same variables that created the resources, so the DAG can't drift from the infrastructure. When the bucket is renamed in one .tf file, the DAG finds out at the next apply, not at 3am. Second, giving Composer its own service account with exactly the roles it needs (Vertex AI user, storage admin on one bucket, BigQuery data editor) instead of the default compute account. The role list gets long. Composer itself needs both a service-agent extension role and a worker role, which cost me an evening of IAM error archaeology. In return, the orchestrator can touch what it should and nothing else.

The workload sizing stayed small too. The Airflow scheduler and web server run happily at 0.5 CPU and 2 GB each, with workers autoscaling between one and three, because our DAGs delegate work to Vertex AI and wait instead of doing it themselves. I'd treat an orchestrator sized like a compute cluster as a smell. Ours is sized like a receptionist, which is what it is.

Learnings from the serving stack

The GKE side is a cluster with an autoscaling node pool (preemptible where the workload tolerates it, at 60 to 70% cheaper), a deployment for the model server, an HPA keyed on CPU around 70%, and a LoadBalancer service in front.

The detail I like most is how the model gets onto the pod. The container image contains only code, and the model artifact lives in GCS. An init container runs gcloud storage cp -r to pull the model and lexical data into a shared volume before the main container starts, and the main container mounts it read-only. Model updates don't require image rebuilds, the image stays small, and read-only mounts mean no pod can quietly corrupt its own weights. Workload Identity binds the Kubernetes service account to a GCP service account with storage.objectViewer, so there are no key files anywhere in the cluster.

Probes deserve their own paragraph, because ML servers break the assumptions web-app defaults encode. A pod that's still loading a model into memory answers TCP but shouldn't get traffic. So the readiness probe waits for the actual /health endpoint, which flips only after the TorchScript artifact is loaded and a warm-up inference has run, and the initial delay is generous by web standards. With an impatient readiness probe, an ML deployment serves 503s during every rollout.

GPU nodes taught their own lesson. Setting gpu_driver_version = "DEFAULT" in the node pool config and letting Google manage driver installation removed a whole category of "why is the accelerator invisible" debugging that I did not miss.

A cluster whose only job is violence

At 1000 RPS, the load generator is a distributed system too. Run Locust from your laptop and you're measuring your WiFi. Run it from one VM and around a few hundred RPS the generator saturates before the target does, and your "results" are a portrait of the wrong bottleneck.

So the stress-test stack is a second GKE cluster whose only purpose is to attack the first one. Terraform deploys autoscaled Locust workers plus a web UI exposed on port 8089, sized by a single variable, so "give me a bigger army" is a one-line change. The Locustfile itself is simple, just users posting realistic queries with 0.1 to 0.5 seconds of think time between requests:

class GKEUser(HttpUser):
    wait_time = between(0.1, 0.5)

    @task(10)
    def predict_single_query(self):
        self.client.post("/v1/predict",
                         json={"query": random.choice(TEST_QUERIES)},
                         name="/v1/predict [single]")

The test queries deliberately span the intent spectrum (exact product hunts, browsy inspiration phrases, how-to questions), because a load test that only sends one query shape gets flattered by every cache in the path. A test-stop listener prints P50/P90/P95/P99 and flags any breach of the 150ms line, and every run ends with an HTML report you can attach to a decision instead of a vibe.

What the load tests actually found

The headline finding had nothing to do with the infrastructure. The single biggest latency lever was request batching inside the API process. The FastAPI service collects incoming requests in an async queue, and a worker drains up to 16 of them every 10 milliseconds into one forward pass, resolving each caller's future individually. Transformers are built for throughput, and feeding them one query at a time wastes almost everything they're good at. Batching tripled sustainable throughput on identical hardware, and the 10ms collection window costs less latency than it saves by an order of magnitude.

Try it

Request Batching Simulator: The batching trade-off above is interactive here. Tune arrival rate, batch size, and collection window, then watch p50/p99 latency and throughput respond in real time. These are the same dynamics that tripled throughput on this serving stack.

The counterintuitive runner-up was torch.set_num_threads(1). PyTorch's default eagerness to parallelize a single small inference across cores meant threads fighting each other under concurrent load. Switching to one thread per worker process and running more worker replicas gave us smoother latency and a better P99 while using less CPU. We found both of these only because the load tests made the bottlenecks visible, and neither showed up under polite manual testing.

Observability closed the loop between test sessions. The API exports a Prometheus histogram, prediction_latency_seconds, with buckets chosen around the SLA: 10ms up through 1s, dense near 100 to 300ms where the truth lives, and Grafana reads from it. We matched bucket edges to the SLA because a histogram with generic buckets can only tell you the P90 was "somewhere between 100 and 500ms", which is a shrug wearing a chart. Ours could answer the 150ms question directly. Next to latency sat a drift monitor comparing live prediction distributions against training via KL divergence, because an API that's fast and confidently wrong passes every load test you'll ever run.

Looking back on the whole exercise, Terraform made the experiments cheap to run and cheap to clean up. Locust kept the results honest, and the Prometheus histograms are how we could prove the improvements. Reproducibility for production is the usual pitch for infrastructure as code. The part I valued more here was that it made destructive experimentation affordable enough that we actually did it.

References