Key-Value Databases

How does a key-value database work?

A key-value database, aka key-value store, associates a value (which can be anything from a number or simple string, to a complex object) with a key, which is used to keep track of the object. In its simplest form, a key-value store is like a dictionary/array/map object as it exists in most programming paradigms, but which is stored in a persistent way and managed by a Database Management System (DBMS).

Key-value databases use compact, efficient index structures to be able to quickly and reliably locate a value by its key, making them ideal for systems that need to be able to find and retrieve data in constant time. Redis, for instance, is a key-value database that is optimized for tracking relatively simple data structures (primitive types, lists, heaps, and maps) in a persistent database. By only supporting a limited number of value types, Redis is able to expose an extremely simple interface to querying and manipulating them, and when configured optimally is capable of extremely high throughput.

What are the features of a key-value database?

A key-value database is defined by the fact that it allows programs or users of programs to retrieve data by keys, which are essentially names, or identifiers, that point to some stored value. Because key-value databases are defined so simply, but can be extended and optimized in numerous ways, there is no global list of features, but there are a few common ones:

  • Retrieving a value (if there is one) stored and associated with a given key
  • Deleting the value (if there is one) stored and associated with a given key
  • Setting, updating, and replacing the value (if there is one) associated with a given key

Key-value databases can also have numerous other features, but at the very least support a system of operating on data in the above ways.

How do I create a key-value database?

Choosing a key-value database technology to use can be a daunting task. If you’re mostly looking to store small bits of data for short periods of time, and prioritize speed over depth of features, a simple key-value store like Redis could be a great tool to try - or an equivalent managed service through the cloud provider of your choice.

If your goals are more ambitious, you might choose a document database like MongoDB, which stores data in named collections, and in which most things can be used as keys by which to look them up. MongoDB also supports advanced indexing and other powerful ways to access and update documents, as well as structures ranging from very simple dictionaries to complex nested objects.

Is MongoDB a key-value database?

MongoDB is a document database, which means that it stores data in the form of “objects” which have properties which can be changed, added to, deleted, and queried against.

While in an academic sense MongoDB stores values (documents) for keys (identifiers), it would be a bit of a simplification to call MongoDB simply a key-value database (though it can certainly do the job). MongoDB document values are rich objects which can contain entire hierarchies and sub-values, and sophisticated indexing allow documents to be retrieved by any number of different keys.

Further, MongoDB’s document values allow nested key-value structures, allowing not only for accessing data by key in a global sense, but accessing and manipulating data associated with keys within documents, and even creating indexes that allow fast retrieval by these secondary kinds of keys.

Ready to get started?

Try MongoDB Atlas Database Service for Free