MOU — Marvellous Onyema UgochukwuMarvellous Onyema
All posts
Backend/30 Oct 2026/5 min read

Caching without losing the truth

Redis should make the portal faster. It should never become the reason the portal is wrong.

The school portal has a lot of hot, read-mostly data: class lists, published fees, result summaries and staff dashboards. MongoDB was doing the same reads repeatedly. Redis was the obvious answer, but adding a cache to a system with money and academic records is a good way to introduce a second source of truth by accident.

Cache only, never authority

I added a small read-through helper instead of sprinkling Redis calls through controllers. Callers ask for a namespace and an id, and the helper either returns the cached JSON or runs the existing Mongo query once, stores the result with a TTL, and returns it.

The important rule is that Redis is never the source of truth. Every helper degrades to a cache miss when Redis is unavailable. A Redis outage costs latency, not correctness.

  • MongoDB stays authoritative for writes and cache misses.
  • PostgreSQL can act as a read mirror, with a fallback to Mongo if it is unreachable.
  • Redis is optional at boot; caching simply turns off.
  • Rate limiting and idempotency fail open when Redis is absent.
  • Background jobs run inline instead of disappearing.

The real test

The useful test is not “does the cache hit?” It is “what happens when the cache disappears?” If the answer is “the system starts degraded instead of failing”, the cache is in the right place.