# Why Build a Custom DBMS?

> Analyzing the widening gap between modern NVMe SSD throughput and legacy database software performance, and why we decided to build a new storage engine from scratch.

[Blog](https://www.unum.cloud/blog.md) [IO](https://www.unum.cloud/blog?tag=IO) [AI](https://www.unum.cloud/blog?tag=AI)

Analyzing the widening gap between modern NVMe SSD throughput and legacy database software performance, and why we decided to build a new storage engine from scratch.

- By

  Ash Vardanian

- Published

  Jan 2, 2021

Why would an AI-Research team spend years designing new storage infrastructure? Well, let’s look at hardware.

---

## The Hardware We Pay For

A high-end server like [DGX A100](https://www.nvidia.com/en-us/data-center/dgx-a100/) today costs around $300,000 and comes with following components:

- 2x CPUs (with shared RAM),
- 8x GPUs (each with private VRAM) and
- 2x SSDs.

Almost the entire cost of hardware is the cost of logic and volatile memory - RAM. RAM is fast, but also small. The chances are, your computer wastes most of the time simply fetching and sending data instead of actually “computing”.

| Channel | Bandwidth | Memory Volume / Socket |
| - | - | - |
| CPU ⇌ [DDR4 RAM](https://en.wikipedia.org/wiki/DDR4_SDRAM) | \~100 GB/s | 1,000 GB |
| GPU ⇌ [HBM2 VRAM](https://en.wikipedia.org/wiki/High_Bandwidth_Memory) | \~1,000 GB/s | 300 GB |
| GPU ⇌ GPU [via NVLink](https://en.wikipedia.org/wiki/NVLink) | \~300 GB/s | - |
| CPU ⇌ GPU [via PCI-E Gen3 x16](https://en.wikipedia.org/wiki/PCI_Express) | \~15 GB/s | - |
| CPU ⇌ SSD [via NVMe PCI-E Gen3 x4](https://en.wikipedia.org/wiki/NVM_Express) | << 4 GB/s | 1,000,000 GB |

Read the table in both directions. From GPU memory down to the SSD, the bandwidth falls by more than two orders of magnitude. In the opposite direction, the capacity falls by three: a socket can address around 1,000 GB of RAM, but the SSDs behind it can hold 1’000x more. So the memory you can afford to fill with data is exactly the memory you can’t afford to wait on.

The PCI-E numbers are not arbitrary either. A Gen3 lane moves 8 GT/s with 128b/130b encoding, which is just under 1 GB/s of payload per lane ([PCI-SIG](https://pcisig.com/specifications)). Sixteen lanes to a GPU give you the \~15 GB/s above, and four lanes to an NVMe drive give you the \~4 GB/s ceiling. Gen4 doubles the transfer rate, and it is already here: AMD EPYC “Rome” CPUs have been shipping with it since 2019, and the DGX A100 itself is built around them. Twice the link is nice, but as we are about to see, the link is not what’s holding us back.

## Where the Bandwidth Goes

For us, Big Data begins where RAM capacities end. We often store 10-1,000 TB of data on a single machine and need to navigate and analyze it in real-time. The (CPU ⇌ SSD) link is the slowest in the chain. Its theoretical throughput (for Gen3 PCI-E) is 4 GB/s, but the real world reads on small files often degrade to 200 MB/s. The reality is even more depressing, if you store your data in structured DBMS. Those mix frequent random read & write operations which slows down SSDs to 10 MB/s.

Again, on the GPU side you can fetch 1 TB/s, but if your SSD provides you only 10 MB/s, most of your chips will be wasting compute cycles…

Put those numbers side by side:

- From 4 GB/s of sequential reads to 200 MB/s on small files is a **20x** drop.
- From 4 GB/s to 10 MB/s under a mixed DBMS workload is a **400x** drop.
- From 1 TB/s of GPU memory to 10 MB/s of storage is a **100’000x** gap.

So where does it all go? Part of the answer is physics. NAND flash is read and written in pages of a few kilobytes, but it can only be erased in much larger blocks, and a page can’t be rewritten before its whole block is erased. So the drive never overwrites data in place. Its controller runs a Flash Translation Layer that writes every update to a fresh page, remaps the address, and later garbage-collects the stale copies, moving still-valid pages around to free entire blocks. When reads and writes are mixed, your reads wait behind that housekeeping, and every byte the controller moves on its own is a byte you didn’t ask for, wearing out a device with a limited number of program-erase cycles.

The bigger part of the answer is software. An NVMe drive is a massively parallel device with deep hardware queues. It only reaches its rated numbers if you keep many requests in flight at once. A program that issues one blocking `read` system call at a time, waits for it, copies the result out of the OS page cache and only then decides what to fetch next, keeps the queue nearly empty. The drive idles, the CPU spends its time in the kernel, and the GPUs downstream wait for both. The tools to fix this exist - Linux gained [`io_uring`](https://kernel.dk/io_uring.pdf) in 2019, and Intel’s [SPDK](https://spdk.io/) lets you drive NVMe from user-space - but most storage software was designed long before them.

## Why Not Take Something Off the Shelf?

The obvious question is, why not just pick one of the many existing databases and tune it? We know them well - we benchmark against them all the time. The problem is that most of them were designed for a different machine.

B-Trees, the backbone of most databases, date back to [1970](https://en.wikipedia.org/wiki/B-tree). [Postgres](https://www.postgresql.org/) grew out of the POSTGRES project that started at Berkeley in 1986, [MySQL](https://www.mysql.com/) appeared in 1995, [SQLite](https://www.sqlite.org/) in 2000. Their storage layers were built in the era of spinning disks, where a random read costs a few milliseconds and a drive serves on the order of a hundred of them per second. In that world, the CPU has plenty of time to spare while waiting for the disk, so nobody minds a system call per page, a lock per operation or a copy between the database cache and the OS cache. A modern NVMe drive serves hundreds of thousands of random reads per second, and suddenly those “free” overheads become the bottleneck.

The newer generation isn’t much better in this respect. [Neo4J](https://neo4j.com/) and [ElasticSearch](https://www.elastic.co/) run on the Java Virtual Machine, so a large part of their memory is managed by a Garbage Collector, and the bigger the heap, the longer the pauses. Many DBMS talk to the world through text protocols, parse SQL or JSON on every request, and serialize every result before it leaves. Each layer makes sense on its own, but they add up. Tuning configuration files doesn’t remove any of them.

And then there is the question of scope. Our workloads include web-scale text, large knowledge graphs and the numerical datasets we train our models on. Off-the-shelf, that means a relational DBMS for tables, a document store for semi-structured data, a graph database for relations and a search engine for text. Four systems, four copies of the data, four import pipelines, and a lot of glue code in between. Instead, we decided to design a single storage engine that could serve all of those shapes of data, and then build thin, specialized layers on top of it.

## How much faster UDisk is?

We generally benchmark UDisk against following competitors:

1. [SQLite](https://www.sqlite.org/) is the most basic SQL database.
2. [MySQL](https://www.mysql.com/) is the most widely used Open-Source DBMS in the world.
3. [Postgres](https://www.postgresql.org/) is the 2nd most popular Open-Source DBMS. Implemented in 1,300,000 lines of C code.
4. [MongoDB](https://www.mongodb.com/) is the most popular NoSQL database. Implemented in 3,900,000 lines of C++ code.
5. [Neo4J](https://neo4j.com/) is the most popular graph database. Implemented in 800,000 lines of Java code.
6. [ElasticSearch](https://www.elastic.co/) is the most popular indexing software. Implemented in 300,000 lines of Java code on top the [Lucene](https://lucene.apache.org/) Java library.

The list is not random. It covers the embedded, the relational, the document, the graph and the search worlds - every kind of system we would otherwise have to deploy side by side.

Abandoning legacy approaches allowed us to reach:

- 50x faster dataset imports.
- 5x smaller compressed representations.
- 100x faster random lookups.
- 10x faster batched insertions.
- compact implementation in 100,000 lines of C++ code.

Each of those numbers maps to a very practical pain. **Imports** are the first thing every data-science project does, and if loading a dataset takes a day, people stop re-loading it, and the data gets stale. **Compression** matters twice: it lets more data fit on the same SSDs, and every byte you don’t write is a byte that doesn’t wear out the drive or occupy the bus. **Random lookups** are how you sample training batches and how you walk a graph - one neighbor list at a time, each in a different part of the disk. **Batched insertions** are how crawlers and pipelines actually produce data: not one row at a time, but in chunks of thousands. Finally, the **code size**. A smaller codebase is one a small team can understand end to end, from the query all the way down to the NVMe queue, and that’s the only way to optimize across layers instead of within them.

## What It Takes

None of this came for free. Getting there meant rethinking the stack from the bottom up:

- Talking to the SSD asynchronously, with many requests in flight, instead of one blocking call at a time.
- Managing our own caches instead of duplicating data between the DBMS and the OS.
- Laying out data on disk so that the common operations turn into large sequential transfers, instead of scattered small ones.
- Batching everything: lookups, insertions and scans, so the hardware always has enough work queued.
- Writing it all in C++, where we control every allocation and every copy.

Too good to be true? Fair enough - we wouldn’t trust such claims without numbers either. Over the next few days, we will go through [text indexing](https://www.unum.cloud/blog/texts.md) against ElasticSearch and MongoDB, and then through the [bottlenecks](https://www.unum.cloud/blog/bottlenecks.md) we had to remove at every layer, from the SSD up to the network, to get there. More benchmarks will follow as UDisk matures.
