Skip to main content

Key-Value Store

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/key-value-store

In short

A key-value store is a NoSQL database that saves each piece of data under a unique key, so an application can read or write it by that key very quickly.

What is a key-value store?

A key-value store is the simplest kind of NoSQL database: every record is a pair made of a unique key and a value. The key is usually a string, such as user:42:profile, and the value can be anything from a number or a string to a JSON document or binary data. You store data with an operation like SET and read it back with GET, always by its key.

Under the hood, most key-value stores work like a giant hash table. The key is hashed to find exactly where the value lives, so reads and writes take roughly the same time no matter how much data is stored. Some stores keep everything in memory for sub-millisecond speed, often with optional saving to disk, while others are built on disk and spread keys across many servers.

Think of a coat check at a theater: you hand over your coat and get a numbered ticket, and later the ticket is all you need to get the coat back. The attendant does not care what is in the pockets. This makes key-value stores a great fit for caches, user sessions, shopping carts, feature flags, and rate-limiting counters.

The trade-off is limited querying. A key-value store is often confused with a document database, which also stores JSON but can search and index fields inside each document. In a pure key-value store, you cannot efficiently ask for all users in a certain city unless you designed your keys for that question in advance.

Key takeaways

  • Each record is a unique key paired with a value.
  • Lookups by key are very fast, usually close to constant time.
  • Common uses include caching, sessions, counters, and feature flags.
  • You generally cannot query by the contents of a value.
  • Good key naming, such as user:42:cart, is an important part of the design.

Example

Basic operations with a Redis-compatible command-line clientbash
# Store a value under a key, then read it back by the same key
redis-cli SET user:42:name "Ada"
redis-cli GET user:42:name        # "Ada"

# Store a session that expires after 30 minutes (1800 seconds)
redis-cli SET session:abc123 "user-42" EX 1800

# Increase a counter atomically
redis-cli INCR page:home:views

# Remove a key
redis-cli DEL session:abc123

Readers ask

Is a key-value store the same as a cache?

Not exactly. A cache is a use case, while a key-value store is a type of database; many caches are built on in-memory key-value stores, but key-value stores can also be durable primary databases.

When should I use a key-value store instead of a relational database?

Use one when you always look data up by a known key and need very fast reads and writes, such as for sessions or counters. If you need joins, complex filtering, or strict relationships between records, a relational database is usually a better fit.

What is the difference between a key-value store and a hash table?

A hash table is an in-memory data structure inside one program, while a key-value store is a database service that many programs can share over the network. Many key-value stores use hash tables internally and add persistence, expiration, and replication on top.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings