﻿# Use the etcd election key to identify leadership, not its announcement text

A leader announcement is application data; use the election ownership key and creation revision when guarding a coordinator’s etcd mutations.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/etcd-election-leader-key-generation/
Language: en

## The same announcement can describe a different leader lifetime

A coordinator advertises its hostname as the election value. After a restart, the same hostname appears again, and a consumer assumes ownership never changed. That conclusion confuses application data with the coordination object. The [Election reference](https://etcd.io/docs/v3.6/dev-guide/api_concurrency_reference_v3/) returns a leadership key with a creation revision and lease. Its proclamation value can be updated while leadership continues, and a new lifetime can reuse familiar text. Preserve the returned ownership identity rather than interpreting a matching label as proof that an earlier decision remains authorized.

## Read the individual election operations

`Campaign` waits to acquire leadership, `Proclaim` changes the elected value, `Leader` returns the current proclamation and `Observe` streams proclamations in order. These operations serve different purposes. The [pinned Go client](https://pkg.go.dev/go.etcd.io/etcd/client/v3@v3.6.0/concurrency) also exposes the leader key and revision separately from the value. A process waiting in a campaign is not already the leader, and a watcher receiving an announcement does not obtain authority to mutate the coordinator's state. Record which operation produced the evidence before deciding what it establishes.

## Guard mutations with the ownership identity

The API describes comparing the leader key's creation revision with the returned ownership revision inside a transaction. Combine that condition with the expected job state before changing related etcd records. The [transaction semantics](https://etcd.io/docs/v3.6/learning/api/) make the comparisons and selected branch atomic within the keyspace. A separate `Leader` read followed by an unconditional write leaves a race if leadership changes between them. Comparing only announcement bytes can also accept a recreated ownership object with the same application label.

## Recover through observed state rather than a remembered role

When the session stops refreshing or a connection outcome is uncertain, suspend new leader-only publication under the old identity. Re-establish the appropriate election state and inspect whether another lifetime already advanced the job. Do not infer continuity solely from the same process name returning in monitoring. Keep logical job identity, campaign attempt and accepted ownership separate. Existing work can sometimes be reused, but its acceptance depends on current inputs and the new authority decision rather than the worker's memory of having previously won.

## Test a repeated label and a changed generation

An isolated fixture deliberately reuses the announcement text across distinct leadership lifetimes. Attempt a guarded update with the earlier key and revision after the successor takes over. Acceptance requires rejection without modifying the successor's state. Also change a proclamation during a valid lifetime to check that consumers do not mistake ordinary value updates for new ownership. Preserve transaction outcomes and returned identities. A test that only observes one leader name at a time cannot establish correct handling of stale commands or repeated application labels.

## Limit the guarantee to the protected state

Election ownership does not atomically execute an external job or undo requests sent before authority changed. [External operation reconciliation](/journal/provider-post-retry-idempotency-ledger/) remains a separate requirement. [Local lock lifetime](/journal/guide-storage-file-lock/) concerns another identity mechanism with different release rules. The accepted coordinator distinguishes who currently owns the election, what that owner announced and which state transition passed an ownership check. This avoids treating an informational stream as a universal authorization service or reporting exactly-once external execution merely because a single elected coordinator was visible.
