Databases demystified

Types of databases

18 min
What you'll learn
  • tell the five families of databases apart — relational, key-value, document, columnar, graph — by how they store data and what they’re typically used for
  • pick a store that fits the shape of your queries: relational by default, with Redis/MongoDB/ClickHouse reserved for a specific job rather than chosen "because it’s trendy"
  • explain the difference between and , and why the product writes to PostgreSQL while analytics reads from a columnar
  • spot a "document-shaped" need that a jsonb column in PostgreSQL can cover without a second database

On the way to the index, QUERY pauses by a darkened section of the vault. Behind the glass is a registry of old-Earth archives that didn’t survive the Great Severance: the cache nodes went dark first, the document stores arrived as ragged fragments, the analytical clusters sit silent across whole sectors. The relational Kotomarket , though, survived in full — every link intact.

A dark gallery of dead old-Earth archives in various shapes; at its far end a relational hologram of linked tables glows intact
Stores come in many kinds: key-value, document, columnar. Our surviving snapshot is relational: tables and the links between them.

Not only relational

For Kotomarket, relational databases suit us especially well: buyers, orders, and products fall naturally into tables with links between them. That’s what this course is about. But across the industry — in 2024 just as today — you’ll meet other approaches too:

  • key-value (Redis) — a blazingly fast "key → value" format for when you need to grab a simple record instantly;
  • document-oriented (MongoDB) — stores flexible documents whose structure can differ from record to record;
  • columnar (ClickHouse) — strong at analytics, when you need to crunch metrics over enormous volumes of data fast.

Most of the popular DBMSs for product development stay relational: PostgreSQL, MySQL, Oracle, MS SQL. Master SQL here and you’ll be able to read data confidently at very different companies.

QUERY: Each archive has its own job: one prizes speed, another flexibility, a third analytics. You and I got lucky: we inherited the one where everything is connected to everything.

The registry of families: sorted out

The registry behind the glass is a good excuse to dig into the database families for real: "what kinds of databases are there, and when do you pick which" is one of the most common interview questions. We’ll go through five families in a row, each laid out the same way: storage, strengths and weaknesses, products, and when to pick it.

Relational databases: a strict schema and links

Storage: rows in tables with a schema declared up front — every column has a type, links between tables run through keys, and the guards integrity itself: you can’t record an order for a buyer who doesn’t exist. Changes are executed as with guarantees — "create the order + decrement stock" are either both written or neither is.

users — a tableidPKintegerfull_nametextcitytextsignup_datedate123Artyom VolkovEkaterina AlekseevNikolay NikitinSaint PetersburgYekaterinburgAlmaty2024-10-202025-01-252024-09-21rows are records · columns are typed properties · there can be just one table
At its core a relational database is a table: rows are records, columns each have a type. Key-based links come on top — and there can be just one table.
usersidnameproductsidtitleordersiduser_idproduct_idFK → PKproducts · banking · ordersPostgreSQL · MySQL
And once there are several tables, keys link them together: a strict schema with relationships between tables.
  • Strengths: expressive SQL (filters, joins, ), integrity enforced at the database level, .
  • Weaknesses: the schema has to be thought through in advance and migrated when things change; scaling horizontally across many servers is harder than for the families.
  • Products: PostgreSQL, MySQL/MariaDB, Microsoft SQL Server, Oracle, SQLite.
  • When to pick it: users, orders, money, inventory — anywhere the data is connected and a bad write is unacceptable. For a new product, this is the default choice.

Key-value databases: a dictionary at top speed

Storage: one giant "key → value" dictionary, most often held in RAM. No tables, no links: put it in by key, pull it back out by key in a fraction of a millisecond.

-- This isn’t SQL, these are Redis commands
SET session:8f3a '{"user_id": 42}'
GET session:8f3a
keyvalueuser:42Anna · Moscowcart:42[milk, cheese]sess:9factive until 18:40lookup by key — instantcache · sessions · carts — Redis
Key-value: instant access by an exact key — and no querying "by content."
  • Strengths: speed, simplicity, easy scaling.
  • Weaknesses: you can only query by an exact key — there’s no way to ask this format "find all sessions for buyers in Moscow"; the inside of a value is opaque to the database.
  • Products: Redis, Memcached, Amazon DynamoDB.
  • When to pick it: caches, sessions, counters, queues, leaderboards — hot data sitting alongside your main database, not instead of it.

Document databases: flexible JSON

Storage: records are documents grouped into collections. The schema isn’t declared up front: a smartwatch has a "battery life" field, a hoodie has a "size chart," and both documents sit happily in the same collection. Nested structures live right inside the document, with no separate tables.

document 1document 2{name: "Mouse",price: 390,tags: ["hit"]}{name: "Food",brand: "Kote",specs: {weight: "2 kg"}}fields and nesting differ — schema is flexiblecatalogs · profiles · CMS — MongoDB
The document model: flexible JSON documents with different sets of fields live in one collection.
  • Strengths: flexible structure, a document is read in full in a single fetch, handy for catalogs with heterogeneous attributes and for quick prototypes.
  • Weaknesses: links between documents are the application’s job, and there’s almost no real ; without discipline a collection turns into a zoo of incompatible formats.
  • Products: MongoDB, Couchbase, Firestore.
  • When to pick it: content, catalogs with wildly varying fields, profiles, and settings.

The line between families is blurry, by the way: PostgreSQL can store documents in a jsonb column and build indexes over them — that often covers a "document-shaped" need without a second database:

-- An example outside our snapshot: attrs is a jsonb column
SELECT name, attrs->>'color' AS color
FROM catalog
WHERE attrs @> '{"brand": "Kotomarket"}';

Columnar databases: analytics over billions of rows

Storage: values are laid out by column rather than by row: all the prices together, all the dates together. A "average order value by month" query reads just two columns off disk instead of the whole table, and values of the same kind compress beautifully. Hence the speed on .

-- The columnar database’s favorite genre (ClickHouse dialect: toYYYYMM is its function)
SELECT toYYYYMM(created_at) AS month, avg(total_amount) AS avg_check
FROM orders
GROUP BY month;
columnar storage: the table is laid out by columnsuser_ideventpricets39029905901490990AVG(price): only one column is readevent analytics · metrics — ClickHouse
Columnar storage: an aggregate reads only the columns it needs off disk, not the whole table row by row.
  • Strengths: and scans over billions of rows, heavy compression.
  • Weaknesses: pinpoint updates and deletes are expensive; reading a single full row is slower than in a row store; transactional load is simply not what they’re built for.
  • Products: ClickHouse, BigQuery, Snowflake, Amazon Redshift.
  • When to pick it: analytics, reports, events, logs, metrics — usually alongside the "live" relational database.

While we’re here, two terms interviewers love: (online processing — lots of short writes and reads, the world of PostgreSQL) and (online analytical processing — heavy analytical reads over the entire history, the world of ClickHouse). The product writes to the OLTP database, while analytics reads from an OLAP copy.

And one last family: graph databases

When the links matter more than the records themselves — "friends of friends," delivery routes, recommendation chains — people reach for graph databases (Neo4j, Memgraph). The data in them is nodes (the records) and edges (the links between records); traversing links of any depth is a native operation. In a relational database, that same traversal is a staircase of joins that grows heavier with every level.

friendAnnBobCaraDanEvenodes are records, edges are links; "friends of friends" = two edges away
A graph: circle nodes are records (people, say), edge lines are the links between them. "Friends of friends" is just hopping along the edges.

A mini-guide: how to choose a database for a project

  1. Start with the questions, not the data. What queries will the product ask: pinpoint "by key," relational "who bought what," analytical "how much this month"?
  2. Default to relational. Connected data plus a high cost of error (orders, payments, stock) means PostgreSQL. The safest start for almost any product.
  3. Specialized databases are for a specific job, not "just in case." Hot reads are straining you — add a Redis cache. Analytics has grown — replicate events into ClickHouse. Wildly variable fields — first jsonb in PostgreSQL, and only when that isn’t enough, a document database.
  4. Count the cost of a second database. Every new means data synchronization, backups, monitoring, and one more technology for the team to maintain.

Pitfall: picking a database "because it’s trendy." "Let’s use MongoDB — schemaless is faster" ends with you designing a schema anyway, except now its rules live in application code instead of under the database’s protection. The reverse pitfall happens too: dragging in ClickHouse for volumes PostgreSQL in milliseconds. You choose a database by the shape of your queries and the cost of error — not by the hype.

ClickHouse is a columnar for analytics: people love it for fast calculations over large tables of events and orders. It has its own SQL ; the platform has a separate ClickHouse trainer. Our , though, is relational: next, QUERY opens the index and shows what it’s made of — tables, rows, and the key-threads between them.

Interview question

Interview question: what types of databases do you know, and how would you choose a store for a new service?

Strong answer: relational (PostgreSQL, MySQL) — strict schema, links, ; key-value (Redis) — caches, sessions, counters; document (MongoDB) — flexible documents; columnar (ClickHouse, BigQuery) — analytics over large volumes; graph (Neo4j) — deep relationships. I choose based on the shape of the queries: I make a relational database the source of truth, and bring in specialized ones for a specific job (cache, analytics) rather than in place of it.

Follow-up: what’s the difference between and ?

Strong answer: OLTP is many short write-and-read transactions (placing orders), with row-store DBMSs like PostgreSQL; OLAP is heavy analytical reads and over history, with columnar DBMSs like ClickHouse. They often work as a pair: the product writes to OLTP, analytics reads from an OLAP .

Check yourself
Which type of database is usually chosen for analytics over large data volumes?