Commit dff6202
authored
fix(cache): implement distributed lock for cache stampede prevention (JhaSourav07#2132)
## Description
This PR introduces a structural architectural fix to prevent Distributed
Cache Stampedes (Thundering Herd) when a highly requested user or
organization dashboard cache expires.
Prior to this PR, the DistributedCache had no cross-instance locking. If
an L2 (Redis) cache expired during high traffic, every concurrent
serverless instance would experience a cache miss and send identical
GraphQL queries to GitHub at the same exact millisecond, easily
triggering secondary rate limits and causing 500s.
This PR solves the vulnerability by introducing a distributed Redis
Mutex lock and formalizing a getOrSet pattern.
**Key Changes**
1. Added getOrSet to DistributedCache: Encapsulates both L1
Promise-based deduplication and L2 Redis Mutex locking (SET lock:KEY 1
NX PX 10000).
2. Graceful Instance Polling: When an edge instance fails to acquire the
mutex (meaning another instance is already fetching the data), it
gracefully polls the Redis cache waiting for the data to populate rather
than blindly spamming GitHub APIs.
3. Stale-While-Revalidate (SWR) Support: Upgraded getOrSet with an
optional shouldFetch predicate. This enables fetchGitHubContributions to
continue fetching "delta syncs" securely by retaining the 7-day stale
cache while acquiring a lock.
4. Refactored lib/github.ts: Stripped out legacy, manual Map locks
(pendingContributions, etc.) and fully delegated fetching logic to
DistributedCache.
Fixes JhaSourav07#1979
## Pillar
- [ ] 🎨 Pillar 1 — New Theme Design
- [ ] 📐 Pillar 2 — Geometric SVG Improvement
- [ ] 🕐 Pillar 3 — Timezone Logic Optimization
- [x] 🛠️ Other (Bug fix, refactoring, docs)
## Visual Preview
## Checklist before requesting a review:
- [x] I have read the `CONTRIBUTING.md` file.
- [x] I have tested these changes locally
(`localhost:3000/api/streak?user=YOUR_USERNAME`).
- [x] I have run `npm run format` and `npm run lint` locally and
resolved all errors (CI will fail otherwise).
- [x] My commits follow the Conventional Commits format (e.g.,
`feat(themes): ...`, `fix(calculate): ...`).
- [x] I have updated `README.md` if I added a new theme or URL
parameter.
- [x] I have started the repo.
- [x] I have made sure that i have only one commit to merge in this PR.
- [x] The SVG output matches the CommitPulse "premium quality" aesthetic
standard (no raw elements, smooth animations, correct fonts).
- [x] (Recommended) I joined the CommitPulse Discord community for
contributor discussions, mentorship, and faster PR support.3 files changed
Lines changed: 133 additions & 65 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
| 214 | + | |
214 | 215 | | |
215 | 216 | | |
216 | 217 | | |
| |||
369 | 370 | | |
370 | 371 | | |
371 | 372 | | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
372 | 490 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | 254 | | |
259 | 255 | | |
260 | 256 | | |
| |||
300 | 296 | | |
301 | 297 | | |
302 | 298 | | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | 299 | | |
323 | 300 | | |
324 | 301 | | |
| |||
397 | 374 | | |
398 | 375 | | |
399 | 376 | | |
| 377 | + | |
400 | 378 | | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
| 379 | + | |
412 | 380 | | |
413 | | - | |
| 381 | + | |
414 | 382 | | |
415 | 383 | | |
| 384 | + | |
416 | 385 | | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
| 386 | + | |
421 | 387 | | |
422 | 388 | | |
423 | 389 | | |
424 | | - | |
| 390 | + | |
| 391 | + | |
425 | 392 | | |
426 | 393 | | |
427 | 394 | | |
| |||
579 | 546 | | |
580 | 547 | | |
581 | 548 | | |
582 | | - | |
583 | | - | |
584 | | - | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | | - | |
| 549 | + | |
589 | 550 | | |
590 | 551 | | |
591 | 552 | | |
592 | | - | |
| 553 | + | |
| 554 | + | |
593 | 555 | | |
594 | 556 | | |
595 | 557 | | |
| |||
627 | 589 | | |
628 | 590 | | |
629 | 591 | | |
630 | | - | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
| 592 | + | |
637 | 593 | | |
638 | 594 | | |
639 | 595 | | |
640 | | - | |
| 596 | + | |
| 597 | + | |
641 | 598 | | |
642 | 599 | | |
643 | 600 | | |
| |||
945 | 902 | | |
946 | 903 | | |
947 | 904 | | |
948 | | - | |
949 | | - | |
950 | | - | |
951 | | - | |
952 | 905 | | |
953 | 906 | | |
954 | 907 | | |
| |||
983 | 936 | | |
984 | 937 | | |
985 | 938 | | |
986 | | - | |
987 | | - | |
988 | | - | |
989 | | - | |
990 | 939 | | |
991 | 940 | | |
992 | 941 | | |
993 | 942 | | |
994 | | - | |
| 943 | + | |
995 | 944 | | |
996 | 945 | | |
997 | 946 | | |
| |||
0 commit comments