Overview
How does the controller decide how hard to push? The gold standard algorithm used in engineering for over a century — and still dominant in everything from industrial robots to Kubernetes autoscalers — is the PID Controller. It looks at the error from three different time perspectives simultaneously to make a smooth, stable decision.
PID stands for Proportional, Integral, and Derivative. Each term contributes a separate correction signal, and the three are summed together to produce the final control output.
P: Proportional (The Present)
- "How far off are we right now?"
- The output is directly proportional to the current error. If the error is small, make a small adjustment. If the error is huge, make a huge adjustment. The scaling factor is called the P-gain (or
Kp). - Example: CPU is at 80%, target is 50%. Error = 30%. With a P-gain of 0.1, the controller adds
0.1 × 30 = 3replicas. - Problem: A pure P-controller often fails to reach the exact target. It may stabilize at 48% CPU when the target is 50%, because as the error shrinks, the corrective force shrinks with it — until it is too weak to overcome friction in the system. This residual gap is called steady-state error.
I: Integral (The Past)
- "How long have we been wrong?"
- The Integral term accumulates all past errors over time. If the system has been sitting at 48% CPU for five minutes, the error is small each second, but it keeps adding up. The 'I' component slowly increases the corrective force until that stubborn gap is finally closed and the system hits exactly 50%.
- Problem: If the integral accumulates too much before the system responds (e.g., during a long outage), it can cause a large overshoot when the system finally recovers. This is called integrator windup and must be guarded against by capping the integral term.
D: Derivative (The Future)
- "How fast is the error changing?"
- The Derivative term predicts future error based on the current rate of change of the error. If CPU usage is dropping rapidly toward the target, the 'D' component notices this speed and applies a "braking" force before the system reaches the target, preventing it from overshooting and having to correct back the other way.
- Problem: The derivative term amplifies noise. If your measurements are noisy (e.g., CPU fluctuates randomly by ±5% every second), the derivative will react to that noise and cause jitter. In practice, D is often filtered or omitted in software systems where measurement noise is high.
Putting It Together
In practice, most software autoscalers use only P or PI control. Full PID is more common in physical systems (robotics, HVAC, motor control) where the derivative term is meaningful. Kubernetes HPA, for example, uses a proportional algorithm with a stabilization window that approximates integral behavior.
Interviewers at companies with large-scale infrastructure (Google, Netflix, Uber) sometimes ask: "How does Kubernetes HPA decide how many replicas to add?" The answer is a proportional formula: desiredReplicas = ceil(currentReplicas × (currentMetricValue / desiredMetricValue)). For example, if you have 4 replicas at 80% CPU and the target is 50%, HPA computes ceil(4 × 80/50) = ceil(6.4) = 7 replicas. Knowing this formula — and that it is purely proportional — demonstrates real depth.