advanced25 min studyLesson 353 in Engineering

List and watch: rebuild state when history expires

Establish a collection baseline, resume supported watches, and handle lost history without mistaking bookmarks for periodic heartbeats.

On this page

What you’ll understand

  • Explain the baseline-and-change relationship in list/watch.
  • Recover correctly from an expired resource history with HTTP 410.
  • Distinguish watch bookmarks and cache resync from a fresh authoritative list.

A controller needs a usable current view, not a promise that one connection will deliver every historical transition forever. Listing establishes a collection baseline, and watching follows changes under the API’s resource-version rules. Connections can break, retained history can expire, and the controller itself can restart. Designing around those facts turns watch events into reliable prompts for reconciliation rather than an assumed permanent event log.

Bind the change stream to a known baseline

In the classic list-then-watch pattern, obtain a list and its collection resourceVersion, then request changes from that boundary using the documented watch parameters. For a paginated list, follow continue tokens consistently to complete the snapshot rather than mixing pages from unrelated baselines. The collection token is server-provided and opaque; it does not count the number of objects or elapsed seconds. A list with no objects can still establish a meaningful boundary. Client libraries usually handle much of this protocol, but your controller still depends on their recovery behavior.

A local materialized view applies the returned changes and schedules affected keys. A handler should not assume an event means it is seeing the only intermediate state: later reads can already contain newer changes, and notifications may lead to coalesced work. The controller’s invariant should be computable from current relevant objects. If a product needs an immutable audit history of every action, an ordinary informer cache and reconcile queue are not substitutes for a separately specified durable event log.

Npages=⌈NobjectsL⌉N_{\rm pages}=\left\lceil\frac{N_{\rm objects}}{L}\right\rceil
This is a request-count estimate for a nonempty snapshot with exactly L objects per full page and a final partial page. Real servers can return fewer results or require further continuation; clients must follow continue tokens, not this estimate, to detect completion.↔ Scroll wide equations horizontally.

Expired history requires a new state boundary

When a watch request refers to history the server no longer retains, the API can return HTTP 410 Gone. The client must rebuild its view from a new list and restart watching from the new valid baseline, rather than retry the same expired token forever. Replace the obsolete cached view so objects absent from the fresh snapshot are not retained as current. A framework’s reflector can perform this recovery, but application code must remain correct when the reconstructed state differs from what individual old events would have shown. External effects therefore need the repeatable reconciliation design established in the first lesson.

A watch BOOKMARK, when requested and sent, reports progress to a resource-version boundary without carrying a complete object update. Clients cannot assume bookmarks arrive at a fixed interval or arrive at all. They are not a substitute for connection timeouts or a guaranteed liveness heartbeat. Similarly, a client-cache resync can requeue known objects without proving a fresh server list occurred. Distinguish reprocessing cached state from relisting authoritative state, and check the documented behavior of the client library version you use.

Bsnapshot≈Nobjects sB_{\rm snapshot}\approx N_{\rm objects}\,s
s is mean serialized bytes per listed object and B is snapshot payload in bytes, excluding framing, compression, and client overhead. Repeated full relists can be expensive even when each controller ultimately needs only a few keys.↔ Scroll wide equations horizontally.

Recover without creating a relist storm

Use backoff and cancellation for reconnect attempts, avoid unnecessary independent watches for the same resource scope, and watch metrics for repeated expiration or slow processing. A short history window with many lagging clients can amplify load through repeated relists. The API’s storage and cache details can evolve, so base correctness on the documented versioned API semantics rather than treating etcd revisions as your application’s resourceVersion format. Readiness should wait for the relevant initial cache synchronization before code relies on that view.

The queue lab illustrates a processing-rate imbalance. Reset, set λ=4 and μ=3, predict backlog 4+t, then Play or Step. Reset and set both λ=2 and μ=3 to predict drainage at t=4. This fluid model has no watch tokens, list snapshots, bookmarks, or expiration rule. It explains why a slow consumer can accumulate pending work, while the actual 410 recovery still needs the discrete baseline protocol. Editing either control preserves elapsed time, so use Reset for the fresh comparison.

Count a relist budget without interpreting the token

A test snapshot contains 1,000 objects averaging 2,000 serialized bytes. The server returns four pages of 250 objects with consistent continue tokens. A later reconnect receives 410 for an expired watch boundary.

  1. The snapshot payload estimate is 1,000×2,000=2,000,000 bytes, or two decimal MB, before framing and compression. Four pages are observed in this constructed example, not inferred as a universal server guarantee.

  2. After the 410 response, rebuild using a fresh consistent list and its new collection token. Completing another same-sized four-page snapshot transfers another two MB of estimated object payload.

  3. Across those two snapshots, eight list requests and approximately four MB of payload were counted. The old and new token strings do not reveal that traffic or a time gap; those quantities came from request counts and measured object sizes.

The result

A new list repairs the state boundary after lost history. Blindly retrying the expired token cannot restore the missing retained history.

Explore the model

Interactive lab

Interactive lab · queue

Consumer lag is a capacity question

Reset, set λ=4 and μ=3, predict backlog 4+t, then Play or Step. Reset and set λ=2 and μ=3 for drainage. This fluid queue does not implement list/watch, bookmarks, cache resync, or HTTP 410 recovery.

Consumer lag is a capacity questionFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. 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 forProcessing capacity affects lag, while correct state recovery requires a separate API protocol.

Check your understanding

A watch requested with an old resourceVersion receives HTTP 410 Gone. Which recovery matches the classic list/watch model?

Try it yourself

A relist includes 600 objects averaging 1,500 bytes. Estimate payload for three complete relists, and explain why asking for bookmarks does not guarantee one every second.

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

Give me a hint

Multiply objects by mean size and round count; distinguish optional progress markers from an interval promise.

Compare with the solution

One snapshot is 900,000 bytes, so three total 2,700,000 bytes before overhead or compression. The documented bookmark facility does not promise a fixed cadence or even delivery of a bookmark; clients still need supported reconnect and timeout behavior.

Take it with you

  • A collection baseline and its watch boundary must agree.
  • HTTP 410 recovery rebuilds current state rather than resurrecting expired history.
  • Bookmarks, cached resync, relisting, and audit logs have different guarantees.

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.