A Beginners Guide: Web3 Architecture
Web3 is the future of the internet and its architecture differs greatly to its predecessor, web2. Let’s take Twitter, a Web2 application, as an example and look at its architecture
Database: Stores data such as users, posts, tags, and comments.
Frontend End: Frontend development handles the design and interface of the web application.
Backend End: backend development focuses on what happens behind the scenes, which defines the business logic. ie. login logout, posting etc
Thus, when a users publishes a tweet on Twitter, the essential are
- Interaction with its frontend / backend / database
- All business logic (code) is hosted on centralized servers (AWS, Azure)
- User interact with the website via internet browser
This is a simple example of how Web2 applications work today.
However, everything changes with web3!
Introduction to Web3 Application
The idea behind the whole process is simple. As a general idea, Web3 can be defined as an application that is using a decentralized network. The concept is that the internet evolves into a completely new infrastructure that is based on blockchain technology.
Basically, Web3 is the new age of web development. This technology enables users to control data privacy and data ownership instead of third parties, like Twitter.
Let's take a look at some basic terms;
Web3 runs on what are called permissionless blockchains. In addition, Web3 technologies provide the foundation for P2P, payments, services and marketplaces.
Blockchain will enhance the process of how data is collected and managed across the web. This technology will bring together all the useful data with the help of AI and IoT. In addition, data in blockchain is immutable and cannot be tampered.
dApp is a decentralized application that operates on a blockchain or peer-to-peer network of computers. It allows users to participate in transactions directly with one another, rather than depending on a central authority.
So what the difference with Web3??
Twitter is the intermediary that controls your privacy data and data ownership. The backend logic and data resides on centralized servers and databases. In order to complete a function, ie a tweet or login, the centralized application needs to authenticate it.
Web3, on the other hand, is decentralized and stores the data and logic (smart contacts) on a distributed state machine. State machine can be defined as a large data structure which holds not only all data like accounts and balances, but a machine state, which can change from block to block according to a pre-defined set of rules, and which can execute arbitrary machine code.
Thus, in a web3 application, the user needs to be authenticated by the decentralized state machine. After user authentication, functions, ie transactions, are executed by the user and confirmed by the consensus of the network, not a single entity.
Architecture of a Web3 application
An Overview
Let’s explore the whole process of what makes Web3 different.
1. Blockchain
These are state machines that are globally accessible. These are maintained by a peer-to-peer network of nodes. So, in short, it refers to a state machine that anyone in the world can write and access. However, users can add new data to the blockchain, but they can never change existing data.
2. Smart Contracts
Smart contracts are business logic stored on a blockchain (state machines). Without any intermediary's involvement, it will execute when predetermined conditions are agreed upon.
Currently, the most well known Smart contracts are transaction protocols that run on the Ethereum blockchain. These are usually written in high level-languages like Solidity to define the logic behind the state changes. Smart contracts always run on private peers, owned by specific network members, against the verified data hosted by those peers.
Here’s a sample Solidity smart contact
3. Ethereum Virtual Machine (EVM)
These machines’ purpose is to carry out the logic defined in the smart contracts. They handle the state changes that occur on the state machine.
4. Front End process
It allows the user to connect and send contract function call requests through the applications frontend. Thus, it communicates with the application logic defined in smart contracts.
Comparing Web2 and Web3 Frontend
In web2, the frontend of an application gives the user the ability to query and post to the database, similarly, this does not change in web3. The main tasks of a Web3 frontend developer are constant with Web2, where they implement everything seen to the users. Web3 frontend developers generally require the same skill set, including proficiency in HTML, CSS, JavaScript, React.js etc.
However, the main difference is Web3.js, which is a collection of JavaScript libraries that lets you interact with an Ethereum node. In general, Web3.js provides an API so we can communicate with the blockchain node with either a HTTP or IPC connection
How to interact with the Ethereum Virtual Machine (EVM) & Smart contracts.
A Web3 application (dApp) must host a Ethereum node or use a 3rd party provider such as Infura, Alchemy and Quicknode. This necessary because any node can query data or execute a transaction on the Ethereum Virtual Machine. However, keep in mind that running a full Ethereum node can become costly in addition to maintenance issues. Therefore, using a third party service can be more convenient.
Next, we will more on to "web3 providers" which allow you to interact with the Ethereum Virtual Machine and smart contracts.
Ethereum providers implement JSON-RPC specifications to communicate with the blockchain networks. Remote Procedure Call (RPC) is a request-response protocol that defines the rules to allow the client to send the message to the remote machine to execute the function and retrieve the response. In such a communication, the program runs as it is located on the client machine, meaning the client doesn't know about the remote machine. All the communication is going through HTTP or Web Sockets.
Unlike twitter, where the access to the app is granted through a centralized database login, in a dApp, access is authenticated by means of a web3 provider. Providers, like Metamask, a cryptocurrency wallet, act like a transaction signer and provider. It stores your encrypted private keys using your browser's data storage. All the write requests transactions are signed with the client's private key. Each transaction will cost the user a fee in Ethereum which would go to other nodes (miners) which verify the transactions.
Therefore, when a user wants to create a new transaction, the dApp , via web3 provider, will request the user to sign the transaction using their private key. Once the user has signed, the dApp will interact with the smart contract, on behalf of the user, and broadcast the output onto the Ethereum network.
Storage on the Ethereum Blockchain
Logically, all of the smart contracts and data should exists on the Ethereum blockchain. However, when a data is committed to the Ethereum blockchain, it costs money (miners fee). Thus, storing everything on the blockchain can become extremely expensive.
Since it's not ideal for the user to pay large transactional fees while using your dApp, a decentralized off-chain storage solution is utilized. Some options are:
IPFS- An Interplanetary File System (IPFS) is a peer-to-peer file-sharing system. IPFS is used for storing and accessing files, websites, applications, and data. IPFS uses content-addressing to identify each file in a global namespace connecting all computing devices.
IPFS also has a layer known as Filecoin. This layer is used to store and find data. Providers like Infura or Pintara are useful to pin files to IPFS and store it on the blockchain.
Swarm is also a decentralized storage, service, and communication platform designed to deliver permissionless, censorship resistant infrastructure for the deployment of dApp code.
Another option is cloud servers, Azure or AWS. However, that would resemble Twitter's web2 architecture, where we have the centralization of data. In addition, if Azure or AWS were to go down, censor your app, you would no longer have access to the data.
For the above reasons, a genuine dApp would store all of its off- chain data on a decentralized storage option.
Wrapping up
Blockchain technology will continue to play an essential role in internet infrastructure as Web3 evolves. Web3 is a shift in the paradigm, where the users gain control of their privacy and data. It will also be interesting to see how Big tech companies will react to this future trend.
Sukhpreet Singh Blockchain Enterprise Architect/Consultant
Gyan Solutions