Comparing the Performance and Cost of dApps Using On-Chain and Off-Chain Event Data Storage

Sam Vishwas
4 min readMar 29, 2023

When building a decentralized application (dApp) on Ethereum, accessing and processing event data from smart contracts is a common requirement. However, the cost and performance of event data access can vary depending on whether the data is stored on-chain or off-chain.

PC: sanfranciscotribe.com

In this article, we’ll compare the cost and network performance of two dApps: dApp-1 and dApp-2, which are connected to the same Ethereum smart contract called SC-1. SC-1 emits two events: event-1 (which has 3 index attributes and 2 non-index attributes, all of which are less than or equal to 32 bytes in size) and event-2 (which has 2 non-index parameters). dApp-1 accesses events from the blockchain directly, whereas dApp-2 uses event streams to load event data into Firestore database and then accesses events from there.

Indexed vs. Non-Indexed Event Parameters

Before we dive into the performance and cost comparison, let’s first review the difference between indexed and non-indexed event parameters.

Indexed event parameters are stored in the “topics” section of the event log, which enables efficient filtering and querying of event data. Non-indexed event parameters, on the other hand, are stored in the “data” section of the event log, which can make filtering and querying less efficient.

Indexed event parameters can offer significant benefits in terms of filtering and querying event data, but they can also increase gas costs for emitting events. This is because indexed parameters require additional calculations to store them in the “topics” section of the event log. However, the cost savings and query performance improvements offered by off-chain event storage using event streams and Firestore can more than offset the additional gas costs for indexing event parameters.

Performance and Cost Comparison

Let’s now compare the performance and cost of dApp-1 and dApp-2 for accessing event data from SC-1.

dApp-1 directly queries event data from the blockchain. This approach can be slow and expensive, especially when querying large amounts of event data. This is because querying events requires interacting with the blockchain, which can be slow due to network conjunctions and costly due to the need to pay gas fees for each transaction.

dApp-2, on the other hand, uses event streams to load event data into Firestore database and then accesses events from there. This approach can be much faster and more cost-effective than directly querying events from the blockchain. Event streams provide real-time updates of event data, and Firestore’s indexing capabilities enable efficient filtering and querying of event data. By optimizing smart contract and event data schema design to take advantage of these off-chain storage and access methods, dApp developers can significantly improve the performance and user experience of their applications.

Accessing indexed parameters from event is equally performant for both dApp-1 and dApp-2, disregarding blockchain network congestion. However, querying non-indexed parameters of event-2 can prove to be a time-consuming and expensive process when accessing data directly from the blockchain. This is because non-indexed parameters are not optimized for searching in the smart contract. Yet, off-chain storage solutions such as Firebase can provide a more efficient and cost-effective alternative. By storing event-2 data in an off-chain database, queries can be performed much faster and at a lower cost compared to querying the blockchain directly. Additionally, off-chain storage offers a more scalable and flexible solution for storing large amounts of event data, which can be challenging to achieve with on-chain storage. Therefore, when it comes to accessing non-indexed parameters of event-2, off-chain storage solutions like Firebase can be an elegant and smooth option to enhance performance and reduce costs.

Conclusion

When building dApps on Ethereum, event data access and processing is a common requirement. By understanding the benefits and trade-offs of on-chain and off-chain event data storage and access methods, dApp developers can optimize their applications for better performance and cost-effectiveness.

If you’re interested in learning more about event indexing and its effects on gas costs and query performance, I recommend taking a look at the following resources.

Moralis Streams, which allows developers to stream real-time blockchain data directly into their applications, without the need for complex infrastructure or setup. This can greatly improve the speed and efficiency of event data access, as well as reduce costs associated with maintaining and managing a blockchain node.

Firebase, a cloud-based platform that provides various services for mobile and web application development. Firebase includes Firestore as one of its core services, which can be used for storing and querying event data from blockchain applications. Firebase also provides real-time database functionality, which can be useful for receiving real-time updates on event data.

Understanding event logs on the Ethereum blockchain,” a Medium article by Luit Hollander.

To learn more about Firestore and its benefits for storing event data, check out “Get to know Cloud Firestore,” a Google playlist that provides a comprehensive guide on how to use Firestore for various use cases.

Listen to Blockchain Events In Real Time With Moralis Streams API & Firebase” by Juhani Ahokas, a Moralis Instructor, on YouTube. This video tutorial provides step-by-step instructions on how to set up a real-time event streaming pipeline using Moralis and Firebase.

In conclusion, accessing and analyzing event data from blockchain applications can be a complex and challenging task, but with the right tools and resources, it can be made much easier and more efficient. By using services such as Moralis Streams and Firebase, developers can greatly improve the speed and accuracy of their event data access, while also reducing costs and improving overall application performance. So if you’re working on a blockchain application that relies on event data, be sure to check out these tools and resources to make the most of your data and improve your application’s performance.

--

--

Sam Vishwas

Experienced software architect available for work. 25+ years of design & development experience. Blockchain enthusiast skilled in multiple languages.