Skip to content
tezvyn:
Menu

Key-Value Store: The Simplest Database Model

Source: Wikipedia: Key-value databaseEasyHow cards are made

Key-Value Store: The Simplest Database Model

A key-value store is a giant dictionary. You give it a unique key, like "user:123", and it returns the associated data. It's the foundation for caching and session management. The footgun is trying to query by value—it's built for key lookups only.

Why it exists

Relational databases are powerful but can be overkill. Sometimes you just need to store a piece of data and get it back quickly using a known identifier. Key-value stores were created for this high-speed, simple lookup pattern, stripping away the complexity of schemas, relationships, and complex query languages.

The mental model

Think of a physical coat check. You hand over your coat (the value) and get a ticket with a number (the key). To get your coat back, you must present that exact ticket. You can't ask the attendant to find your coat by its color or brand (querying by value), only by its ticket number. The system is incredibly fast because it only does one thing: map ticket numbers to coats.

How it works

At its core, a key-value store is a massive hash map or associative array. It stores data as a collection of key-value pairs. The key is a unique string that acts as an identifier. The value can be anything: a simple string, a complex JSON object, or even a binary file. When you request data, you provide the key. The database uses this key to directly look up the location of the value and returns it, bypassing the complex query parsing of a relational database.

When to use it

Use a key-value store for scenarios demanding high-speed reads and writes with simple access patterns. Three common places are: first, caching, where you store the result of expensive operations with a key for quick retrieval; second, session management, where a user's session ID is the key to their session data; and third, real-time leaderboards, where a player's ID is the key and their score is the value.

When not to use it

Avoid key-value stores when you need to query data based on its attributes, not just its key. If your application requires complex queries, relationships between data entities (like joining users and their orders), or enforced data consistency through transactions, a relational (SQL) or document database is a better fit. You cannot ask a key-value store to "find all users who live in California."

One canonical example

Redis is a classic key-value store. A web application might store a user's profile in a primary SQL database. When the user logs in, the application fetches the profile, serializes it as a JSON string, and stores it in Redis with the key user_profile:12345. Subsequent requests for that profile can read directly from Redis, which is much faster than querying the SQL database again.

Interview question

Which scenario highlights a fundamental limitation of key-value stores?

  • a.Caching the results of an expensive database query.
  • b.Quickly retrieving a user's session data using their session ID.
  • c.Finding all products in a catalog that are currently on sale.Correct
  • d.Storing a user's entire profile as a JSON object.
Why?

Key-value stores are designed for direct key lookups and cannot efficiently query data based on its attributes or values, as explicitly stated in the card. Options A, B, and D describe common and appropriate use cases for key-value stores, leveraging their strength for fast, key-based access.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on database — each one lists the topics its interview covers.

See open roles