﻿# Rebuild an etcd watcher from a consistent snapshot after compaction

A watcher that falls behind compacted history needs a fresh baseline and a revision-linked resume, rather than skipping directly to the latest event stream.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/etcd-watch-compaction-resync/
Language: en

## A healthy cluster can outlive a watcher's history

A controller disconnects while many job records change. When it returns, the requested revision has been compacted. The datastore can be healthy and current while the controller's local cache is incomplete. Restarting a watch at the newest revision does not recover changes that were never applied locally. The operational failure is the missing baseline, not necessarily a broken network or lost datastore writes. A controller that schedules work from that cache can continue making plausible decisions from a state that no longer describes the keyspace it claims to observe.

## Read the compaction response as a state transition

The [Watch API](https://etcd.io/docs/v3.6/learning/api/) documents cancellation when the requested history is compacted and reports `compact_revision`. Reissuing the same unavailable start revision cannot restore the discarded history. Nor does changing the cursor alone repair local objects that should have been updated or removed. Treat the condition as an instruction to rebuild the observed range under an explicit recovery path. Preserve the previous cursor for diagnosis, identify which derived state depends on the range and prevent that state from being presented as fully current while its baseline is being replaced.

## Obtain one coherent replacement baseline

Read the required key range and retain the response revision for the snapshot being assembled. If pagination is necessary, pin subsequent pages to that same revision instead of combining independent latest reads. Otherwise a job can move between pages, appear twice or be missed while the controller builds its replacement cache. The API supports historical Range reads and a response indicating further results. If the chosen baseline revision itself becomes unavailable before the read completes, restart the baseline procedure according to a bounded policy; partial pages are not a completed coherent replacement.

## Connect the new watch to the installed snapshot

Watch start revision is inclusive. After installing a complete baseline, resume with the revision immediately following it so later changes are covered without pretending the baseline was an event replay. Keep installing the cache and advancing its durable cursor coordinated in the application. Advance only after all relevant events in a delivered revision have been applied. The [guarantees guide](https://etcd.io/docs/v3.6/learning/api_guarantees/) describes revision-atomic event delivery and resumption within retained history. A crash between updating derived objects and recording progress needs an explicit replay or deduplication policy at that local boundary.

## Verify deletions and revision boundaries

An isolated acceptance test disconnects the watcher, changes and deletes recognizable keys, compacts the needed history and then reconnects. Compare the rebuilt local view with the defined keyspace baseline and subsequent events. Include changes during pagination and a restart while applying one delivered revision. Acceptance requires deleted objects to disappear, retained objects to have the correct values and the recorded cursor to describe work actually applied. Simply receiving a newly created watch or a progress notification is insufficient evidence of a correct rebuilt cache or of current consensus state at every wall-clock instant.

## Keep ordinary catch-up separate from disaster recovery

[Restore-related revision handling](/journal/guide-dc-etcd-restore-watch-revisions/) addresses a different problem in which the datastore history itself can move backward after restoring a snapshot. Ordinary compaction recovery should not be described as reconstructing discarded historical events or recovering missing writes. [Endpoint identity and discovery](/journal/guide-dc-etcd-advertised-url-boundary/) helps verify that the replacement baseline came from the intended cluster. Watch delivery can be delayed and is not a linearizable read contract. The accepted controller records baseline identity, applied progress and remaining uncertainty, then uses a stronger read or conditional transition where an ownership decision requires it.
