trigger.dev icon indicating copy to clipboard operation
trigger.dev copied to clipboard

Webapp env vars *_REDIS_READER_HOST / *_REDIS_READER_PORT are declared but never consumed

Open brentshulman-silkline opened this issue 6 months ago • 2 comments

Summary

The webapp's env schema in apps/webapp/app/env.server.ts declares per-subsystem Redis reader env vars (CACHE_REDIS_READER_HOST, RATE_LIMIT_REDIS_READER_HOST, REALTIME_STREAMS_REDIS_READER_HOST, PUBSUB_REDIS_READER_HOST, ALERT_RATE_LIMITER_REDIS_READER_HOST, TASK_META_CACHE_REDIS_READER_HOST), each of which falls back to the global REDIS_READER_HOST / REDIS_READER_PORT. The self-hosting docs at docs/self-hosting/env/webapp.mdx document REDIS_READER_HOST as the way to use a Redis read replica.

However, no subsystem actually reads env.*_REDIS_READER_HOST — every consumer instantiates a single ioredis client off the primary *_REDIS_HOST/*_REDIS_PORT. Setting REDIS_READER_HOST on a self-hosted webapp is a complete no-op today.

Evidence

Repo-wide grep for any consumer of the reader-host names (excluding the schema declaration file):

$ grep -rn 'CACHE_REDIS_READER_HOST\|RATE_LIMIT_REDIS_READER_HOST\|REALTIME_STREAMS_REDIS_READER_HOST\|PUBSUB_REDIS_READER_HOST\|ALERT_RATE_LIMITER_REDIS_READER_HOST\|TASK_META_CACHE_REDIS_READER_HOST\|readerHost\|reader_host' apps/webapp internal-packages
apps/webapp/app/env.server.ts: …  # declarations only

Only one file matches — env.server.ts itself.

Example consumer (apps/webapp/app/services/taskIdentifierCache.server.ts:42-60):

function initializeRedis(): Redis | undefined {
  const host = env.CACHE_REDIS_HOST;
  if (!host) return undefined;
  return new Redis({
    connectionName: "taskIdentifierCache",
    host,
    port: env.CACHE_REDIS_PORT,
    username: env.CACHE_REDIS_USERNAME,
    password: env.CACHE_REDIS_PASSWORD,
    // … no CACHE_REDIS_READER_HOST anywhere
  });
}

The same single-client pattern is used in every other *_REDIS_HOST consumer: apiRateLimit.server.ts, realtimeClientGlobal.server.ts, services/realtime/v1StreamsGlobal.server.ts, tracePubSub.server.ts, projectPubSub.server.ts, magicLinkRateLimiter.server.ts, etc.

Compare with DATABASE_READ_REPLICA_URL, which is properly wired in apps/webapp/app/db.server.ts:249 — it constructs a second PrismaClient exposed as $replica. Prisma read splits work as documented; Redis ones don't.

Impact

Self-hosters who provision an ElastiCache reader endpoint (or any Redis replica) and set REDIS_READER_HOST per the docs see no traffic on the replica. We confirmed this on our production deploy: after binding REDIS_READER_HOST for ~24 h on 8 webapp pods, the replica node held a flat ~6 connections (cluster-internal replication only) and engine CPU was unchanged vs the primary. The Aurora half of the same change — DATABASE_READ_REPLICA_URL — worked as advertised and absorbed ~44 sustained connections plus ~3% CPU.

Suggested fix

Two reasonable shapes:

  1. Per-subsystem twin client. Wherever env.<NAME>_REDIS_HOST is consumed today, also accept env.<NAME>_REDIS_READER_HOST/PORT, and when present, construct a second ioredis client. Provide a small splitRedis({ writer, reader }) helper that routes read commands (GET/HGET/SMEMBERS/HGETALL/ZRANGE/EXISTS/etc.) to the reader and writes to the writer.

  2. Drop the orphan declarations. If a Redis read split isn't on the roadmap, remove the *_REDIS_READER_HOST/*_REDIS_READER_PORT entries from env.server.ts and the REDIS_READER_HOST row from docs/self-hosting/env/webapp.mdx so users don't waste deploy cycles wiring them up.

Happy to put up a PR for (1) if you'd like — would be useful to know if there's an intended design (e.g. is the eventual goal cluster-mode + read-from-replicas, or twin-client?).

Versions

Reproduced against main (current HEAD); also present in the v4.4.x release we run in prod.

brentshulman-silkline avatar May 16 '26 20:05 brentshulman-silkline

Quick scope confirmation before someone writes a PR. The issue lists 6 subsystem reader-host vars, but the pattern is wider than that — and the gap is total.

I grepped apps/webapp/app/env.server.ts and counted 17 *_REDIS_READER_HOST declarations across these subsystems:

ADMIN_WORKER ALERTS_WORKER ALERT_RATE_LIMITER BATCH_TRIGGER_WORKER CACHE COMMON_WORKER LEGACY_RUN_ENGINE_WORKER PUBSUB RATE_LIMIT REALTIME_STREAMS REQUEST_IDEMPOTENCY RUN_ENGINE_DEV_PRESENCE RUN_ENGINE_RUN_LOCK RUN_ENGINE_RUN_QUEUE RUN_ENGINE_WORKER RUN_REPLICATION SCHEDULE_WORKER Then I grepped for consumers across apps/, internal-packages/, and packages/:

grep -rln 'REDIS_READER_HOST|REDIS_READER_PORT' apps internal-packages packages
--include='.ts' --include='.tsx' | grep -v env.server.ts

(no matches)

Zero consumers anywhere in the repo. Every subsystem instantiates a single ioredis client off its *_REDIS_HOST / *_REDIS_PORT. The reader vars are scaffolding only — the docs entry for REDIS_READER_HOST in docs/self-hosting/env/webapp.mdx describes a feature that hasn't been wired up at all.

That changes the shape of the fix. Two paths:

Remove the dead schema + docs. Strip the 17 reader declarations from env.server.ts and the docs entry. Net ~200-line deletion. Honest, but it gives up on the read-replica feature for self-hosters who were hoping to use it. Wire up the reader clients. Add a second ioredis instance per subsystem that uses the *_REDIS_READER_HOST fallback when set, and route read-only commands through it. Real feature work — each subsystem has different read/write split needs, and ioredis doesn't auto-route, so each call site has to choose. Probably wants tests with testcontainers. Happy to do either, just want a maintainer call before opening anything — fixing only the 6 the issue names would leave the other 11 inconsistent, and (1) is large enough that I don't want to send it speculatively. Which way would you like to go?

rachit367 avatar May 19 '26 05:05 rachit367

Confirmed on main, and the surface is a bit wider than the list above.

apps/webapp/app/env.server.ts declares 17 *_REDIS_READER_HOST vars and 17 matching *_REDIS_READER_PORT vars: the global pair plus 16 subsystem ones.

ADMIN_WORKER            ALERTS_WORKER           ALERT_RATE_LIMITER
BATCH_TRIGGER_WORKER    CACHE                   COMMON_WORKER
PUBSUB                  RATE_LIMIT              REALTIME_STREAMS
REQUEST_IDEMPOTENCY     RUN_ENGINE_DEV_PRESENCE RUN_ENGINE_RUN_LOCK
RUN_ENGINE_RUN_QUEUE    RUN_ENGINE_WORKER       RUN_REPLICATION
SCHEDULE_WORKER

(TASK_META_CACHE_REDIS_READER_HOST from the original list isn't in the schema any more.)

Grepping apps, internal-packages and packages for any consumer of those names, or for readerHost / readerPort, returns nothing outside env.server.ts itself. So all 34 are inert.

The part that actually costs someone something is docs/self-hosting/env/webapp.mdx:33-34, which lists REDIS_READER_HOST and REDIS_READER_PORT as supported, with defaults of REDIS_HOST and REDIS_PORT. Someone reads that, points them at a read replica, gets no error, and no traffic moves.

Two ways to close it, and they're very different in size:

  • Wire up reader clients so the vars do what the docs say. That touches Redis client creation in every subsystem listed above.
  • Drop the reader vars and the two docs rows, and revisit if read replicas are actually wanted later.

Which would you prefer? I'll take the docs side if that's the call. I didn't want to guess, since one of these is a feature and the other is a deletion.

BILLKISHORE avatar Aug 10 '26 11:08 BILLKISHORE