Appearance

Informers, caches, and workqueues

Follow a change from an API watch to a controller worker that can retry safely.

What you will understand

  • Explain what a shared informer keeps in its cache and how handlers use it.
  • Trace why multiple events for a single object can result in one queued key.
  • Reason about a controller crash after it performs a write.

Builds on: Watches and server-side apply

How a watch feeds the controller’s cache

You maintain a controller that generates a ConfigMap for each application. Three changes to the same app arrive while a worker is busy. Processing each event as an instruction can lead to unnecessary intermediate writes. Instead, controllers usually arrange to reconcile one object key, then read the current state when a worker takes it. The key commonly contains a namespace and name. The worker can compare the latest requested config with what already exists.

A shared informer uses API lists and watches to maintain a local cache of selected objects and notify handlers about changes. Multiple consumers in a controller can use the same cache and watch. A controller can wait for the initial cache sync before starting workers. The view can still lag a fresh API read, so the worker should tolerate missing objects and updates made since its read. Step through the cache scene to see the gap between an API write and the local update.

What does an informer pass to a controller?

The informer maintains a local object cache. Event handlers enqueue object keys and workers reconcile the latest state they can observe.

  1. InformerList objects and start a watch
    Local cacheA baseline view is available and updates can follow it.
  2. Watch handlerEnqueue namespace/name
    WorkqueueThe queue holds the object key, so repeated events can be coalesced.
  3. WorkerRead current object state
    Reconcile loopThe worker compares continued intent with existing children.
Follow the sequence

The informer maintains a local object cache. Event handlers enqueue object keys and workers reconcile the latest state they can observe.

A single informer is shown; production controllers often depend on several caches and must synchronize them.

Explore another case

Why a queue can combine multiple changes

A workqueue holds keys that need attention. If a key is already waiting, later changes can be combined with it. A change while the same key is being processed can mark it for another pass. When the worker completes, it clears the processing state and handles any queued repeat. If processing fails, a rate-limited queue can delay retries so a persistent problem does not keep the worker hammering the API. Common queue state is in memory, so a restarted controller rebuilds work from current objects and its watches.

Why can one queue key represent several events?

A queue can coalesce updates and retry a failed reconciliation. Every retry should be safe for work that may have partly succeeded.

  1. Coalesce changes for one key

    Multiple pending notifications for one key can produce one current-state reconciliation.

  2. Attempt reconciliation

    The worker reads current intent and issues repeatable operations with stable identities.

  3. Clear success or schedule a retry

    Success resets failure history; transient failure requeues with backoff instead of a tight loop.

The scene illustrates standard workqueue behavior without prescribing one library version or retry duration.

Explore another case

Recover when a write survives a crash

Ask what happens if the controller writes the generated ConfigMap, then crashes before reporting success. On restart it can reconcile the source again. A safe worker finds the surviving object and decides whether it needs another change. It should avoid replacing it or producing a second external result solely because the earlier status update was missed.

Save the example as repeat.yaml. Apply it twice and compare UIDs. Same-account repeated apply demonstrates updating or leaving an object in place under the same identity. The commands do not run an informer. Use them as an API-side practice step in the controller internals lab.

apiVersion: v1
kind: ConfigMap
metadata:
  name: m15-repeat
  namespace: kv-lab
  labels:
    lab: m15
data:
  desired: stable
kubectl --context kind-kv-lab -n kv-lab apply -f repeat.yaml
kubectl --context kind-kv-lab -n kv-lab get configmap m15-repeat -o jsonpath='{.metadata.uid}{"\n"}'
# Apply again; the object UID should remain the same.
kubectl --context kind-kv-lab -n kv-lab apply -f repeat.yaml
kubectl --context kind-kv-lab -n kv-lab get configmap m15-repeat -o jsonpath='{.metadata.uid}{"\n"}'
kubectl --context kind-kv-lab -n kv-lab get configmap m15-repeat --watch --request-timeout=5s

Avoid repeating unsafe operations on retry

A retry expected to succeed after a transient timeout is different from endlessly retrying invalid input. Record the failure and use backoff where another attempt may help. If the controller writes to an external system, use that system’s support for idempotency or record a stable operation identity so retries do not repeat irreversible work.

Check your understanding

Two updates arrived while a single key waited in the queue. What value should the worker converge on?

Read the current desired value when the key is processed and compare it with the current child. If the cache has not caught up, another update can enqueue a later pass. The intended result is convergence on current state.

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu