ADR-00026 — MongoDB TTL Collection vs. Redis for the Checkout-Session Cache

By Engineering Team•October 2, 2026•5 min read
  • Status: Approved / Implemented
  • Date: 2026-10-02
  • Authors: Engineering Team

Context

The checkout-session cache (shop-service’s checkout_session collection, read directly by order-service — see ADR-00025) needs a short-lived store with automatic expiry: a record that matters for roughly an hour and then should disappear without anyone having to clean it up. A purpose-built in-memory cache (Redis) is the conventional choice for this shape of data. The question this ADR settles is whether to stand up that infrastructure now, or to use the MongoDB server every service already depends on — which natively supports TTL-based expiry via an expireAfterSeconds index — and defer Redis until there’s a concrete reason to pay for it.

This is not a one-off choice specific to checkout. user-secure-engine already stores OTP codes the same way — a MongoDB otp_codes collection, TTL-indexed on expires_at (expireAfterSeconds: 0, lifetime from OTP_CODE_TTL_MINUTES, default 5 minutes) — so this ADR is documenting and formalizing a pattern already in production use for a second, independent short-lived-state need, not introducing a new idea for checkout alone.


Decision Drivers (FRs & NFRs)

See SR-012 for the full requirement list.

  • FR-10 (Session Time-To-Live): the checkout-session record MUST expire automatically after a fixed, short window.
  • NFR-05 (Flagged, Temporary Direct Cross-Service Read): the current direct-Mongo-read mechanism behind the cache is explicitly temporary, pending a dedicated cache service (e.g. Redis-backed) as its long-term replacement — this ADR is the record of why that replacement hasn’t happened yet and what would trigger it.

Options Evaluated

Option 1 — A MongoDB TTL collection, reusing the server every service already runs (CHOSEN)

checkout_session is a normal MongoDB collection with an expireAfterSeconds: 0 index on an absolute expire_at timestamp — the same mechanism user-secure-engine’s otp_codes collection already uses for OTP expiry. No new infrastructure, no new client library, no new operational surface: every service in this monorepo that needs a MongoDB connection already has one.

  • Pros:
    • Zero new infrastructure cost at this stage. No Redis instance (or cluster, for any real availability guarantee) to provision, size, patch, back up, or monitor, and no added monthly run-cost — a meaningful consideration while the product has not yet reached a traffic level that would strain MongoDB’s TTL-reaper mechanism.
    • TTL expiry is native, not hand-rolled. expireAfterSeconds is a first-class MongoDB index feature — no background reaper job, no cron, no extra code to write or maintain for cleanup.
    • Consistent with an already-implemented pattern. user-secure-engine’s otp_codes collection already solves a structurally identical problem (short-lived, tenant-scoped state that must disappear on its own) the same way. Choosing the same mechanism for checkout sessions means one operational pattern to understand, not two different caching stacks for two different features.
    • Low engineering lift. No new client, no new connection pool, no new client-side library to wire through dependency injection in either Go service.
  • Cons & Accepted Trade-offs:
    • Not an in-memory store. A MongoDB read/write is slower than a Redis round-trip under real load — acceptable today because checkout and OTP volumes are both low relative to what would make that latency difference actually matter.
    • No native pub/sub or cache-invalidation signaling. Not needed by either consumer today (checkout-session reads are always by-token lookups; OTP reads are always by-key lookups), but would be missing if a future feature needed it.
    • This is the reason the direct-Mongo cross-service read (behind TD-03) exists at all. Because there is no dedicated cache-service API to front this data, order-service reads shop-service’s collection directly via a shared database connection instead of through a real service boundary — a direct consequence of this ADR’s choice, not an unrelated shortcut.
    • Will not scale indefinitely. A MongoDB TTL index reaps lazily (a background task, not instant-on-expiry), and write/read volume on a shared collection will eventually compete with the service’s other collections for the same server’s resources as traffic grows.

Option 2 — A dedicated Redis-backed cache service

Stand up a Redis instance (or managed Redis service) behind a small dedicated cache-service API, and have shop-service write checkout sessions (and, eventually, user-secure-engine write OTP codes) through it instead of through MongoDB.

  • Pros:
    • Purpose-built for exactly this shape of data — in-memory reads/writes, native TTL, far lower latency at scale than a disk-backed collection.
    • Fronting the cache with a real service API removes the direct-cross-service-database-read exception entirely, resolving TD-03’s schema-drift risk as a side effect — order-service would call an API, not hand-maintain a mirrored bson struct.
    • Removes load from the primary MongoDB server as traffic grows.
  • Cons (Why Rejected For This Stage):
    • Real, ongoing infrastructure cost before there’s traffic to justify it. A new stateful service needs provisioning, an availability/backup story, and monitoring — cost and operational surface the product doesn’t yet need to carry.
    • Non-trivial engineering lift today. Building and wiring a new cache-service API (and migrating two independent, already-working features — checkout sessions and OTP codes — onto it) is meaningful work with no immediate correctness or feature benefit; the current MongoDB-based implementations already satisfy every functional requirement.
    • Premature for the product’s current scale. Nothing observed today indicates MongoDB’s TTL mechanism is a bottleneck for either consumer.

Final Decision

Option 1 is selected and implemented, consistently, for both the checkout-session cache (shop-service’s checkout_session) and OTP codes (user-secure-engine’s otp_codes) — a deliberate, cost-conscious choice to reuse existing MongoDB infrastructure and its native TTL support rather than stand up Redis before the product’s scale calls for it.

This decision is explicitly temporary and scoped to the product’s current stage, not a permanent architectural stance. It should be revisited — migrating the checkout-session cache (and, at the same time or separately, OTP codes) to a dedicated Redis-backed cache service — when either of these becomes true:

  • Read/write latency or throughput on the MongoDB-based cache measurably degrades checkout or OTP-send performance, or
  • Overall traffic grows enough that the cost of standing up and operating Redis is clearly justified by the performance or infrastructure-load benefit.

Until then, TD-03’s direct-cross-service-read exception remains in place as this decision’s direct consequence, and SR-012 NFR-05 continues to flag it as temporary rather than as settled architecture.