Skip to content

Namespace Redis keys; read DB user and name from Consul - #8

Merged
anshg1214 merged 1 commit into
mainfrom
chore/env-keyed-db-redis
Nov 22, 2026
Merged

anshg1214 merged 1 commit into
mainfrom
chore/env-keyed-db-redis

Conversation

@anshg1214

Copy link
Copy Markdown
Member

What

Lets environments that share a Postgres or Redis server keep their data apart, so Synapse test can run next to prod.

  • Redis: every key and the live-update pub/sub channel is prefixed with a configurable namespace.
  • Deploy template: the database user and name come from Consul instead of being hardcoded.

Why

Synapse test is being added on the same Postgres and Redis servers as prod. The template hardcoded user: synapse / dbname: synapse, and Redis used one fixed set of names. A Redis database number would not have been enough: pub/sub ignores it, so the live inbox channel synapse:inbox:<user_id> would have been shared between environments.

How

  • store.Redis wraps the go-redis client and carries a Namespace set once from redis.namespace (env SYNAPSE_REDIS_NAMESPACE). store.NewRedis and store.NewRedisClient read it from config, so no component is told the namespace separately.
  • Everything that names a key or channel goes through rdb.Name: the live publisher and hub (live.Channel(ns, userID)), the OAuth token cache, the rate limiter (including the Telegram buckets) and the Telegram connect keys.
  • An empty namespace leaves every name unchanged, so prod and beta behave as before.
  • deploy/config.yaml.ctmpl reads pg_user and pg_dbname with the same KEY helper as pg_password, and redis_namespace with OPTKEY (optional).
  • cmd/ingest now builds its client with store.NewRedisClient, with the same options as before.

Flags and config

  • New setting redis.namespace (SYNAPSE_REDIS_NAMESPACE), default empty.
  • New Consul keys, required in every environment: pg_user and pg_dbname. For prod and beta they are synapse and synapse, the current values.
  • New optional Consul key: redis_namespace.
  • Deploy order: the Consul keys must exist before this release is deployed. If they are missing, the render contains KEYDOESNOTEXIST_pg_user and run-synapse-command refuses to start.

Testing

  • Unit: TestConnectKeysFollowTheNamespace.
  • Integration: TestPublishGoesToTheNamespacedChannel and TestHubOnlyHearsItsOwnNamespace (live), TestMBIntrospector_CacheKeyFollowsNamespace (OAuth), TestBucketsStayInsideTheirNamespace (rate limit).
  • The e2e suite now runs under a namespace (e2e), so any component that ignores it fails a test. Full suite passes.
  • Template: rendered the old and new templates with stubbed Consul functions. With the keys present, prod output differs only by the new namespace: "" line; with the test values it renders synapse_test and namespace: "test". This did not use the real consul-template.
  • gofmt, go vet and go vet -tags integration are clean.

Reviewer guide

About 390 lines across 28 files. Suggested order:

  1. internal/store/redis.go and internal/store/namespace.go: the client wrapper and the one helper.
  2. internal/live, internal/oauth, internal/ratelimit, internal/adapter/telegram: each now names keys through rdb.Name.
  3. deploy/config.yaml.ctmpl, config.example.yaml, internal/config.
  4. Tests.

Environments that share a Postgres or Redis server need to keep their
data apart. The deploy template hardcoded the database user and name,
and every Redis key and the live-update channel used one fixed set of
names.

store.Redis wraps the client and carries a namespace set once from
redis.namespace (or SYNAPSE_REDIS_NAMESPACE). The live publisher and hub,
the OAuth token cache, the rate limiter and the Telegram connect flow
name their keys and channels through rdb.Name. An empty namespace leaves
every name unchanged.

The deploy template now reads pg_user and pg_dbname with the same KEY
helper as pg_password, and redis_namespace as an optional key. The keys
must exist in Consul before this release is deployed, or the render
fails at startup.
@anshg1214
anshg1214 merged commit b9eddc5 into main Nov 22, 2026
1 check passed
@anshg1214
anshg1214 deleted the chore/env-keyed-db-redis branch November 22, 2026 16:16
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.

1 participant