How Do Ethereum Dapps Store Information?
Decentralized storage and its advantages
Ethereum Dapps are made up of a frontend, an Ethereum API to talk to the blockchain, and smart contracts. This is the entire architecture of a blockchain app. But when we compare this architecture to that of a traditional app, we can see one part missing, the database.
A traditional backend communicates with the database to receive and give information. So how does a Dapp handle this?
You might say that the Ethereum blockchain itself can be used for storage. While you would be right in that smart contracts on-chain can store states. The Ethereum blockchain was not designed to hold large data information.
Every node needs to be able to store all the blockchain data. If huge amounts of information were stored on the blockchain, every node would have to store all of it, which would not be feasible for the nodes.
And don’t forget about the gas fees. I can’t even imagine the costs of storing large data on-chain. So storing data on-chain is out of the question. Instead, we use databases that are decentralized, just like the blockchain.
IPFS — InterPlanetary File System
IPFS is a peer-to-peer hypermedia protocol. It is essentially a distributed, decentralized system for storing various types of information.
When you upload a file to the IPFS, the file is split into smaller pieces and cryptographically hashed. The file is then given a unique identification called a content identifier(CID) which can then be used to access the file.
The IPFS, like the blockchain, is made up of nodes and mapping called the distributed hash table(DHT). These nodes are where the information is stored. Every node only stores the data it is interested in. If one node wants to access some file on another node, they view the file using the DHT and the file’s CID, download it, and become another provider for the file.
Since IPFS acts like a blockchain, updating files is not possible. So you would have to push a new version of the file to the IPFS, which would get you a new CID. This is quite a headache as every time you update a file, you have to use a new CID.
To avoid this, we use something called IPNS or InterPlanetary Name System. IPNS solves this issue by creating an address that can be updated.
How to use IPFS in your Dapp
To use IPFS in your Dapp, you will need to store the IPFS hash. For example, you have users in your Dapp and store the username, email, and profile picture. You can store the username and email directly on the state of your user smart contract.
In the case of a profile picture, you can store it in the IPFS and store the hash in the smart contract state. This way, you can store the profile picture in the Ethereum blockchain for lesser gas fees.
You can also use services that help you work with IPFS for a seamless experience. These services help you organize information on the IPFS into folders, pin data automatically, and host websites onto the IPFS. Below we will look at two such services — Infura and Fleek.
They are best in class when it comes to connecting to the Ethereum blockchain in your Dapp. They also offer IPFS services that help manage all your IPFS data, such as retrieving, storing, and hosting. Very professional services and really easy to use. However, you will need to give your credit card details for creating a project using IPFS.
They are a little expensive, so you can consider using your own IPFS node or Fleek if you are a little tight on budget.
Fleek is another service that you can consider. The UI is really well done, and you can see all your folders and your files with their hashes. It is free at the start so you can experiment.
You can easily get started with the npm package, and you can click here to see usage examples. It is definitely not as good as Infura, but it is a great place to start.
IPFS protocol has allowed many more use cases of Dapps. It has achieved this through its decentralization and lower Ethereum gas fees.
Any Dapp that hopes to reduce operating costs but remain decentralized will have to use IPFS.
Additional reading — While IPFS handles content addressing, there is no guarantee that a file will exist forever if it is not pinned.
Filecoin is another protocol built on IPFS that addresses this issue to provide content persistence. FIlecoin, with its consensus protocols and incentives, when paired with IPFS, offer truly decentralized storage.
Here are some other articles you might like