RPC Load Balancer
Currently, there are many options for RPC providers in web3, some are better in terms of speed, some are better in terms of availability, and developers have to choose only one between these many options which satisfies their needs and we all know no RPC provider exists that satisfies all of the needs of a web3 developer, we can solve this by giving developers option to load balance their RPC queries between multiple providers. As RPC requests are load balanced between multiple providers, users will get the best possible RPC provider at the time of making the request.
We are using 2 algorithms to facilitate this - Round Robin & Dynamic Balancing. Round Robin lets developers switch RPC providers after some successful requests from an RPC provider. Dynamic Balancing lets developers add some weights and options with which we will dynamically balance RPCs and use them.
We have built a JS/TS SDK which works well with ethers.js and web3.js.
This makes current interaction with web3 much faster, and easier and reduces the risk of low reliability as dApp will use multiple RPC providers.
This SDK not just load balances requests between multiple RPC providers but it also caches those request for some amount of time and then invalidates it, you can also choose which methods to exclude from caching, this is very crucial in increasing speed as methods like eth_chainId and similar can be cached for a longer time. It also retries once any requests fail with different provider thus increasing reliability of the whole system.
Check it out - svinge.satyamkulkarni.xyz
I didn't have previous experience in building load balancers from scratch but with some research, I was able to find some algorithms like round robin and dynamic balancing which helped in building this.
Caching is hard. It's not about caching but invalidating the cache at the right time so that data doesn't become stale. As we don't have a background job checking at the correct intervals for invalidating the cache, we had to come up with another way to invalidate the cache at the right moment.
The dynamic balancing algorithm has multiple weights based on which we do the sorting of RPC providers. This algorithm was particularly hard because sorting based on multiple parameters is hard, we solved this by trying various permutations and arriving at one that works and is very efficient, though have to admit, sometimes it chooses RPC provider which is slow just because its average time is very low because it has more requests than other providers, will improve it once I open source it and bring in the ethereum community to possibly contribute in it.