Blazing Fast APIs For Internet Scale DeFi: Performance Optimization Via Refactoring And New Components
The Elrond API infrastructure and code have recently undergone massive changes and refactoring. The resulting increases in performance and scalability have arrived in a timely manner for the highly anticipated debut of the Maiar Exchange.
The research for improvement opportunities and the results of the hard work required to implement them have yielded remarkable results: the increase in the amount of simultaneously connected users, better surge traffic management, and reduced response times.
One important change was a decision about the framework used. Others were a series of novel caching strategies to better handle requests, each of which resulted in faster response times, and overall made the entire API layer feel very snappy.
Here are the most important changes and improvements:
Transition from ExpressJS to NestJS
The NestJS framework is better suited for our micro-services architecture and has great tooling and documentation which will further allow us to build on its capabilities.
Caching synched with round times
An Elrond blockchain round takes 6 seconds. The data in the latest minted block thus can’t change for the entire duration of a round. We used this insight to always serve requests for the same data from the cache, greatly improving the response time for in-demand data at a particular point in time.
Our API servers have been fitted with an in-memory caching module that significantly reduces the amount of queries that need to be made towards the separate Redis server instances. This further reduces the time required for data to be made available towards requesters.
This new component constantly watches all blockchain transactions and triggers refreshes on outdated cached information. Thanks to this proactive fetching mechanism, the time to live for data has been increased from 1 minute to 1 hour. This means the computational intensive requests to update all data happen less often, and certain pieces of data are perhaps available even sooner than before. The TX processor takes special care about ESDTs and transactions related to them, such as minting or transfers.
This awesome component is available as an NPM package here: https://www.npmjs.com/package/@elrondnetwork/transaction-processor
This new component proactively updates certain pieces of data and places them directly into the cache, to avoid foreseeable computational intensive queries. For example, the list of active Validator nodes is refreshed every 1 minute and takes 30 seconds to generate. The Cache warmer proactively computes the list so the Elrond explorer has the info ready asap, without the need to need wait for it after making the query.
Internet Scale Data Management For (Almost) Anyone
If the infrastructure footprint required made you think twice before engaging with Elrond API infrastructure, great news! Thanks to the optimizations mentioned above, and several other improvements, we were able to reduce our infrastructure footprint by 80% and ended up with double the capacity.
Here’s what you need to become an Elrond API provider yourself:
- An Elrond Observer Squad
- A Redis server
- An Elastic Search Instance
- The Elrond API https://github.com/ElrondNetwork/api.elrond.com
The API layer is just one of the many and fascinating aspects of the Elrond technology stack. Dive into the fully open-sourced blockchain protocol and the vast array of tools surrounding it.