Web3 dapps architectures

Alberto Molina
Coinmonks
5 min readJun 2, 2022

--

The very first thing that any developer notices when moving into the web3 industry is that dapps architectures can be substantially different from traditional web2 web applications.

In web2 we have a Front End, and a Back End (it can be a back for front or just a web api) but in web3 we have a third component in the mix “the blockchain” which can either completely or partially replace the backend.

The best way in my opinion to present the different architectural options is to go one component at a time.

Front End

The Front end can be either centralized (returned from a server like in web2) or decentralized (returned from IPFS for example, a decentralized storage network that can hold all sorts of files, including html pages).

Decentralized front ends are immutable and always available (like blockchain smart contracts) but they can be harder to manage (upgrade etc…).

Back End

Blockchain directly

You can choose to use your smart contracts as APIs, your FE will interact directly with the blockchain to read and write data (like an SPA interacting with web APIs directly). In this model, your FE will connect to a wallet (like metamask) that will authenticate the user and allow him to select the network to interact with (mainet, testnet, …) and validate or reject the transactions to be sent to the blockchain.

You will not have to deal with client sessions nor use traditional authorization and authentication protocols like Oauth2 or OpenID, you will simply rely on the public/private key cryptography (the founding stone of blockchain) offered by the wallet which is the element that will enable your dapp to interact with the blockchain.

It cannot get any more web3ish than that!

Blockchain directly and Backend

It could also happen that your dapp not only stores data on the blockchain but also stores data on your backend server (for confidentiality issues for example). In this scenario your FE will communicate with both, the Blockchain and the BE:

The FE to Blockchain interaction will be managed by a wallet connected to the FE (just like in the “Blockchain directly” model described above).

The FE to Backend interaction will be managed in a traditional web2 way (user session, cookies, etc…).

Blockchain directly (only write) and Backend

This model is identical to the previous one the only difference is that you could use your backend to store your smart contracts’ state, that way your FE will directly interact with the blockchain when sending transactions but it will not read data from the blockchain (which can actually be an issue from the performance point of view), instead it will read form your backend.

This approach trades some decentralization for performance. There are some companies like Moralis that already offer backend services to track your contract’s state and boost your dapp performances.

Blockchain through Backend with user’s wallet

A completely different way to deal with your blockchain smart contracts would be to force your users transactions to go through the back end. There are two ways to implement this:

The first option is to make your users use the FE wallet to sign the blockchain transactions. Those transactions will be sent to your backend that will simply forward them to the blockchain. This can give you the possibility to double check if the user made a mistake before delivering the transaction to the blockchain.

The second option is to make your users use the FE wallet to sign messages instead of transactions (check the EIP-712). These signed messages cannot be sent to the blockchain since they are not transactions, instead, your backend will bunch together many users signed messages and use a different account (a backend wallet) to send all the messages to the blockchain in one single transaction. By doing that, it is the backend account that will pay the transaction fees, users won’t spend a penny.

In both cases, users use their private keys to sign the data that gets eventually transferred to the blockchain, no way for your backend to forge anything. The problem is that your backend could get hacked which could compromise the exchange of information between your users and the blockchain.

Blockchain through Backend with backend’s wallet

The least decentralized way to deal with your blockchain (but probably the simplest one for your users) is to manage everything from your backend.

Users will not use any FE wallet, they will not sign anything, they will communicate with your backend and it is the backend wallet that will generate and sign the transactions to send to the blockchain.

Your backend is in full control, it can forge anything which makes this approach very attractive to hackers.

It cannot get any more web2ish than that!

--

--