Repository navigation
Fix/cache rebuild storm - #1168
Merged
Merged
Conversation
Production went down repeatedly with the next process's Prisma pool exhausted (P2024) and RDS refusing connections (P1001). Logs showed a background rebuild of one user's large addresses running for 7.7 hours (~23 min per 25k-tx address), restarting after a failure, while every new tx for an uncached address started another full-history rebuild inline. - One rebuild per address at a time in a process: concurrent callers (dashboard loads, each new tx) share it. - initPaymentCache starts the rebuild in the background instead of holding up tx handling until it finishes. - A rebuild invalidated while reading is no longer discarded (which made a busy address never finish caching). It writes its result, then re-applies the txs created, updated or price-linked since it started. - Catch-up writes require a new :cacheBuilt marker, checked atomically, so a cache cleared meanwhile isn't refilled with a few weeks and taken as complete. - After a failed rebuild, further requests for that address back off for 10 minutes instead of restarting the full read. - Users' dashboards are cleared after a rebuild so they include it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-ups to the rebuild changes: - The fallback write after a lost generation race was an unconditional SET, so it could land while clearPaymentCacheForAddress was between deleting the :cacheBuilt marker and deleting the week keys, restoring the marker over a partially deleted cache that catch-up then completed. Clears now bump a per-address clear epoch and set an expiring :cacheClearing flag while they delete. Every rebuild write (normal, fallback and catch-up) atomically requires the epoch read when the rebuild started and no clear in progress. New txs still only bump the generation, so they don't make a rebuild discard its result. A clear during the rebuild does. - If catch-up fails after the fallback write, the published weeks, marker and balance are cleared before rethrowing, so a cache missing the changes made during the read isn't taken as complete. Backoff and in-flight cleanup are unchanged. - initPaymentCache during a rebuild backoff no longer looks like a scheduled rebuild: it drops the address's balance and its users' dashboards (still without writing a lone payment week). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Loading the dashboard (or payment list) SCANned the whole Redis keyspace once per user address, twice per request, to find the address's week keys, then fetched each week with its own GET. With a large keyspace that is hundreds of thousands of sequential round trips, so uncached dashboards took minutes. Week keys are named after their week, so the candidates for each address are now derived from its oldest and newest tx (one grouped query that MySQL answers from the (addressId, timestamp) index alone), checked with pipelined EXISTS, and read with batched MGET. An address counts as cached if it has the built marker or any cached week, so caches written before the marker existed aren't rebuilt, and addresses with no payments stop being rebuilt (and holding the dashboard in "rebuilding") on every load once they have the marker.
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 2 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
…torm # Conflicts: # constants/index.ts # services/transactionService.ts
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.
No description provided.