/images/head.jpg

Secondary Index Built on top of a Key-value Store

Key-value stores such as RocksDB have been used as the storage engine in several databases (MySQL/Mongo/Rockset/FoundationDB). A database typically requires support of secondary indexes. To support secondary index, a common approach is to store the secondary index as yet another key-value pairs. Let’s take a look at the following example.

Employee table

userid (primary key) salary age
Alice 40000 30
Bob 20000 23
Charlie 30000 30
David 20000 25

To store the user table, we can store each record using tableName,primary_key -> salary,age as key/value pairs.

Enduring Ideas/Techniques that are still in use today

There are a few ideas/techniques that were invented a long time ago but they are being used/re-implemented again and again, even in today’s sytems. Why do people look and reimplement these techniques, even after 50 years? Because, even after 50 years, we face largely the same problem when we design new systems.

Here are a few examples.

Ideas First-time Introduced What problem is solved?
Trasactions Make it easy to develop on top of a data store. Modifications are atomic and failures are handled automatically by the system.
Log-structure storage/FS Turn random writes into sequential ones (good for hard disk drives, original motivation for developing LFS). No in-place overwrites (required for solid-state drives.).
Paxos Reach concensus across a number of programs/nodes

Delete Request in LevelDB

When I searched for how leveldb/rocksdb and a few other KV stores handle delete requests, the document will usually say ``we insert a special a tombstone value or a tombstone entry.’’. However, for KV stores which allow storing arbitrary keys and values, I am not sure which value we should use as the special tombstone value, as whatever value we pick as a tombstone value, it could be a valid value stored by the user. I looked up leveldb source code and I think I have figured out how leveldb handles this case.

Postgres Handling Transaction ID Wrap Around

NOTE: OpenGauss may fix this problem.

Three special values are reserved as special transaction IDs, including invalid, bootstrap and frozen transactions. The rest values are used in a circular fashion.

Once a row is older enough (controlled by vacuum_freeze_min_age, an integer value measuring the number of transactions), the vaccum process can mark its TID as FrozenTransactionId. FrozenTransactionId is smaller than any normal transaction ID and thus these frozen rows are visible for all current and future transactions.

Transaction

The four properties that a transaction in the database world has are ACID: A stands for atomicity, C for consistency, I for isolation and D for durability. The purpose of transactions is to maintain data in the face of concurrent access and system failures.

Property Definition Technique to achieve that property
Atomicity A transaction either succeeds or fails as a single operation. Undo log
Consistency A database should start in a consistent state and end in another consistent state, after applying a transaction or transactions. Relies on the other three properties.
Isolation Each transaction should be executed as if it was the only transaction that is running. Transactions are isolated and are not aware of each other. Allowing concurrent transactions. 2-phase locking or MVCC
Durability Writes made to the database will survive system failures. Redo log

Database Management Systems

The following content was selected from chapter 17, Transactions.

Hit the Road iOS App

Hit the Road is an iOS app for tracking ourdoor exercises. It can track walking, running, and biking. It is supposed to run on iPhones, but not iPads or iWatchs.

User Privacy

The app only stores the duration and mileage for each activity locally in the installed device. The app does not store the GPS locations. It only uses GPS locations during an activity to get the mileage. The app does not send any data over the network.