Informer cache and workqueues
Why a queue holds keys instead of commands
A watch can deliver repeated notifications; an object can also change while a worker is busy. Informers use list/watch to maintain a local representation, and a queue can schedule a namespace/name for reconciliation. Instead of treating each notification as an instruction to increment a count, read the current desired state when processing the key. An informer callback should perform short work and enqueue, not block on slow external operations.
BookRequest book-lab/readingupdate -> enqueue keyupdate -> enqueue same keyworker -> read current state and reconcileCache lag and optimistic concurrency
A controller-runtime client may read through a cache while sending writes directly to the API server. A successful write is not a promise that the next cached read already contains it. For conflict-sensitive updates, use a suitably fresh read and retry with a newly fetched resourceVersion; do not assume resourceVersion is an integer counter that the application may increment. Recompute the intended change on each retry, and avoid overwriting fields owned by other actors.
Read version A -> another writer changes object -> update returns 409Read fresh version B -> recompute -> attempt bounded updateClassify errors and bound retries
Use a queue rate limiter or a delayed requeue rather than an immediate infinite loop. In controller-runtime, a nonterminal error normally causes rate-limited requeue; a successful result can request a delayed requeue. Exact API semantics depend on the selected library. The local Retry function is a separate policy example: conflict requests reread, timeout or unavailable requests backoff, forbidden and invalid stop with a report. A cap of three retries is illustrative, not a built-in Kubernetes default; a later change or explicit recovery can trigger new work.
attempt 0 + conflict -> rereadattempt 1 + timeout -> backoffattempt 3 + timeout -> report-and-stopLeader election and event feedback
A Lease can coordinate which replica acts as leader, but cannot turn an external operation into an exactly-once transaction. Reconcile must still tolerate duplicates, interrupted writes and a leader transition. A status write may itself trigger another event; avoid needless updates and use predicates only when their filtering will not suppress required drift or deletion handling. Set bounded concurrency, context deadlines and queue metrics; watch for sustained errors rather than treating a quiet queue as proof of health.
Replica A holds Lease -> A processes keysLease lost -> stop leadership workReplica B acquires Lease -> observes current statePractice and next step
Use the retry table and duplicate/drift test in the Go fixture. Explain why a timeout might mean the write succeeded despite the missing response and why rereading before another write matters. Predict the decisions for forbidden, conflict and exhausted retries, then run the test. Next: operator testing and least privilege.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Informer cache and workqueues — knowledge check
Understand informer cache lag, queued keys, optimistic concurrency and bounded retries.
What should a typical queue entry identify?
A status update returns 409 Conflict. What is the next useful step?
Does a successful direct write guarantee an immediately fresh cache read?
What does the local model do on a forbidden response?
What does leader election not guarantee?