advanced32 min studyLesson 35 in Engineering

Control loops and reconciliation

Design repeatable controllers around observed state, delayed feedback, idempotent actions, and bounded recovery.

On this page

What you’ll understand

  • Distinguish desired state, observed state, and an event notification.
  • Make reconciliation safe when an action succeeds but its response is lost.
  • Analyze a simple feedback gain without treating it as a production-controller simulation.

A controller compares existing and desired state, then chooses a bounded correction. Lost API responses, stale caches, and competing actors complicate that process. The design must converge after repeated, interrupted observations without duplicating irreversible side effects or oscillating between incompatible decisions.

Reconcile state rather than replaying assumptions

Kubernetes controllers use control loops to move current state toward desired state. An event is a reason to inspect state, not necessarily a complete description of everything that happened. Reconciliation should compare desired and observed resources to decide what remains. It should tolerate repeated invocation and recover after a restart without depending solely on an in-memory sequence of previous notifications.

Separate observation from authority. One controller may own a replica count while another owns certificate renewal. If both rewrite an entire object indiscriminately, one can repeatedly erase the other's work. Document owned fields, stable resource identities, and preconditions for destructive actions. A useful status record says which desired generation was observed and why progress is blocked, allowing the user to distinguish a stale report from an evaluated but unsatisfied request.

et+1=(1−η)ete_{t+1}=(1-\eta)e_t
This idealized discrete loop assumes an exact instantaneous observation and a proportional correction η times the current error. It ignores delays, integer resources, saturation, and noise; it is an analytical teaching model, not a Kubernetes controller equation.↔ Scroll wide equations horizontally.

Make retries safe across uncertain outcomes

Suppose an external API creates a database but the response never reaches the controller. Retrying a blind create can allocate a second database. A stable idempotency key or an externally searchable ownership identity lets the next attempt discover the first result. Record enough intent before the side effect to recover after process death. For irreversible operations, a durable workflow and explicit compensation may be necessary; merely calling the function reconcile does not make it safe.

Reconciliation should be idempotent in effect: observing an already satisfied target should not create more changes. Use bounded retries with backoff for transient failures and clear conditions for invalid specifications. An authorization error does not become healthy merely because the controller retries it quickly. Avoid making every unrelated event trigger a broad inventory scan or an expensive external write. The platform needs both a convergent state machine and a sustainable cost per observation.

Tune feedback without producing oscillation

In the scalar model, a gain strictly between zero and two reduces error magnitude; gains above one alternate its sign. The endpoints zero and two do not reduce its magnitude. Actual infrastructure adds provisioning delay, noisy metrics, and discrete allocation. These effects can make an aggressive controller overshoot even when the instantaneous scalar model appears stable. Observe how long a change takes to become effective, and prevent the controller from interpreting its own unfinished operation as evidence that an identical new operation is needed.

Keep a recovery ledger: time to detect a mismatch, time waiting in the controller queue, external action time, and time to verify the result. Measure all four during a dependency outage and recovery. A low average reconcile duration can coexist with a long queue delay. Rate-limit work by tenant or dependency when necessary, and expose blocked conditions instead of generating an infinite stream of indistinguishable warnings.

Tconverge≈Tdetect+Tqueue+Tact+TverifyT_{\mathrm{converge}}\approx T_{\mathrm{detect}}+T_{\mathrm{queue}}+T_{\mathrm{act}}+T_{\mathrm{verify}}
The additive estimate follows one serial correction path. Parallel actions, repeated attempts, and changing desired state need a richer model; none of these terms is automatically bounded by an event notification.↔ Scroll wide equations horizontally.

Analyze gain and the recovery path

An idealized controller starts with an error of eight units and applies gain η = 0.5. Its real reconciliation path has detection of ten seconds, queue wait of five, provisioning of forty, and verification of fifteen. Compare the mathematical decay with operational completion.

  1. The ideal update factor is 1 − 0.5 = 0.5, so the first three errors are 4, 2, and 1 unit. After four steps the error is 0.5 unit.

  2. At gain 1.5 the factor would be −0.5: magnitude still halves, but errors alternate −4, 2, −1, and 0.5. That sign change illustrates overshoot in the ideal model.

  3. The real serial path estimate is 10 + 5 + 40 + 15 = seventy seconds. A ten-second event detector therefore does not imply ten-second convergence.

  4. A retry at second twenty may occur while provisioning is still underway. Use a stable operation identity and inspect its state rather than allocating a duplicate resource.

The result

Scalar gain intuition helps explain feedback, while actual completion depends on observation delay, queued work, and safe external operations.

Explore the model

Interactive lab

Interactive lab · optimization

Inspect one ideal feedback recurrence

The lab performs gradient descent on f(x) = x²/2 with xₙ = (1 − η)ⁿx₀. Interpret x as a scalar error and vary η. Gains above two diverge. This exact toy recurrence has no provisioning delay, API failures, resource quantization, or controller event queue.

Inspect one ideal feedback recurrenceGradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Current readings: Position x0: 2; Objective x²/2: 2; Iteration behavior: Stable step. All parameters are adjustable with the labeled controls below.−4−202402.174.356.528.7Parameter xf(x) = x²/2n = 0
ObjectiveDiscrete iterate
Position x0
2
Objective x²/2
2
Iteration behavior
Stable step
n = 0 · t = 0 s

Gradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forGain changes convergence and overshoot even before real-world delays are introduced.

Check your understanding

A create request times out, so the controller cannot tell whether a database was created. What is the safest next design step?

Try it yourself

In the ideal recurrence, start at error six and choose η = 1.25. Compute three updates. Explain why these values do not establish the stability of an autoscaler with a two-minute provisioning delay.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

The update factor is −0.25; keep its sign. Then identify an assumption missing from the real autoscaler.

Compare with the solution

The errors are −1.5, 0.375, and −0.09375. Their magnitudes shrink while signs alternate. The toy model assumes immediate exact feedback and immediately effective corrections. With provisioning delay, an autoscaler may repeatedly act on the same stale deficit and oversupply capacity. It needs an explicit pending-capacity model, measured delays, and realistic closed-loop tests before choosing its gain or step policy.

Take it with you

  • Events prompt a fresh comparison of desired and observed state.
  • Safe retries need durable identity around uncertain side effects.
  • Convergence requires bounded queues and actions as well as sensible feedback.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.