Key-Value Store
- In Turkish
- anahtar-değer veritabanı
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
# 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:abc123Readers 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
- NoSQLDatabases, p. 29NoSQL is a family of databases that store data in models other than relational tables, such as documents, key-value pairs, wide columns, or graphs.
- CacheBackend & APIs, p. 8A cache is a fast, temporary storage layer that keeps copies of frequently used data so later requests can be served quickly without repeating slow work.
- Hash TableData Structures, p. 18A hash table is a data structure that stores key-value pairs and uses a hash function to find the value for any key in constant time on average.
- SessionBackend & APIs, p. 43A session is a way for a server to remember a user across many requests, usually by keeping their data on the server and giving the browser a session ID.
- DatabaseDatabases, p. 6A database is an organized collection of data stored on a computer, managed by software that lets applications save, search, and update it efficiently.
- Rate LimitingBackend & APIs, p. 37Rate limiting is a technique that caps how many requests a client can make to a server or API within a time window, protecting it from abuse and overload.
- RedisDatabases, p. 37Redis is an in-memory key-value store that reads and writes in well under a millisecond, which makes it a popular cache, session store and message broker.
Spotted a mistake or something missing on this page?Suggest an edit