What are the main difference between chain and state database in Hyperledger fabric. I'm confusing whether they both are same.
2 Answers
There are two place which "store" data in Hyperledger Fabric:
- the ledger
- the state database
The ledger is the actual "blockchain". It is a file-based ledger which stores serialized blocks. Each block has one or more transactions. Each transaction contains a read-write set which modifies one or more key/value pairs. The ledger is the definitive source of data and is immutable.
The state database holds the last known committed value for any given key. It is populated when each peers validates and commits a transaction. The state database can always be rebuilt from re-processing the ledger. There are currently two options for the state database: an embedded LevelDB or an external CouchDB.
As an aside, if you are familiar with Hyperledger Fabric channels, there is a separate ledger for each channel as well.

- 11,418
- 2
- 18
- 41
-
1You mentioned "state database holds the last known committed value for any given key", so when we query from where it retrieves data 1) from chain or 2) from state db? If it is from state db how can it retrieve a specific key because you mentioned "state database holds the last known committed value for any given key" – Moulali Nov 28 '17 at 05:20
-
Queries or GetState in chaincode return data from the state db. They will only return the last value for a key. If you want to get the entire history for a key, you need to enable the historical database on in the configuration of your peer – Gari Singh Nov 28 '17 at 10:21
-
@GariSingh thanks for the explain, could you give some documentation links about the about "historical database on in the configuration", from the hyperledger FAQ, the only mentioned is using the `GetHistoryForKey()` for query all the history of values for a key, thanks. – Liping Huang Aug 08 '18 at 08:01
The chain is a transaction log, structured as hash-linked blocks, where each block contains a sequence of N transactions. The block header includes a hash of the block’s transactions, as well as a hash of the prior block’s header. In this way, all transactions on the ledger are sequenced and cryptographically linked together.
The state database is simply an indexed view into the chain’s transaction log, it can therefore be regenerated from the chain at any time.
Source: http://hyperledger-fabric.readthedocs.io/en/release/ledger.html

- 320
- 1
- 7