A key-value database (also known as a key-value store and key-value store database) is a type of NoSQL database that uses a simple key/value method to store data.

The key-value part refers to the fact that the database stores data as a collection of key/value pairs. This is a simple method of storing data, and it is known to scale well.

The key-value pair is a well established concept in many programming languages. Programming languages typically refer to a key-value as an associative array or data structure. A key-value is also commonly referred to as a dictionary or hash.

Examples of Key-Value Stores

Below are examples of key-value stores.

These are simple examples, but the aim is to provide an idea of the how a key-value database works.

Phone Directory

Key Value
Bob (123) 456-7890
Jane (234) 567-8901
Tara (345) 678-9012
Tiara (456) 789-0123

Artist Info

Key Value
artist:1:name AC/DC
artist:1:genre Hard Rock
artist:2:name Slim Dusty
artist:2:genre Country

Stock Trading

This example uses a list as the value.

The list contains the stock ticker, whether its a “buy” or “sell” order, the number of shares, and the price.

Key Value
123456789 APPL, Buy, 100, 84.47
234567890 CERN, Sell, 50, 52.78
345678901 JAZZ, Buy, 235, 145.06
456789012 AVGO, Buy, 300, 124.50

IP Forwarding Table

This is an example of an IP forwarding table. It forwards an IP address to a MAC address of a physical computer.

Key Value
202.45.12.34 01:23:36:0f:a2:33
202.45.123.4 00:25:33:da:4c:01
245.12.33.45 02:03:33:10:e2:b1
101.234.55.1 b8:67:a3:11:23:b1

What Type of Data can be stored in a Key-Value Database?

The Key

The key in a key-value pair must (or at least, should) be unique. This is the unique identifier that allows you to access the value associated with that key.

In theory, the key could be anything. But this may depend on the DBMS. One DBMS may impose limitations while another may impose none.

In Redis for example, the maximum allowed key size is 512 MB. You can use any binary sequence as a key, from a short string of text, to the contents of an image file. Even the empty string is a valid key.

However, for performance reasons, you should avoid having a key that’s too long. But too short can cause readability issues too. In any case, the key should follow an agreed convention in order to keep things consistent.

The Value

The value in a key-value store can be anything, such as text (long or short), a number, markup code such as HTML, programming code such as PHP, an image, etc.

The value could also be a list, or even another key-value pair encapsulated in an object.

Some key-store DBMSs allow you to specify a data type for the value. For example, you could specify that the value should be an integer. Other DBMSs don’t provide this functionality and therefore, the value could be of any type.

As an example, the Redis DBMS allows you to specify the following data types:

  • Binary-safe strings.
  • Lists: collections of string elements sorted according to the order of insertion.
  • Sets: collections of unique, unsorted string elements.
  • Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. Allows you to do things like, select the top 10, or the bottom 10, etc.
  • Hashes, which are maps composed of fields associated with values. Both the field and the value are strings.
  • Bit arrays (or simply bitmaps).
  • HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set.

Check out Redis’ intro to Redis data types for a more detailed explanation of these.

What can a Key-Value Database be used for?

Key-value databases can be applied to many scenarios. For example, key-value stores can be useful for storing things such as the following.

General Web/Computers

  • User profiles
  • Session information
  • Article/blog comments
  • Emails
  • Status messages

Ecommerce

  • Shopping cart contents
  • Product categories
  • Product details
  • Product reviews

Networking/Data Maintenance

  • Telecom directories
  • Internet Protocol (IP) forwarding tables
  • Data deduplication

Key-value databases can even store whole webpages, by using the URL as the key and the web page as the value.

But depending on what needs to be done with the data, other DB models could be more suitable for storing web pages and articles.

Examples of Key-Value Database Management Systems

Here are some of the DBMSs that use the key-value approach.