One of the major challenge with Ethereum Blockchain is the scale-ability and throughput. Hypeledger Fabric try to address that with state database so this post is related to that. You will get to understand about state database, it's usage and types of state db in hyperledger fabric .
Very first question comes, why we need to have state database and what is this ? so let's understand that first.
What is state database and why it is needed ?
Let's understand with below example. Let's assume we storing the car ownership on blockchain network.
- Mark purchased new car from company with car Id 1234. We store that ownership details in blockchain block with key as 1234 and value as Mark. Here key represent the Car Id and value represent the car owner.
- After few months, Mark sell that car to John and we store that change in ownership in blockchain block with key as 1234 and value as John.
- Again after few months John sell that car to Bob and we store that change in ownership in blockchain block with key as 1234 and value as Bob.
In this process, key remain same and only the ownership of the car getting changed and getting stored in blockchain.
Whenever anyone wants to check who is the current owner of the car with car id 1234, that query will go through the blockchain (all the blocks) and then get the latest value and this query surely going to take some time.
What if, we maintain a separate storage (database) and store only the latest record (key value pair) in that storage. In this example, this storage only contains latest value which is key as 1234 and value as Bob so that if anyone wants to check current owner of the car, they need not to go to blockchain rather read that from that storage.
This storage is known as state database. Same is shown in below diagram.
Benefits of State Database ?
As explained in above example, latest key value pair get stored in state database along with blockchain so all the queries for latest records will be address by state database instead of going to blockchain. This is more efficient process and improve the performance.
Function to interact with State Database
The ChaincodeStub provides functions that allow you to interact with the underlying ledger to query, update, and delete assets as shown below.
- func (stub *ChaincodeStub) GetState(key string) (byte, error)
This function or API helps to get the state from ledger means returns the value of the specified key from the ledger.
- func (stub *ChaincodeStub) PutState(key string, value byte) error
As the name suggests, this API or function puts the specified key and value into the transaction's Write set as a data-write proposal.
- func (stub *ChaincodeStub) DelState(key string) error
This function helps to delete the specified key in the Write set of the transaction proposal.
Types of State Database
There are two types of state database in hyperledger fabric as shown below.
- Level DB
- Couch DB
Level DB: This is Key-Value storage library where we store key and value pairs. This helps to create indexes on keys only and does not have option to have indexes on values.
Couch DB: This is documented oriented storage that support rich queries. Another benefit of couch db is that, it support JSON. Couch DB does support index on value to perform rich queries.
Chaincode interface provides set of functions that you can use for Level DB and separate set of functions for Couch DB.