feat(caching): improve cache warming with limits, batching, metrics, …#1080
Open
abdulazizishaq212-prog wants to merge 1 commit into
Open
Conversation
…and deferred startup - Add CACHE_WARM_MAX_ENTRIES, BATCH_SIZE, BATCH_DELAY_MS, STARTUP_DELAY_MS to CACHE_WARMING constants (all env-var configurable with safe defaults) - Prioritise warmCoursesList by enrollment count with recency fallback - Prioritise warmUserProfiles by lastLoginAt recency with updatedAt fallback - Process user profiles in configurable batches with inter-batch delay to bound DB and memory pressure - Emit cache_warming_entries_total (Counter) and cache_warming_duration_seconds (Histogram) Prometheus metrics via MetricsCollectionService - Defer startup warming until after the server is accepting traffic by switching OnModuleInit to OnApplicationBootstrap + STARTUP_DELAY_MS pause - Update specs: MetricsCollectionService mock, enrollment-priority/fallback tests, batch-size assertion with fake timers, deferred-startup delay tests
|
@abdulazizishaq212-prog Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
Well done on the job done so far! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses five weaknesses in the cache warming pipeline: unbounded DB reads, unweighted entry selection, burst Redis
writes, no observability, and warming that competed with the boot sequence.
Changes
Configurable entry ceiling (CACHE_WARM_MAX_ENTRIES)
Every DB read in the warming pipeline now has an explicit take / limit derived from CACHE_WARMING.MAX_ENTRIES (default
500, overridable via CACHE_WARM_MAX_ENTRIES). Previously, warmCoursesList issued an unbounded find against the courses
table.
Real-signal prioritisation
recency fallback when the enrollment table is empty.
updatedAt DESC for fresh installs or seed data.
Batched processing with inter-batch delay
warmUserProfiles now processes the working set in slices of CACHE_WARMING.BATCH_SIZE (default 50, env:
CACHE_WARM_BATCH_SIZE), with a CACHE_WARMING.BATCH_DELAY_MS pause (default 50 ms, env: CACHE_WARM_BATCH_DELAY_MS)
between batches. This bounds peak Redis write throughput and heap allocation per warming cycle.
Prometheus metrics
Two new instruments added to MetricsCollectionService:
Every warm* method calls metrics.recordCacheWarming(target, keysWarmed, durationMs) after completing.
Deferred startup warming
CacheWarmingScheduler now implements OnApplicationBootstrap instead of OnModuleInit. NestJS fires onApplicationBootstrap
after the HTTP server is listening, so warming no longer competes with the tail end of the boot sequence. An additional
CACHE_WARMING.STARTUP_DELAY_MS grace period (default 5 s, env: CACHE_WARM_STARTUP_DELAY_MS) further defers the first
warmAll() call.
Environment variables (all optional)
┌─────────────────────────────┬─────────┬──────────────────────────────────────────┐
│ Variable │ Default │ Description │
├─────────────────────────────┼─────────┼──────────────────────────────────────────┤
│ CACHE_WARM_MAX_ENTRIES │ 500 │ Max rows fetched per warming target │
├─────────────────────────────┼─────────┼──────────────────────────────────────────┤
│ CACHE_WARM_BATCH_SIZE │ 50 │ User profiles processed per batch │
├─────────────────────────────┼─────────┼──────────────────────────────────────────┤
│ CACHE_WARM_BATCH_DELAY_MS │ 50 │ Delay between profile batches (ms) │
├─────────────────────────────┼─────────┼──────────────────────────────────────────┤
│ CACHE_WARM_STARTUP_DELAY_MS │ 5000 │ Grace period before startup warm-up (ms) │
└─────────────────────────────┴─────────┴──────────────────────────────────────────┘
Testing
startup delay assertion.
Files changed
closes #1053