The Mongoose Caching Problem (And How We Solved It)
From 300 RPS to 700+ RPS: A 2.2x Performance Breakthrough
When you're building a Node.js application with MongoDB, you quickly hit a wall: your database can't handle the query volume. You add caching. But caching is fraught with pitfalls-cache stampedes, stale data, invalidation nightmares.
We built @mongoose-performance-cache to solve this once and for all. Here's what we learned.
The Problem: Why Your Mongoose App Feels Slow
Let me paint a scenario. You have a typical SaaS API:
// User authentication - runs thousands of times
app.get('/api/user/:id', async (req, res) => {
const user = await User.findById(req.params.id);
res.json(user);
});
// Popular reports - same query, different users
app.get('/api/reports/active', async (req, res) => {
const reports = await Report.find({ status: 'active' });
res.json(reports);
});
Each request hits MongoDB. At 100 concurrent users:
- 500+ database queries per second
- MongoDB CPU at 85%+
- P99 latency: 500ms+
- Users complaining: "Why is this so slow?"
You need caching. But here's where it gets tricky.
Caching is Hard (For Good Reason)
Traditional caching with Redis is deceptively simple:
// Naive caching approach
async function getActiveReports(req, res) {
const cached = await redis.get('active_reports');
if (cached) return res.json(JSON.parse(cached));
const reports = await Report.find({ status: 'active' });
await redis.set('active_reports', JSON.stringify(reports), 'EX', 300);
res.json(reports);
}
Looks clean. But in production, it breaks in three catastrophic ways:
Problem 1: The Cache Stampede
It's 2:59:55 PM. Your cache expires in 5 seconds.
2:59:55 - 100 concurrent requests arrive
2:59:59 - Cache expires
3:00:00 - All 100 requests query MongoDB
(not 100 queries in sequence, but 100 in parallel)
↓
MongoDB CPU: 95%+, P99 Latency: 2000ms+
This is called a cache stampede or thundering herd. Your database crashes right when it matters most.
Problem 2: Invalidation Hell
You need to update a report:
// Update the report
await Report.findByIdAndUpdate(reportId, { status: 'completed' });
// Now what? The cache says it's still 'active'
// Do you clear the entire 'active_reports' cache?
// Do you clear all report caches?
// Do you manually track which queries are affected?
Most teams pick option 2: clear everything. Your hit rate plummets.
Problem 3: Concurrent Writes
Multiple requests update the same document:
Request A: Update report status → Queue update → Cache in 50ms
Request B: Update report name → Queue update → Cache in 100ms
What gets cached? The result of B (name change), but A's status change is lost.
The Solution: Intelligent Caching
We built @mongoose-performance-cache to solve all three problems automatically.
1. Cache Stampede Protection
When multiple concurrent requests hit the same uncached query, only one database query executes. The rest share the result.
import { initCache } from '@mongoose-performance-cache';
const cache = initCache({ ttl: 600 });
cache.applyCacheToQueries(ReportSchema);
// 100 concurrent requests
const reports = await Report.find({ status: 'active' });
// Result: 1 MongoDB query, 100 shared responses
// Not 100 queries. Not throttled. Just smart.
Impact: Eliminated cache stampedes entirely. No more thundering herd.
2. Smart Query-Aware Invalidation
Here's where it gets clever:
// This query is cached
const activeReports = await Report.find({ status: 'active' });
// Update report - change the name
await Report.findByIdAndUpdate(reportId, { name: 'Q4 Report' });
// What happens to the cache?
// The system automatically determines:
// "Does 'name' appear in the query filter?"
// "No. So don't invalidate."
// ✅ Cache remains valid
// Now update the status
await Report.findByIdAndUpdate(reportId, { status: 'completed' });
// The system determines:
// "Does 'status' appear in the query filter?"
// "Yes. Invalidate this specific query."
// ✅ Only relevant queries cleared
This isn't guessing. It's pattern matching on your actual query filters.
Impact: 70% higher hit rates in write-heavy workloads. The cache stays valid longer.
3. Batch Write Optimization
Instead of flushing each cache write individually:
Traditional: 1000 writes → 1000 Redis operations → Network overhead
v1.2: 1000 writes → Batch queue → 40 Redis operations (deduped)
The library groups cache writes into 50ms batches, automatically deduplicating them. If the same key is written twice, only the latest write persists.
Impact: 60% fewer Redis operations. Smoother latency.
The Results: Benchmarks That Matter
We stress-tested this on real hardware (Lenovo 14 Ada: 2-core @ 1.2GHz, 5.88GB RAM) with a realistic workload:
v1.1 Performance (Before)
├─ Throughput: 300-350 RPS
├─ P50 Latency: ~120ms
├─ P95 Latency: ~250ms
└─ P99 Latency: ~500ms
v1.2 Performance (After)
├─ Throughput: 705-708 RPS ⬆️ 2.2x
├─ P50 Latency: ~70ms ⬆️ 42% faster
├─ P95 Latency: ~95ms ⬆️ 62% faster
└─ P99 Latency: ~150ms ⬆️ 70% faster
Test details: 50 concurrent clients, 60-second duration, 70% read / 15% write / 15% aggregate operations.
Production Grade: The Audit Story
Before shipping this, we ran a comprehensive audit across 45,534 real-world operations:
What we verified:
- ✅ Zero data corruption (100% success rate)
- ✅ Proper invalidation semantics (every edge case tested)
- ✅ Memory safety under load (no OOM crashes)
- ✅ Serialization accuracy (BSON types handled correctly)
- ✅ Distributed cache consistency (Redis pub/sub working)
Data Integrity Score: 9.6/10
We found (and fixed) three minor issues:
- Size estimation was ±80% off (now ±20%)
- $pull operator missing type safety (now validated)
- Geospatial queries weren't cache-safe (now rejected)
All fixes were straightforward. No architectural flaws. No data corruption vectors.
How It Works: Just Three Lines
import { initCache } from '@mongoose-performance-cache';
// 1. Initialize once
const cache = initCache({
ttl: 600, // 10 minutes
enableSmartInvalidation: true // Smart invalidation
});
// 2. Apply to your schemas
cache.applyCacheToQueries(UserSchema);
cache.applyCacheToQueries(ReportSchema);
cache.applyCacheToQueries(ProjectSchema);
// 3. Use Mongoose normally
const users = await User.find({ status: 'active' });
const reports = await Report.find({ published: true });
// That's it. Caching works automatically.
// Invalidation happens automatically.
// No manual cache busting.
The Architecture: Why It's Different
Traditional caching treats the cache as a simple key-value store. We treat it as an extension of Mongoose itself.
Standard Caching:
User Logic → Cache Layer → Database
(dumb, knows nothing about queries)
Our Approach:
User Logic → Mongoose Hooks → Smart Cache → Database
(knows query structure)
(understands invalidation)
(prevents stampedes)
Key innovations:
- Query-aware invalidation: Matches update operations against cached query filters
- Inflight coalescing: Shares database results across concurrent requests
- Batch deduplication: Groups cache writes, eliminates duplicates
- Memory circuit breakers: Prevents OOM by monitoring heap pressure
- Event loop protection: Prevents background operations from starving requests
When to Use This
Perfect for:
- High-traffic APIs (100+ req/s)
- Read-heavy workloads (70%+ reads)
- Applications with repeating queries
- Teams that want caching without the complexity
Not needed for:
- Strongly consistent requirements (financial transactions)
- Very low traffic (<10 req/s)
- Queries that are always unique (no repeating patterns)
The Numbers: What You Actually Save
On a typical SaaS backend (100 concurrent users, 70% cache hit rate):
Without caching:
├─ Database queries: 3000/sec
├─ MongoDB CPU: 85%+
├─ P99 latency: 500ms
└─ Database cost: $2000/month
With @mongoose-performance-cache:
├─ Database queries: 900/sec (-70%)
├─ MongoDB CPU: 25%
├─ P99 latency: 150ms (-70%)
└─ Database cost: $400/month (-80%)
That's $19,200/year in saved database costs. Plus your users get 70% faster responses.
Why We Open Sourced This
We built this for production use. It's battle-tested on real traffic. We open sourced it because:
- Caching shouldn't be hard. Teams shouldn't have to solve this themselves.
- The problem is universal. If you use Mongoose at scale, you need this.
- Collaboration improves everything. We want the community to find edge cases we missed.
Getting Started
Install:
npm install @mongoose-performance-cache
Or on Bun:
bun add @mongoose-performance-cache
Full documentation: https://github.com/VictorAjadi/mongoose-cache
The Questions We Know You Have
Q: What about cache invalidation consistency across multiple servers?
A: We use Redis pub/sub for distributed invalidation. All instances sync automatically.
Q: Can I use this with existing caching middleware?
A: Yes. Our cache layer is independent. Works alongside other caching strategies.
Q: What happens if Redis goes down?
A: Automatic fallback to in-memory cache. No errors. Service continues working.
Q: Will this work with my existing MongoDB queries?
A: Yes. Zero code changes needed. Just call applyCacheToQueries(schema) once.
Q: How do I monitor cache performance?
A: Built-in stats: cache.getStats() returns hits, misses, hit rate, memory usage, etc.
What's Next
We're planning:
- Cache analytics: Deep insights into query patterns and hit rates
- Custom invalidation strategies: Plugin system for complex invalidation logic
- GraphQL support: Automatic caching for GraphQL queries
- Geospatial query support: Currently geo queries ($near, $geoIntersects) are rejected because exact coordinates make caching hard. We're exploring geohash-based caching (round coordinates to varying precision levels) and TTL stratification (shorter TTLs for precise queries).
-
Multi-database adaptation: The same patterns (stampede protection, smart invalidation, batch optimization) work for any database. Coming next:
- PostgreSQL (Beta Q1 2025)
- MySQL (Q2 2025)
- DynamoDB, Cassandra, SQLite (Community-driven)
Join Us
This library is open source and actively maintained. Contributions welcome:
- Report bugs or edge cases
- Submit feature requests
- Share your performance results
- Help with documentation
The Takeaway
Caching is hard. But it doesn't have to be.
With @mongoose-performance-cache, you get:
- 2.2x faster responses
- 70% fewer database queries
- Zero cache invalidation complexity
- Production-grade reliability
All with three lines of code.
Try it on your next project. Or your existing one. Let us know what you think.
Ready to make your Mongoose app faster?
npm install @mongoose-performance-cache
Happy caching! 🚀
Questions? Found a bug? Have a feature idea?
Open an issue on GitHub or reach out to us on LinkedIn.
Performance numbers based on stress testing with 50 concurrent clients on Lenovo 14 Ada (2-core @ 1.2GHz, 5.88GB RAM). Results may vary based on hardware, query complexity, and data size.
Top comments (0)