Skip to content

Fix/cache rebuild storm - #1168

Merged
Klakurka merged 4 commits into
masterfrom
fix/cache-rebuild-storm
Nov 26, 2026
Merged

Klakurka merged 4 commits into
masterfrom
fix/cache-rebuild-storm

Conversation

@Klakurka

Copy link
Copy Markdown
Member

No description provided.

Klakurka and others added 3 commits November 26, 2026 10:14
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.
@Klakurka Klakurka self-assigned this Nov 26, 2026
@Klakurka Klakurka added the bug Something isn't working label Nov 26, 2026
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 106bbd16-1e22-43a8-926f-06912b7d6e17
Commits

Reviewing files that changed from the base of the PR and between a6e3cf0 and 128fc59.

Files selected for processing: 5

  • constants/index.ts
  • redis/clientInstance.ts
  • redis/paymentCache.ts
  • services/transactionService.ts
  • tests/unittests/paymentCache.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

…torm

# Conflicts:
#	constants/index.ts
#	services/transactionService.ts
@Klakurka
Klakurka merged commit 322457b into master Nov 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant