Skip to content

refac(RPC Optimization): Eliminate Client-Side Duplication & Leverage Redis Caching#185

Merged
akiraonstarknet merged 7 commits intoakiraonstarknet:devfrom
EjembiEmmanuel:akira-dev
Oct 21, 2025
Merged

refac(RPC Optimization): Eliminate Client-Side Duplication & Leverage Redis Caching#185
akiraonstarknet merged 7 commits intoakiraonstarknet:devfrom
EjembiEmmanuel:akira-dev

Conversation

@EjembiEmmanuel
Copy link

📊 Problem Analysis

Root Cause Discovered

The homepage was making ~600 RPC calls from the browser on every page load because client-side atoms were duplicating server-side calculations.

Before Optimization:

  • Browser: ~600 RPC calls (client-side strategy.solve())
  • Server: ~100 RPC calls (API calculations)
  • Total: ~700 RPC calls per page load

🎯 Solution: Client-Side RPC Elimination

Key Changes Made

1. Eliminated Client-Side RPC Duplication

  • Removed strategy.solve() calls from strategiesAtomAsync
  • Added proper cache configuration to prevent unnecessary refetches
  • Result: 0 browser RPC calls on page load

2. Enhanced Redis Caching 🚀

  • Leveraged existing 30-minute TTL on full API responses
  • Cache hit = 0 RPC calls (the goal!)
  • Result: Instant page loads on refresh

3. Improved API Response 📊

  • Added tags to API response for client-side filtering
  • Components use API data instead of creating strategy instances
  • Result: Clean separation of concerns

📈 Performance Results

Scenario Before After Improvement
Page Load (Cold) ~700 RPC calls ~100 RPC calls 85% ⬇️
Page Refresh ~700 RPC calls 0 RPC calls 100% ⬇️
Subsequent Loads ~700 RPC calls 0 RPC calls 100% ⬇️

Key Insight: The 85% improvement comes from eliminating client-side duplication. The 100% improvement comes from Redis caching.

Vercel website link

https://troves-client-eight.vercel.app

Checklist before requesting a review

  • I have performed a self-review of my code
  • I assure there is no similar/duplicate pull request regarding same issue
  • My PR passes all checks (build, lint, formatting, etc)

- Replace direct instantiation of RpcProvider with a singleton pattern in a new provider module.
- Update all API routes to use the new getProvider function for consistent provider access.
- Adjust refetch intervals in balance atoms for improved performance.
- Implement a lazy initialization pattern for the Redis client to avoid
unnecessary connections.
- Update getDataFromRedis and setDataToRedis functions to use the new
getRedisClient method.
- Enhance error handling in the GET API route to manage Redis operation
failures gracefully.
- Upgrade starknetkit to version 3.3.0
- Add support for Keplr mobile app browser with a new connector.
strategy solving methods

- Wrap strategy solving logic in try-catch blocks to prevent crashes and
set safe defaults on errors.
- Improve error logging for better debugging.
- Ensure consistent handling of undefined values in strategy actions and
calculations.
@vercel
Copy link

vercel bot commented Oct 20, 2025

@EjembiEmmanuel is attempting to deploy a commit to the zkdice-xyz's projects Team on Vercel.

A member of the Team first needs to authorize it.

- Remove redundant strategy fetching logic from components and
API routes.
- Introduce shared strategy resources to minimize RPC overhead and
improve performance.
- Update API routes and components to utilize shared instances for
configuration and pricing.
Reduced the refetch interval for balance queries from 30 seconds to 5 seconds.
@akiraonstarknet akiraonstarknet merged commit f95f44e into akiraonstarknet:dev Oct 21, 2025
0 of 3 checks 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.

2 participants