Key-Value and Graph Models

Two models at opposite ends: one that does almost nothing very fast, and one built entirely around following relationships.

Key-Value and Graph Models

Both models, and the SQL equivalents

query.sql SQLite
Result

Worth knowing

A key-value store supports get, put and delete on an opaque value. That is the entire interface.
The narrowness is the feature: no query planner, no joins, and a lookup that is a hash away.
A graph model makes relationships first-class. Traversing them costs the same whether the graph is small or huge.
In SQL, a graph traversal is a recursive CTE — possible, and verbose enough to show why a dedicated model exists.

Overview

A key-value store maps a unique key to a value. The central operation is usually retrieving, creating, or updating that value by key. This simple model can fit sessions, caches, counters, and state whose access patterns are known in advance. The value may be opaque to the store, limiting queries across its internal fields.

In this guide
  1. Key-value: retrieve by a known key
  2. Graph: follow relationships
  3. Choose from the access pattern
  4. A concrete model choice
  5. Account for durability and queries
  6. Further reading

Key-value: retrieve by a known key

Redis is a common example and supports several richer data types beyond a plain string. Its data structures and persistence options mean it is not merely a simple dictionary; choose the operation and durability behavior deliberately.

Graph: follow relationships

A graph database represents entities as nodes and their relationships as edges, often with properties on both. It is useful when a question requires traversing several connections, such as paths between people, dependencies, or recommendations. The relationship itself is a first-class part of the model.

A relational database can represent the same graph with tables and joins. A graph engine may make repeated, variable-depth traversal easier, but does not make every query faster. If most requests are simple lookups by ID, a graph model may add complexity without helping.

Choose from the access pattern

Ask what the application needs to query and update, how relationships change, and what consistency and transaction guarantees are required. Do not choose a database because a category sounds newer. You may also combine stores, but then decide which system owns each fact and how updates remain consistent.

For comparisons with document databases and relational tables, see relational databases and non-relational databases.

A concrete model choice

A session store keyed by a random session token may need only set, get, expiry, and delete operations; a key-value model maps directly to that workload. A dependency explorer may need to walk from one service through several dependency levels; a graph model makes those edges central. If requirements later include rich filtering over arbitrary fields, the original simple model may need indexes, a different store, or a second representation. Capture expected queries and update rules before selecting the model.

Account for durability and queries

A cache that can be rebuilt has different persistence needs from a store holding the only copy of a customer's state. Check whether the selected key-value product persists data, how replication and failover work, and what commands are atomic. For graph queries, define maximum traversal depth, path semantics, and cycle behavior. These concerns are part of choosing a data model, not details to leave until deployment. Prototype the hardest expected query and update before committing to the model.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why is a key-value store fast?

  2. How does a graph edge differ from a SQL foreign key?

  3. What is a graph database usually worse at than a relational one?

Cheat sheet

Key-Value and Graph Models

Compare key-value and graph models against relational tables, see the queries each answers in one hop, and learn when leaving SQL pays off.

DATABASE · vizlearn.in/database/key_value_and_graph_models.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.