We discussed the world state database in previous sections. Here, let's examine it in more detail. It is a database that offers queries and appends itself to the states of the ledger. We know that the world state is modeled as a key-value store that is versioned. Here, the values of the keys are appended or retrieved by applications via simple put and get operations. The world state is maintained in a database, which could be either LevelDB or CouchDB. The following is a quick comparison and lists recommendations for this database:
- Similarities:
- Both can store binary data.
- Both support chaincode operations, such as get and set. This is where get and set are essentially getting and setting the asset (the key's value).
- Both support range queries, where keys are queried by range.
- Both also support composite keys; for example, a combination of asset ID and owner ID can be used to get all the assets a particular owner owns.
- Differences: Business applications today model assets as JSON. Hence, CouchDB allows applications to perform rich queries to chaincode where assets are JSON modeled, using CouchDB's JSON query language:
- LevelDB is the default database, and is collocated with a node, mostly embedded in the same operating system process.
- CouchDB runs on a separate operating system process. However, it maintains a one-to-one relationship with network nodes and CouchDB instances.
- Recommendations:
- LevelDB is recommended when the states are simple key-value pairs.
- When the state structure is JSON, then CouchDB is recommended.
- It is possible to start with LevelDB and later move to CouchDB. However, I personally recommend modeling assets' data as JSON and therefore use CouchDB.
- Indexing is recommended for CouchDB, where indexes can be packaged alongside chaincode in a different directory. The moment chaincode is installed and initiated on peers, indexes also get deployed to channels and the chaincode's database for states also known as CouchDB.
- Key Feature-Pluggable: HLF has various components that are pluggable, and a database is one of them. Businesses can have a relational database, a graph store, or a temporal database as the database of choice for the world state.