Repository navigation
fix(config): pick up system_config writes on every instance within a second - #616
Merged
Merged
Conversation
…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.
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.
Closes #597. Part of #603.
Problem
getSystemConfig()cachedsystem_configfor five minutes per process, andinvalidateSystemConfigCache()cleared only the instance that took the write. With several replicas, disabling a login method, removing an OAuth provider or turning onphishing_resistant_onlystayed 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: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:
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 directUPDATEneed not touch it.invalidateSystemConfigCache()from repopulating the cache with pre-write values.Why a version check and not LISTEN/NOTIFY
LISTEN/NOTIFY was rejected:
NOTIFYon 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
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.tests/postgres/systemConfigFreshness.spec.ts): two module instances against the migrated schema. A write with a backdatedupdatedAt, 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) andnpm 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.mddescribes the new cache behaviour.docs/production-operations.mddropssystem_configfrom the per-process state table, because it now agrees between instances.Patch changeset included.