What is a Key-Value Store?

Key-value stores are one of the most popular data storage alternatives to relational databases. But what is a key-value store exactly, and how do key-value stores work?

What is a key-value store?

A key-value store, also known as a key-value database, is a method of storing data that consists of multiple key-value pairs. Each key is a unique identifier that is linked to a value, which may or may not be unique. 

You can think of a key-value store as analogous to a set of lockers. Each locker can only be unlocked by a single key, and each key only unlocks one locker. Having the right key allows you to access the contents of the locker (the "value" in this analogy). However, there are no restrictions on the locker contents - different lockers may contain the same or different items.

Key-value stores are an alternative database paradigm to relational databases, in which data is organized into columns and rows. Including key-value stores, there are four main types of non-relational ("NoSQL") databases: document stores, column-oriented databases, and graph databases.

How do key-value stores work?

Not all key-value stores work exactly the same way. Depending on the implementation, the differences may include:

  • You may be able to use any object as a key or value, or you may be limited to certain types of objects (e.g. strings).
  • The key-value store may keep data in cache memory, in RAM, or on disk.
  • Ordered key-value stores keep data in a particular order for efficiency and ease of use. Unordered key-value stores do not place restrictions on the order in which data is stored.

The data structures that implement key-value stores include maps, dictionaries, and hash tables. These data structures are highly efficient compared with standard row-column tables, which may need to contain placeholders for empty or optional values. Key-value stores occupy exactly the space they need for each key/value pair and no more.

If a key-value store grows large enough, it may be distributed across multiple machines, improving its performance and fault tolerance.

Key-value stores: benefits and use cases

Traditional row-column relational databases are highly structured, but inflexible. This organization makes it easier to optimize their performance, especially for complex aggregations, but harder to customize them for different use cases.

Key-value stores take the opposite approach. What they sacrifice in structure, they make up for with fast performance when reading and writing. Most interactions with key-value stores involve a simple lookup, rather than the convoluted queries, joins, and other operations done on relational databases.

In general, key-value stores are highly resilient and scalable. This makes them a good fit for use cases such as storing user sessions and massive surges in web traffic (e.g. e-commerce websites during the holiday season).

Key-value stores in Redis

If you want to implement a key-value store for yourself, there's no shortage of options. According to the DB-Engines ranking, Redis is the most popular key-value database technology.

Redis is an open-source, in-memory data structure store that is frequently used to implement key-value databases. Unlike other key-value stores, Redis allows you to use arbitrary objects for keys and values, not just strings. Redis is capable of using the following data structures as keys and values:

  • Binary-safe strings
  • Lists
  • Sets
  • Sorted sets
  • Hashes
  • Bit arrays (i.e. bitmaps)
  • HyperLogLogs
  • Streams

One of the best features of Redis is its compatibility with dozens of different programming languages, including Java, C/C++, Python, Rust, and many more. For convenience and ease of use, many developers choose to install a third-party Redis Java client such as Redisson.

Redisson provides implementations of many familiar Java distributed objects and collections, smoothing the learning curve for Java developers who use Redis. The good news is that Redisson offers just as much key - value store functionality - and more - than the base version of Redis.

Similar terms