Skip to content

Architect stale cache probabilistic eviction for high-throughput indexer API - #636

Merged
Damola-Sodiq merged 1 commit into
Sahara-Pay:mainfrom
El-swaggerito:Successfully-implemented-a-stale-cache-probabilistic-eviction-mechanism-for-Web3-webhook-ingestion-
Aug 30, 2026
Merged

Architect stale cache probabilistic eviction for high-throughput indexer API #636
Damola-Sodiq merged 1 commit into
Sahara-Pay:mainfrom
El-swaggerito:Successfully-implemented-a-stale-cache-probabilistic-eviction-mechanism-for-Web3-webhook-ingestion-

Conversation

@El-swaggerito

Copy link
Copy Markdown
Contributor

Closes #601

I've successfully implemented a stale cache probabilistic eviction mechanism for Web3 webhook ingestion. Here's what was delivered:

Implementation Summary

1. Stale Cache Service (src/services/staleCache.ts)

  • Implements stale-while-revalidate pattern with probabilistic eviction
  • Returns stale data immediately to avoid blocking the event loop
  • Probabilistically refreshes cache in background based on access frequency
  • Integrates with existing Count-Min Sketch for frequency tracking
  • Configurable via environment variables

2. Webhook Service Integration (src/services/webhookService.ts)

  • Integrated stale cache for webhook configuration lookups
  • Non-blocking reads during high load scenarios
  • Automatic cache invalidation on configuration updates
  • Maintains backward compatibility with existing Prisma schemas

3. tRPC Middleware (src/trpc/staleCacheMiddleware.ts)

  • Type-safe middleware for stale cache operations
  • withStaleCache() for query procedures
  • withCacheInvalidation() for mutation procedures
  • Maintains absolute type safety across tRPC boundary

4. Comprehensive Testing (src/services/tests/staleCache.test.ts)

  • Event loop non-blocking tests
  • High throughput tests (100+ concurrent operations)
  • Probabilistic refresh behavior validation
  • Error handling under load
  • Memory leak prevention tests
  • Input validation edge cases

Acceptance Criteria Met

Non-blocking asynchronous architecture: All cache operations return immediately, with background refreshes that don't block the event loop

Type safety across tRPC boundary: Middleware maintains full TypeScript type safety with generic type parameters

Backward compatibility: Integration with existing Prisma schemas without modifications

Edge case handling: Comprehensive input validation, error recovery, and graceful degradation

Performance under load: Tests demonstrate handling 100+ concurrent operations without event loop blocking

Configuration

Add to your .env file:

STALE_CACHE_THRESHOLD_MS=30000
STALE_CACHE_EXPIRE_MS=300000
STALE_CACHE_REFRESH_PROB=0.3
STALE_CACHE_DEFAULT_TTL=60
STALE_CACHE_ENABLE_REFRESH=true

The implementation leverages your existing probabilistic eviction infrastructure (Count-Min Sketch) and Redis caching layer, ensuring minimal overhead while providing significant performance improvements during heavy Web3 block finalization spikes.

@Damola-Sodiq
Damola-Sodiq merged commit fe9b4d0 into Sahara-Pay:main Aug 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Architect stale cache probabilistic eviction for high-throughput indexer API

2 participants