Back to Bytes

HPA Configuration for Guard Service — lab audio overview

2026-04-21

Configure Horizontal Pod Autoscaler for the defense guard service. Generate HPA YAML with CPU-based scaling, custom metrics for guard chain latency, stabilization windows, and scale-down policies.

GenAI Security Engineering › AI Security Engineering › Chapter 1 · Prompt Injection Defense › Deploy injection defense as FastAPI sidecar on GKE

5:39
Configure Horizontal Pod Autoscaler for the defense guard service. Generate HPA YAML with CPU-based scaling, custom metrics for guard chain latency, stabilization windows, and scale-down policies.
Share

More from this chapter

Transcript
Host: Welcome back to AI Security Engineering. You're in the chapter on prompt injection defense — the discipline of stopping attackers from hijacking your AI system through cleverly crafted text. This is core security work for any team running large language models in production. And today's skill — making your defense layer scale automatically under load — is where security meets reliability engineering. Expert: Picture this. You're on the security platform team at a mid-sized fintech — maybe two hundred engineers, and your company just rolled out an AI assistant that reads customer emails and takes actions on their accounts. In front of that assistant sits a defense layer — a small service that inspects every incoming message for prompt injection attacks. Think of prompt injection as someone slipping instructions into a message that trick the AI into doing something it shouldn't, like leaking data or approving a transfer. Now Monday morning hits. Traffic spikes ten times higher than your test load. Your defense layer — the thing standing between attackers and your AI — starts slowing down. Inspections that took fifty milliseconds now take two seconds. Engineers panic and someone suggests just turning off the guard to keep the product responsive. That's the exact moment security dies in production. The only way to avoid that moment is to make your defense layer scale itself — automatically, smoothly, under pressure. That's what you're about to build. Host: So this is the third and final exercise in the objective. In the previous one, you built a generator that produces the deployment files for the guard service — the configuration that tells the cluster how to run it, who it's allowed to talk to, and what cloud identity it uses. Now you're extending that. So what exactly are we building today? Expert: You're writing the configuration that makes the guard service grow and shrink on its own based on how busy it is. The mechanism that does this is called a Horizontal Pod Autoscaler — let me unpack that. Your guard service runs as copies — each copy is called a pod, basically one running instance. "Horizontal" means adding more copies side by side, rather than making one copy bigger. And "autoscaler" just means it happens automatically. So you're writing a rule book that says: watch these signals, and when they cross these thresholds, add or remove copies. Here's the key idea — the conceptual shift. Most people, when they first hear "autoscaling," think of one number: CPU usage. If the processor is busy, add more copies. That works for a web server. It does not work for a security service. Because a guard service can be slow for reasons that have nothing to do with CPU — maybe it's waiting on a language model to score a suspicious message, maybe the chain of checks it runs is backed up. So you'll scale on CPU, yes, but you'll also scale on what's called a custom metric — a signal you define yourself, specifically the time it takes your guard chain to finish inspecting one message. If inspections are taking too long, add more copies, even if CPU looks fine. That shift — from "scale on the machine" to "scale on the user-felt latency" — is the production mindset. Host: What's the part that trips people up here? Expert: Two things, and they're related. The first is scaling too aggressively. If you tell the system "add a copy the moment latency ticks up," you'll get what's called flapping — copies being added and removed every thirty seconds as traffic wobbles. That's expensive and it actually makes things less stable. The fix is something called a stabilization window — basically a cooling-off period that says "don't make another scaling decision for a few minutes after the last one." You'll set different windows for scaling up versus scaling down, because you want to scale up fast when attackers show up, but scale down slowly so you don't drop capacity right before the next spike. The second trap is forgetting that this is a security service. If you let it scale down too fast during a quiet moment, a sudden burst of attack traffic will hit an under-provisioned guard and some requests will bypass inspection or time out. So your scale-down policy needs to be deliberately conservative — remove copies in small steps, with long waits in between. Err on the side of too much security capacity, not too little. Host: So walk us out. What will the listener be able to do after this, and what does it mean for their team? Expert: After this exercise, you'll be able to take any security-critical service and write the scaling rules that keep it both responsive and safe under real production traffic — using standard cluster signals and your own latency measurements, with the right guardrails against flapping and premature shrinking. For your team, this is the final piece that makes your prompt injection defense genuinely production-ready — not a demo, not a prototype, but a layer that holds up during a traffic spike or an attack campaign without human intervention. Host: And since this is the final exercise in the objective, you now have the complete picture — a defense service, its deployment configuration, and its scaling behavior. That's a working implementation of production prompt injection defense you can bring straight back to your architecture discussions. Thanks for listening, and good luck with the build.

Want to go deeper? Explore disciplines with hands-on labs, quizzes, and chapter podcasts.