Skip to content

fix(cache): log startup error when MemoryBackend is selected in production#223

Closed
anshul23102 wants to merge 1 commit into
Coder-s-OG-s:mainfrom
anshul23102:fix/215-memory-backend-warning
Closed

fix(cache): log startup error when MemoryBackend is selected in production#223
anshul23102 wants to merge 1 commit into
Coder-s-OG-s:mainfrom
anshul23102:fix/215-memory-backend-warning

Conversation

@anshul23102
Copy link
Copy Markdown

Summary

Fixes #215.

`pickDefaultBackend` silently fell back to `MemoryBackend` when neither `KV_REST_API_URL+KV_REST_API_TOKEN` (Upstash) nor `REDIS_URL` (self-hosted Redis) were set. In a serverless deployment (Vercel) each Lambda invocation is an isolated process with its own in-memory store, so `MemoryBackend` provides no cross-request state sharing. Rate limits, deduplication, and any other shared-state feature were silently non-functional in production without any log evidence.

Changes

`src/lib/cache.ts`

  • Adds a `console.error` in `pickDefaultBackend` that fires when `MemoryBackend` is selected and `NODE_ENV === 'production'`. The message identifies which env vars need to be configured, making the misconfiguration immediately visible in Vercel function logs.
  • Exports `__pickBackendName` (test-only) — a pure function that mirrors the backend-selection logic and accepts env vars as arguments, enabling unit tests to assert backend selection without module reloading.

`src/lib/cache.test.ts`

  • Adds a `backend selection` suite (6 tests) covering: Upstash selection, IoRedis selection, MemoryBackend fallback, Upstash-over-Redis priority, production warning fires, and warning absent outside production.

Behaviour

Environment Redis config Result
any Upstash vars set UpstashBackend (no warning)
any REDIS_URL set IoRedisBackend (no warning)
development / test none MemoryBackend (no warning, intended)
production none MemoryBackend + console.error startup log

Test plan

  • npx vitest run src/lib/cache.test.ts - 27/27 pass (21 existing + 6 new)
  • npx tsc --noEmit - no errors
  • npx eslint src/lib/cache.ts src/lib/cache.test.ts - no errors

…ction

MemoryBackend is intentional for local development and tests, but in a
serverless deployment (e.g. Vercel) each Lambda invocation gets its own
isolated in-process store. Rate limits and any other feature that requires
shared state across requests therefore have no effect when MemoryBackend
is active.

Previously, the fallback to MemoryBackend was completely silent. Operators
had no indication from deployment logs that rate limiting was non-functional
when neither KV_REST_API_URL+KV_REST_API_TOKEN (Upstash) nor REDIS_URL
(self-hosted Redis) were configured.

Changes:
- pickDefaultBackend now emits a console.error startup message when it
  selects MemoryBackend and NODE_ENV === 'production', so the
  misconfiguration is immediately visible in Vercel function logs.
- Adds __pickBackendName (test-only export) so the backend selection logic
  can be asserted in unit tests without reloading the module.
- Adds six backend-selection tests to cache.test.ts covering all three
  backends, the Upstash-over-Redis priority, and the production warning.

Closes Coder-s-OG-s#215
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 25, 2026

@anshul23102 is attempting to deploy a commit to the codersogs-3057's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions
Copy link
Copy Markdown

Hey @anshul23102

You have 5 open PRs right now. The limit is 3 at a time.

Please get your existing PRs merged or closed before opening new ones:

This PR will remain open but won't be reviewed until you're under the limit. See our Contributing Guidelines for details.

@anshul23102
Copy link
Copy Markdown
Author

Temporarily closing to stay within the 3-open-PR limit. The fix is complete and tested (27/27 pass). Will reopen once one of the security PRs (#221 or #222) is merged.

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.

[Bug] MemoryBackend silently selected when Redis is unconfigured, making all rate limiting per-Lambda-invocation and effectively disabled in production

1 participant