How the controller-runtime Cache Actually Works, and Why Your Controller Does Not Crash the API Server
In controller-runtime, r.Get() and r.List() inside a reconciler read from a local in-memory cache built on client-go informers. That cache is filled by a list + watch (now a streaming list with sendInitialEvents) and kept current by the Reflector using resourceVersion, with relists only after 410 Gone. Since client-go 1.36 the delta queue is RealFIFO, which delivers every delta in global order with no deduplication; coalescing of bursts happens only in the controller's workqueue, keyed by namespace/name. The design makes reads nearly free, but it brings stale reads after writes, memory growth from cached objects and indexes, and hidden O(n) List scans, and occasionally a need for APIReader.
Related posts
- Kubernetes v1.37: etcd RangeStream Cuts Memory Use on Large List Reads
Kubernetes Blog mediumAdeptKubernetes v1.37 promotes etcd RangeStream to beta and enables it by default via the EtcdRangeStream feature gate. When paired with etcd v3.7, the API server reads whole collections, such as during watch cache initialization and uncached list fallbacks, through a streaming RPC instead of key-count-paginated Range calls. Previously a full page sat in memory on both etcd and the API server at once. With RangeStream, chunks are sized adaptively by bytes and are decoded and freed one at a time, which lowers peak memory and OOM risk. The API server falls back to paginated Range automatically on older etcd, and a non-zero etcd_request_duration_seconds_count{operation="listStream"} metric confirms the streaming path is in use.
- Kubernetes v1.36: Server-Side Sharded List and Watch
Kubernetes Blog hardMasterKubernetes v1.36 adds alpha server-side sharded list and watch (KEP-5866), letting the API server filter list responses and watch events so each horizontally scaled controller replica receives only its slice of a resource collection instead of deserializing and discarding the full stream. Clients set a new `shardSelector` in ListOptions using `shardRange()` over `object.metadata.uid` or `object.metadata.namespace`. The server matches objects by a deterministic 64-bit FNV-1a hash within `[start, end)`, so results are consistent across API server replicas, and `||` lets one replica cover non-contiguous ranges. Controllers inject the selector through informer `WithTweakListOptions`. They should check for `shardInfo` in the response metadata and fall back to client-side filtering when the server did not honor the selector.
- Slack’s Incident on 2-22-22
Slack Engineering mediumAdeptA Consul agent rolling restart caused Mcrib, Slack's memcached control plane, to replace and flush cache nodes, dropping hit rates during peak traffic. A cache-fronted Group DM membership query hit a Vitess keyspace sharded by user, so every miss became a scatter query across all shards. Database load grew superlinearly, queries timed out, caches couldn't refill, and client retries kept the system stuck in a metastable cascading failure. Recovery required throttling client boots, fetching only missing keys, reading from replicas and later moving the query to a channel-sharded table; Mcrib's very efficiency at replacing nodes increased cache churn and worsened the outage.
- Kubernetes Informers are so easy... to misuse!
Render mediumAdept
Kubernetes Informers report the eventual current state of an object, not every intermediate transition, so controllers must use level-driven logic rather than comparing oldObj and newObj in UpdateFunc. Each handler processes events serially and buffers pending notifications in an unbounded ring buffer, so a slow handler facing a large initial LIST (around 10^5 items at Render) can trigger an "OOMLoop" in which the Pod is OOM-killed and restarted repeatedly. Render recommends keeping handlers fast or using a workqueue, reading from the Informer cache instead of the API, filtering with WithTweakListOptions label selectors, and trimming objects with SetTransform. SetTransform carries a debugging cost, because zeroed-out fields can confuse tools and future teammates.
- Fixed cadence to seconds: making ClickHouse Cloud autoscaling more reactive
ClickHouse mediumAdept
ClickHouse Cloud's vertical autoscaling recommender moved from a fixed timer loop to a controller-runtime based design, so services that hit memory pressure get a recommendation in seconds instead of waiting for the next scheduled pass. Two sources, a slotted periodic sweep and a reactive poller, feed one rate-limited work queue. The queue provides deduplication, exponential backoff, bounded concurrency and leader election without custom code. The reactive signals live in ClickHouse: a materialized view filters breach rows such as soft memory rejections out of the Prometheus metrics firehose into a small TTL'd table ordered by timestamp, which is polled every few seconds with a cheap five-minute window query.