Skip to content

fix(config): pick up system_config writes on every instance within a second - #616

Merged
Bccorb merged 1 commit into
mainfrom
fix/system-config-cross-instance-freshness
Nov 23, 2026
Merged

Bccorb merged 1 commit into
mainfrom
fix/system-config-cross-instance-freshness

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Closes #597. Part of #603.

Problem

getSystemConfig() cached system_config for five minutes per process, and invalidateSystemConfigCache() cleared only the instance that took the write. With several replicas, disabling a login method, removing an OAuth provider or turning on phishing_resistant_only stayed ineffective on the other instances for up to five minutes.

Change

Each instance still serves config from memory, but once the cached copy is older than SYSTEM_CONFIG_CHECK_INTERVAL_MS (1 second) it runs one fingerprint query:

SELECT md5(coalesce(string_agg(key || '=' || value::text, ',' ORDER BY key), '')) AS fingerprint FROM system_config

If the fingerprint matches the cached one, the cached copy is kept. If it differs, the rows are reloaded and validated as before. Writes on any instance, and direct database edits, apply everywhere within about a second. The writing instance still clears its own cache at once.

Details:

  • The fingerprint hashes stored content instead of reading updatedAt. That column is set from each instance's own clock, so clock skew between replicas could make a newer write look older, and a direct UPDATE need not touch it.
  • The fingerprint is read before the rows. A write landing between the two reads pairs an older fingerprint with newer rows, so the next check reloads. A current fingerprint is never paired with stale rows.
  • Concurrent callers share one in-flight load. A generation counter stops a load that started before invalidateSystemConfigCache() from repopulating the cache with pre-write values.
  • Fails closed: if the fingerprint query errors, the read throws instead of serving a copy that can no longer be shown to be current, and the failed check does not extend the cached copy.

Why a version check and not LISTEN/NOTIFY

LISTEN/NOTIFY was rejected:

  • Failure direction. A dropped listener connection fails silently toward stale config. Notifications sent while it is down are lost, so it would still need a polling fallback to be correct. That fallback is this check, and with it the listener adds nothing a security fix needs.
  • Deployment shape. LISTEN needs a dedicated session-level connection held outside the pool. It does not work through PgBouncer in transaction pooling mode, which managed Postgres setups commonly use.
  • Moving parts. A listener needs reconnect logic, a trigger or explicit NOTIFY on every write path including direct edits, and its own health reporting.

Cost of the version check: at most one query per instance per second, and only when config is actually read. The query hashes a table of a few dozen small rows and returns 32 bytes. There is no per-request cost inside the interval. No new service or migration is needed, which matches the direction for shared state in #581.

Tests

  • Unit (tests/unit/config/getSystemConfig.spec.ts): serving from memory inside the interval, keeping the cached copy on an unchanged fingerprint, reloading on a changed fingerprint, failing closed when the fingerprint query fails, sharing one load between concurrent callers, and the invalidation race.
  • Postgres (tests/postgres/systemConfigFreshness.spec.ts): two module instances against the migrated schema. A write with a backdated updatedAt, as from a replica with a slow clock, is picked up by the other instance once the interval passes.

Checks: npm run typecheck, npm run lint, npm run format:check, npm run test:run (195 files, 3446 tests) and npm run coverage (lines 99.28%, branches 95.82%) pass. The Postgres project (TEST_DB=postgres, 20 files, 285 tests) passes against a scratch database. A security review of the diff found nothing.

Docs

docs/configuration.md describes the new cache behaviour. docs/production-operations.md drops system_config from the per-process state table, because it now agrees between instances.

Patch changeset included.

…second

Each instance compares an md5 of the stored rows with its cached copy at
most once a second and reloads on a change, instead of caching for five
minutes per process.

Closes #597.
@Bccorb
Bccorb merged commit cc2a474 into main Nov 23, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(config): invalidate the system config cache on every instance

1 participant