Sub topic 8
Work queues: deduplication, ordering and rate limiting
This chapter assumes you know what an informer cache is, that a controller watches objects through the API server, and that a Reconcile function receives a namespaced name. You should also read simple Go.
Why a queue sits between the informer and Reconcile
An informer receives events from a watch. Events can arrive faster than a controller can process them, and one object can change many times in a second. The controller therefore does not call Reconcile directly from an event handler. The handler adds a key to a work queue, and worker goroutines take keys from the queue.
A key is only the object's namespace/name. It carries no event type and no object contents. The worker reads the latest object from the cache when it runs. This follows from level-triggered logic: the controller acts on current state, so the history of events is not needed.
Deduplication
The queue keeps a key only once while it waits. If default/web changes five times before a worker is free, the queue still holds one default/web entry. The worker then reconciles against the newest state.
The queue also guarantees that one key is never processed by two workers at once. If a key is added while a worker is processing it, the queue marks it dirty. It puts the key back after the worker calls Done. This keeps reconciliation of a single object serial, so you do not need locks for per-object state.
Ordering
Ordering is roughly first in, first out across different keys. There is no ordering between events for the same object, because they collapse into one key. Do not write a controller that depends on seeing event A before event B. Read the object and decide from its current state.
Rate limiting and retries
When Reconcile returns an error, the controller calls AddRateLimited for that key. The default rate limiter combines two limits and uses the larger delay:
- Per-item exponential backoff, starting at 5 ms and doubling for each failure up to 1000 seconds.
- An overall token bucket of 10 items per second with a burst of 100.
A successful run calls Forget, which resets that key's failure count. Backoff therefore stops one broken object from consuming the workers, and the token bucket protects the API server during mass failures.
The Result value gives you other options:
| Return value | Effect |
|---|---|
| `ctrl.Result{}, nil` | Success. The key is forgotten. |
| `ctrl.Result{}, err` | Retry with rate-limited backoff. |
| `ctrl.Result{RequeueAfter: 30 * time.Second}, nil` | Run again after a fixed delay, without counting a failure. |
package controllers
import (
"context"
"time"
ctrl "sigs.k8s.io/controller-runtime"
)
type PollingReconciler struct{}
func (r *PollingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ready := false // replace with a real check of an external system
if !ready {
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
}
return ctrl.Result{}, nil
}Common mistakes
- Returning an error for a condition that is not a failure, such as waiting for another object. Use
RequeueAfterinstead. - Assuming one call per event. Ten changes may produce one call.
- Setting
MaxConcurrentReconcilesabove 1 and sharing unsynchronized state in the reconciler. The queue separates calls per key, but different keys run in parallel.
Exercise
A Deployment is updated three times in 50 ms while all workers are busy. How many times does the queue hold its key, and what does the worker see when it runs?
Show answer
The queue holds the key once, because waiting keys are deduplicated. The worker reads the cache and sees the latest state of the Deployment, with all three updates already applied.
Exercise
Your reconciler must wait for an external database to become ready, which takes about a minute. Should it return an error while waiting? Explain.
Show answer
No. An error triggers exponential backoff and is logged and counted as a failure. Return ctrl.Result{RequeueAfter: ...} with a nil error so the controller checks again after a fixed delay.