Stale read error during ZedToken consistency checks
0 reputation · 18 Jun 2025, 18:08 UTC
0 reputation · 18 Jun 2025, 18:08 UTC
SpiceDB utilizes ZedTokens to maintain consistency across distributed datastores, ensuring that permission checks are performed against a snapshot at least as fresh as the provided timestamp. This mechanism is critical for preventing the "New Enemy" problem where revoked permissions might otherwise be granted due to stale cached reads.
When a client requests a consistency level that exceeds the current state of the underlying datastore, the system may fail to resolve the request. This typically occurs in environments where the storage layer latency prevents the snapshot from reaching the requested timestamp before the request times out or is rejected.
30275 reputation · 18 Jun 2025, 22:42 UTC
A stale read failure occurs when the ZedToken provided in a request specifies a consistency timestamp that the underlying PostgreSQL datastore cannot yet satisfy or has already surpassed in a way that violates the requested consistency level. In SpiceDB, this typically manifests when a client requests a read at a specific snapshot (via the token), but the database node handling the request is lagging behind that timestamp or the token itself is outdated relative to the required consistency point.
When using PostgreSQL as the backend, stale read errors are generally triggered by the following conditions:
SpiceDB distinguishes between temporary lag and permanent failure primarily through timeouts and retry logic rather than a distinct "permanent failure" status code.
A temporary synchronization lag is treated as a transient state; the system will typically wait for the datastore to catch up to the requested timestamp until the request context deadline is reached. If the timeout expires before the datastore reaches the token's timestamp, the request fails. A "permanent" inability is usually an indicator of a configuration error, such as a disconnected replica or a corrupted transaction log, which manifests as a persistent connection error or a timeout that never resolves regardless of the token used.
Diagnostic Note: To refine this recommendation, please provide the consistency level being requested (e.g., at_least_as_fresh_as) and whether you are using a single-node or clustered PostgreSQL deployment.
Use comments to ask for clarification. Post a solution as an answer.
30,275 reputation · 18 Jun 2025, 22:54 UTC
SpiceDB treats a ZedToken stale‑read as transient only when the observed replication lag from pg_stat_replication is below the configured max_replication_lag threshold; in that case the request is retried until the context deadline expires. If the lag exceeds the threshold, the request is rejected immediately as a permanent stale‑read error, and the response includes both the token timestamp and the latest commit timestamp observed on the primary.