DEV Community

Cover image for What is LevelDB and how does it work?
Ali Sherief
Ali Sherief

Posted on • Updated on

What is LevelDB and how does it work?


Databases form an important part of an application stack, after the server and client side parts of it have been written. There are a lot of databases in use today, but picking which one to use largely depends on what kind of work your app does. If it is a heavyweight app then it will benefit from a stand-alone database software configured on its own server, such as MariaDB, Oracle, MS SQL, Postgres and others. Small apps, and particularly those that desire to be portable without needing to host a server-side API to store its state in, will usually be bundled with a file-based database such as SQLite. The database I will talk about in this post, LevelDB, falls in the second category.

LevelDB is described on Wikipedia as "an open-source on-disk key-value store", this link redirecting you to the subject NoSQL. And databases that don't store data in the common relational format are known as NoSQL databases, which means any database storage, including file-based stores, that have data stored in a different format, like key-value or even as a structured markup document, are also NoSQL databases.

Since LevelDB exposes keys and values for users to store and retrieve data, it is an example of a NoSQL database.

Origins of LevelDB

LevelDB was created by Jeff Dean and Sanjay Ghemawat, who both work at Google. It is based on the implementation of Bigtable, another similar Google tool that has dependencies on proprietary internal Google libraries, so they wanted to build something with similar concepts of Bigtable that can be open-sourced.

Features

  • LevelDB has three basic operations: Get, Put, and Delete. Get retrieves a value given a key, Put writes a value into a key, creating the key if it doesn't exist, and Delete deltetes the key and its value. There are open (takes a filename argument) and close functions for creating/loading and unloading a database, and functions that return iterators over all the keys and values.

  • The keys and values can be any byte array and not just strings. This is useful if you have data that you want to store that you don't want to encode into a string.

  • LevelDB supports atomic operations. You can run many operations at once in a single uninterruptible call.

All of these make for a dead-simple database which at the same time having an extremely efficient, optimized implementation.

Different LevelDB libraries

Each language has its own implementation of the LevelDB library, the main library https://github.com/google/leveldb is used for C++, levelup is the version for NodeJS, which is itself a convenience wrapper for the low-level leveldown package[1], but instead of using those two, you install the level npm package and reqquire('level'). Memdown is the same as leveldown but stores the database in memory, so it's discarded if the database is closed or the app exits, the database is deleted. And finally, level-js when used together with Browserify lets you use LevelDB in browsers.


[1] Actually, levelup is a wrapper for several different leveldown implementations, all of them NodeJS packages but not all of them actually making use of the C++ LevelDB. For example, there are leveldown lookalikes that use MongoDB or DynamoDB as the backend, two NoSQL databases completely unrelated to LevelDB. The full list of leveldown implementations is at https://github.com/Level/awesome/#stores.

Top comments (0)