This article tries to answer a very common question that often arises whenever we are working on a Web3 Application.
To make an informed decision about what to keep on Blockchain, we must know and consider all the variables involved. We should take into account the cost, ease of access, and security. These requirements can vary depending on the projects and application use case, but an application with good architecture will take care of all the criteria mentioned above. Let’s discuss all of them in detail.
Though we sometimes think blockchain at its core is just a database, when we comprehend the difference in cost for storing data in a database versus storing it on the blockchain, we realise that the latter is far more secure and costly than the former. To give an idea of the cost let’s see the cost of storing a 256-bit word: As of 17 Oct 2022, a gas is worth 0.000000012 ETH and if you plan to store data or let’s say a 256-bit word it will cost you 20,000 gas. A kilobyte is thus 640k gas or 0.00768 ETH or 9.70 USD as per the current rate of Ethereum which is $1309. This means to store a 100kb file you will be paying around $1000.
One thing that we can be sure of is that once we put some data on the blockchain, it can never be mutated without the consent of the authorized person in other words data stored on the blockchain is immutable. This makes it an ideal place to store information like ownership, the balance of tokens of any game, trade records, and so on.
Ease of Access
While deciding on what should be stored on the blockchain, one should also keep in mind the retrieval process of that information. We cannot execute complex queries while fetching data from the blockchain like we do traditional SQL or NoSQL databases. Due to this, we have to structure the smart contract variables in such a fashion that makes it easier or in some cases possible to read the data.
We also need to make sure we don’t apply searching on large datasets and use appropriate data types to avoid large gas fees.
Size of the Data
As discussed above, storing even 100 Kilobytes of data can cost around a thousand dollars which makes it extremely un-economical to store large amounts of data for example assets on the blockchain. A common solution to this is storing large assets on a distributed file system like IPFS or FILECOIN.
This is a common practice when working with NFTs where the image of the NFT and the metadata both are stored on IPFS and the link to those files is stored on the blockchain.
One other way is to delegate authority. We can authorize a certain user and trust him to provide genuine data which can be validated in smart contracts using asymmetric cryptography, although this solves the issue of storage in some cases but can make the app centralised.
Here is an example of delegating authority using signatures:
Data Structure Combinations
In Solidity, we can use mappings for storing data that is queried frequently. Mappings work on Hash maps which gives us constant lookup times. More often than not, mappings are used in combination with arrays and structs ( custom data types ) as mappings cannot be iterated on their own and structs help to store complex data.
Blockchain is no doubt the most secure and reliable place to store information but when using a complex tool, we should weigh the requirements and decide which part of the project needs to be kept on the chain and which can work off-chain. More often than not, the non-sensitive data will be better if stored in our traditional database systems and use blockchain for crucial data to make complete use of blockchain features while keeping costs in check.