العربية

Choosing a Database: Relational, Document, Key-Value, Graph and More

Choosing a Database: Relational, Document, Key-Value, Graph and More

Last updated: December 3, 2026

Most database advice is a list of product names. This one is about shapes of data instead, because that is what you are really choosing between. Once you know whether your data is tables, documents, keys, a graph or a stream of measurements, the product shortlist gets short.

If you only want a default: use a relational database, and for most new projects that means PostgreSQL, or SQLite for something small or local. The rest of this guide explains when to leave that default, and when not to.

The short answer

⚡ The short answer: start relational. Move to a document, key-value, graph, time-series or warehouse database only when you can name the specific job the relational database is bad at. Most "we need NoSQL" decisions fail that test.

What a database is, and what the engine does for you

A database is organized data stored so that programs can read and change it quickly and safely. The software that does the work is a database management system, or DBMS. It handles storage on disk, lets many users read and write at once, enforces rules about what data is valid, and recovers after a crash. When people say "the database" they usually mean the data and the DBMS together.

The key word in that list is safely. A spreadsheet or a plain file can hold the same rows, but it will not stop two users from overwriting each other, and it will not refuse a bad value. That gap is why applications use a database instead of a file once more than one person or process touches the data.

The main families, side by side

FamilyExamplesGood atWatch out for
Relational (SQL)PostgreSQL, MySQL, SQLite, SQL ServerStructured data with relationships, transactions, reporting with joinsChanging the table design after the fact takes planning
DocumentMongoDBRecords that vary in shape and are read as a whole, such as content or product catalogsRelationships across documents are more work than a SQL join
Key-value / in-memoryRedisCaches, sessions, counters, queues: very fast lookups by keyNot a place to run ad hoc queries over everything; memory is limited
GraphNeo4jData where the connections are the point: recommendations, networks, fraud ringsA niche tool; overkill when relationships are simple
Time seriesInfluxDBMetrics and sensor readings with a timestamp on every rowPoor fit for general application data
Warehouse / columnarBigQuery, SnowflakeAnalytics over very large datasetsBuilt for reading in bulk, not for serving an app one row at a time

Relational: tables, keys and joins

A relational database stores data in tables of rows and columns and links tables with keys. You query it with SQL, a language standardized by ISO that every major relational system implements in large part. Here is the same small library modeled the relational way: one table of authors, one of books, linked by author_id.

import sqlite3
con = sqlite3.connect(":memory:")
con.executescript("""
CREATE TABLE authors (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE books (
    id INTEGER PRIMARY KEY,
    author_id INTEGER NOT NULL REFERENCES authors(id),
    title TEXT NOT NULL,
    year INTEGER
);
INSERT INTO authors VALUES (1, 'Ursula K. Le Guin'), (2, 'Octavia E. Butler');
INSERT INTO books VALUES (1, 1, 'The Dispossessed', 1974),
                         (2, 1, 'A Wizard of Earthsea', 1968),
                         (3, 2, 'Kindred', 1979);
""")

sql = """SELECT authors.name, books.title, books.year
FROM books JOIN authors ON authors.id = books.author_id
ORDER BY books.year"""
for row in con.execute(sql):
    print(row)
# ('Ursula K. Le Guin', 'A Wizard of Earthsea', 1968)
# ('Ursula K. Le Guin', 'The Dispossessed', 1974)
# ('Octavia E. Butler', 'Kindred', 1979)

That output is what SQLite returned when this was run. The author's name is stored once. Rename her and every book reflects it. The REFERENCES clause means the database will not let you add a book for an author that does not exist. That is the safety the engine gives you for free. If you want to learn the query language itself, start with our SQL for beginners guide.

Document: one record, one self-contained object

A document database stores each record as a self-describing object, usually JSON-like. MongoDB, for example, stores records as BSON documents, a binary form of JSON with extra data types. The same data as one document per author looks like this:

{
  "name": "Ursula K. Le Guin",
  "books": [
    {
      "title": "A Wizard of Earthsea",
      "year": 1968
    },
    {
      "title": "The Dispossessed",
      "year": 1974
    }
  ]
}

Reading an author and all their books is a single fetch with no join. That is the appeal. The cost shows up when the same data is needed from the other side. If you also want "every book from 1974", the book titles are now buried inside author documents, and a book that has two authors has to be copied or referenced by hand. Document databases fit data that is naturally read as a unit and rarely sliced across records.

One thing worth knowing before you choose a document store for flexibility: relational databases have caught up on this front. PostgreSQL has json and jsonb column types, so you can keep a flexible blob inside an otherwise structured table.

Key-value and in-memory: speed for simple lookups

Redis describes itself as an in-memory data store used as a cache, a message broker and more. You give it a key, you get a value back, in memory, which is far faster than a disk-backed query. The usual pattern is not to replace your main database but to sit beside it: cache an expensive result, store a login session, or count requests per minute. Treat it as a speed layer, not as the system of record.

Graph, time series and warehouses: the specialists

  • Graph. Neo4j is a native graph database: it stores nodes and the relationships between them directly, and you query paths with its Cypher language. Choose it when the question is "how are these things connected, and through how many hops". Questions like "friends of friends who bought this" are awkward as repeated joins and natural as a graph walk.
  • Time series. Systems like InfluxDB are built for data that is a timestamp plus a measurement, arriving constantly, such as server metrics or sensor readings. They are designed around that shape, with features for compressing and expiring old data.
  • Warehouses. BigQuery and Snowflake store data by column so that analytical queries scanning millions of rows are fast. They are for reports and analysis, not for the database behind your sign-up form.

The embedded option: SQLite

SQLite is a different kind of relational database. It has no server: the whole database is a single file read directly by your program. Its own documentation says it does not compete with client/server databases; it competes with fopen(). It is a good fit for apps, tools, tests and small sites, and a poor fit when many machines need to write to the same data at once. Our SQLite and Python expense tracker is a worked example.

How to choose

Work through these questions in order and stop at the first clear answer.

  1. Is it a small app, a script or a local tool? SQLite.
  2. Is it a typical web or business application with users, orders, accounts and reports? PostgreSQL or MySQL. Pick the one your team and host already support well.
  3. Do you need very fast reads for the same lookups, over and over? Add Redis next to your main database as a cache.
  4. Is the data mostly whole objects with changing fields, and you rarely query across them? A document database is reasonable. Check first whether a JSON column in PostgreSQL does the job.
  5. Is the central question about connections? Look at a graph database.
  6. Is it a firehose of timestamped measurements, or analytics over huge history? A time-series database or a warehouse.
⚠️ A common mistake: choosing a database for the scale you hope to have. A single well-configured relational server handles far more than most projects will ever send it. Choose for the shape of your data today, keep your queries behind a thin layer in your code, and migrate if the evidence says you must.

Licensing and hosting: two things to check early

Open source is not one thing. PostgreSQL is released under the PostgreSQL License, a permissive license similar to BSD or MIT. Other systems use copyleft or source-available licenses with different conditions for commercial use or for offering the software as a service. Read the license before you build a product on top of a database, and check whether you want to run it yourself or pay a cloud provider to run it. Running a database means backups, upgrades and security patches, and a managed service takes those off your plate for a fee.

Whichever you choose, take backups and test restoring them. A backup you have never restored is a hope, not a backup.

Where to go next

Frequently Asked Questions

❓ What is the difference between SQL and NoSQL databases?

SQL databases are relational: data lives in tables with a fixed structure and you query it with SQL. NoSQL is a loose label for everything else, including document, key-value, graph and column stores. Many NoSQL systems trade some of the relational features for flexibility or speed in a narrower job.

❓ Which database should a beginner learn first?

A relational one. SQL is a skill that carries across PostgreSQL, MySQL, SQLite and SQL Server, and SQLite lets you practice without installing a server.

❓ Can PostgreSQL store JSON like MongoDB?

Yes, in part. PostgreSQL offers json and jsonb column types with operators and indexing for them, so you can mix flexible documents and structured tables in one database.

❓ Is Redis a database?

It is an in-memory data store that can persist data to disk, and people use it as a cache, message broker and fast key-value store. Most teams run it alongside a main database rather than instead of one.

❓ Do I need a graph database for social features?

Not at first. Simple relationships such as followers or friends fit in relational tables. A graph database starts to pay off when you routinely query paths several hops deep.

Sources

Add Valley4Techs as a Preferred Source

Follow us on Google News for the latest updates

Add Now
Comments