feat: pgdog.min_lsn read-your-writes replica routing floor#1156
Open
mediprtl wants to merge 1 commit into
Open
feat: pgdog.min_lsn read-your-writes replica routing floor#1156mediprtl wants to merge 1 commit into
mediprtl wants to merge 1 commit into
Conversation
Add an opt-in read-your-writes floor. A client sets the `pgdog.min_lsn`
connection parameter (startup option or SET) to a commit LSN it must observe;
PgDog then routes the read only to a replica that has replayed at least that far.
- Reads carrying `pgdog.min_lsn` are filtered to replicas whose replayed LSN
(from the existing LSN monitor) is >= the floor.
- When no replica qualifies, PgDog raises NoReplicaCaughtUp (SQLSTATE 58000)
carrying a catch-up ETA ("eta ~Ns"), derived from the replica's replication lag
in time (now() - pg_last_xact_replay_timestamp()), so a client can size a retry.
The rejection logs at debug (expected backpressure), not error.
- Optional `min_lsn_primary_fallback`: an unmet floor falls back to the primary
instead of erroring.
- `pgdog.min_lsn` is registered as an untracked parameter; inert when unset or on
setups without replicas.
Includes unit tests (routing, NoReplicaCaughtUp, ETA-from-lag) and a pgx
integration test.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sticky reads via a per-read LSN floor - #912
Part of #912 (@levkk asked for a PR there). Opt-in read-your-writes that keeps
reads on replicas instead of pinning tables to the primary.
A client sets
pgdog.min_lsn(startupoptions=-corSET pgdog.min_lsn='X/Y'),intercepted like
pgdog.role. The load balancer routes the read only to areplica whose replayed LSN (already tracked by the LSN monitor) is >= that floor.
If none qualify it errors recoverably with
NoReplicaCaughtUp(SQLSTATE 58000)carrying a catch-up ETA from the replica's replication lag, or — opt-in —
falls back to the primary.
Addressing the concerns from #912
rides on the async message that triggers the read — so there's no round-trip to
race. (PG 19's
WAIT FOR LSNmakes client-side LSN a first-class primitive too.)per-driver-plugin direction you described — it's a small primitive those can
build on.
never serves a stale read.
Testing
pgdog.min_lsnhas run continuously in our production and sandbox for 3+ weeks,built on the LSN monitor PgDog already ships (it reads the monitor's tracked
replayed LSN — no new poller).
Production — 2 read replicas
pgdog.min_lsnover the window.caught up before the client stopped retrying and fell back on the client side.
No stale reads — routing is conservative (a read only goes to a replica
whose replayed LSN is ≥ the floor).
NoReplicaCaughtUpresponses (clientsretried): most reads were served immediately or after one short wait; the floor
only holds a read back until the replica catches up.
~50 min over the window. The floor adapts — the reported ETA averaged ~1 min
and tracked actual lag (max ~26 min).
Sandbox — 1 read replica
NoReplicaCaughtUpresponses over ~4 weeks; lower lag / lighter load.Coverage — unit tests (replica-filter routing,
NoReplicaCaughtUp+ETA-from-lag) and a pgx integration test in
integration/load_balancer/pgx/.Notes / happy to adjust
min_lsn_primary_fallbackis currently a bool in[general]— glad to fold itinto an existing routing enum if you'd prefer.
replay_lag_secondslives on the pool'sLsnStatswrapper; can move it intopgdog_stats::LsnStatsif that's the better home.