feat: implement Redis cache invalidation strategy#148
Open
elijah4196 wants to merge 1 commit into
Open
Conversation
- Reduce fee config CACHE_TTL from 3600s to 600s (10 min) - Add CacheInvalidationLogger utility with structured pino logging - Add KYC status cache invalidation on updateUserKYCLevel - Add CacheTags.apiKeys() and apiKeyService invalidation contract - Add fee config invalidation logging in FeeService private methods - Add POST /admin/cache/flush endpoint protected by requireAdmin closes Pidoko257#106
|
@elijah4196 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! 🚀 |
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.
feat: implement Redis cache invalidation strategy
Summary
Implements a coherent Redis cache invalidation strategy across the ProxyPay API, addressing inconsistent TTLs and missing invalidation hooks for user-specific data. This PR introduces event-driven invalidation for user caches, corrects reference data TTLs, and adds an admin endpoint for emergency cache flushing with full structured audit logging.
Changes
1. Reduce fee config TTL (
src/services/feeService.ts)CACHE_TTLfrom3600(1 hour) to600(10 minutes) so fee changes propagate across instances within an acceptable window.2. Cache Invalidation Logger (
src/utils/cacheInvalidationLogger.ts)CacheInvalidationEventinterface,CacheInvalidationTriggerunion type, andlogCacheInvalidation()function.{ event: "cache_invalidated", key?, pattern?, trigger, adminId?, timestamp }.3. KYC status cache invalidation (
src/services/kyc.ts)updateUserKYCLevel()now invalidatescache:kyc:{userId}vialayeredCache.del()immediately after the DB write succeeds.cache_invalidatedlog withtrigger: "kyc_level_update".4. API key cache tag (
src/services/cachedQueryManager.ts)CacheTags.apiKeys(userId)static method returninguser:{userId}:apikeys.5. API key cache invalidation contract (
src/services/apiKeyService.ts)cache:apikeys:{userId}key pattern (TTL 600s) and exportinginvalidateApiKeyCache(userId)for use at mutation sites when API key CRUD is implemented.6. Fee service invalidation logging (
src/services/feeService.ts)invalidateCache(id)andinvalidateAllCaches()now calllogCacheInvalidation()withtrigger: "fee_config_change"after flushing from LayeredCache.7. Admin cache flush endpoint (
src/routes/admin.ts)POST /api/admin/cache/flush?key={pattern}route protected byrequireAdminmiddleware.layeredCache.delPattern(pattern)and emits acache_invalidatedlog withtrigger: "admin_flush"and the admin's user ID.{ flushed: true, pattern }on success; 400 ifkeyparam is missing/empty; 403 if caller is not admin.Cache Key Registry
cache:kyc:{userId}updateUserKYCLevelcache:apikeys:{userId}fee_config:{id}fee_config:activecache:country:listcache:asset:metadata:{assetCode}Requirements Addressed
updateUserKYCLevel✅POST /admin/cache/flushendpoint with auth, logging, validation ✅CacheInvalidationLoggerwith structured pino events for all triggers ✅Migration Notes
All changes are fully backward-compatible. No database migrations required. The only behavioral change visible to existing code is the reduced fee config TTL (3600s → 600s), which means cached fee configs will refresh more frequently — this is intentional and safe.
Testing
All changes are additive and non-breaking. Existing test suites continue to pass. The implementation uses:
layeredCacheL1+L2 infrastructure (no new Redis connections)requireAdminmiddleware for the flush endpointcloses #106