SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
  • Common Database Types
  • SQL vs NoSQL
  • ACID Transactions
  • B-Trees and B+ Trees
  • LSM Trees
  • Transaction Isolation Levels
  • Locking and MVCC
  • Normalization
  • OLTP vs OLAP
  • BASE and PACELC
  • Write-Ahead Log (WAL)
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Data and Storage/Database Basics

Common Database Types

A quick map of the main kinds of databases and what each one is good at.

⏱ 12 min readBeginnerMust KnowHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A database is a program that stores data safely and lets you ask for it later. There is not just one kind. Each kind is built for a different job.

  • Relational (tables): the safe all-rounder. Good for connected data and money.
  • Document, key-value, wide column: flexible or very fast, built to spread over many machines.
  • Graph, time series, search, vector: special tools for links, time, words and meaning.
  • Big systems often use several databases at once. Pick by the job, not by fashion.

02Concept: What is it?

Think of a database as a specialised filing system. All of them save and fetch data, but each one organises the data in a different shape. The shape decides what is easy and what is painful.

The main families
🧾Relationaltables and joins
πŸ“„DocumentJSON records
πŸ”‘Key-valuea giant dictionary
πŸ›οΈWide columnhuge writes
πŸ•ΈοΈGraphthings and links
⏱️Time seriesdata by time
πŸ”ŽSearch enginewords and ranking
🧭Vectorsimilar meaning
Eight common kinds. The next topics in this part look at most of them one by one.
🧠One question to start with
Ask: how will I read this data? By id? By a time range? By words? By links between things? The way you read decides the database type.

03Real-Life Example

🍡The Tool Shop
A good workshop does not have one tool. A hammer drives nails, a saw cuts wood, a tape measure measures. You can bang a nail with a spanner, but it is slow and ugly. In the same way, you can store everything in one database, but a tool made for the job is faster and safer.
Tool shop
Databases
Toolbox with labelled drawers
Relational database (tables with rules)
A box of loose papers, each different
Document database
A cloakroom: ticket number in, coat out
Key-value store
A giant ledger with many writers
Wide column database
A map of friends with lines between them
Graph database
A diary written day by day
Time series database
The index at the back of a book
Search engine
Finding a similar-looking photo
Vector database
An online shop using four databases
πŸ–₯️Shop app
→↓
🧾Ordersrelational
πŸ”‘Cartskey-value
πŸ”ŽSearchsearch engine
⏱️Metricstime series
Each part of the shop uses the store that suits it. This is called polyglot persistence (many languages of storage).

04Why Do We Need It?

🎯Right tool, right job
A good fit gives you speed and simple code. A bad fit gives you slow queries and ugly workarounds.
πŸ“ˆScale
Some types spread easily over many machines. Others are happier on one strong machine.
πŸ›‘οΈSafety
Money and orders need strong promises. A page view counter does not.
πŸ’ΈCost
A database made for your pattern needs fewer servers for the same load.
πŸ—£οΈInterviews
Choosing a database and explaining why is part of almost every design talk.
🧩Clear thinking
Knowing the families stops you from using one tool for everything.
Start from how you read and write the data, then pick the database type. Never start from a brand name.

05Where Do We Use It?

🏦Banks and payments
Relational: strict rules, safe transactions.
πŸ›’Carts and sessions
Key-value: look up by id, in a flash.
πŸ“šCatalogs and content
Document: each item has a different shape.
πŸ‘₯Social links
Graph: friends, followers and recommendations.
πŸ“‘Monitoring and IoT
Time series: millions of stamped points.
πŸ€–AI search
Vector: find things that mean the same.

06Types / Parts / How It Works

The families at a glance

TypeData shapeBest atExample products
RelationalTables of rows and columns, linked by keysJoins, transactions, reportsPostgreSQL, MySQL
DocumentJSON-like documentsFlexible shapes, one record read in one goMongoDB, Firestore
Key-valueKey points to a valueVery fast lookups, caches, sessionsRedis, DynamoDB
Wide columnRows with many flexible columns, grouped by a partition keyHuge write volume over many machinesCassandra, Bigtable
GraphNodes (things) and edges (links)Walking many links quicklyNeo4j, Amazon Neptune
Time seriesPoints stamped with timeMetrics, sensors, rolling windowsInfluxDB, TimescaleDB
Search engineInverted index of wordsFull-text search and rankingElasticsearch, OpenSearch
VectorLists of numbers (embeddings)Nearest neighbour: similar meaningPinecone, pgvector
General purpose
Relational and document
  • Handle many kinds of query
  • A safe default to start with
  • Large community and tools
  • Not the best at any one extreme
Specialist
Key-value, graph, time series, search, vector
  • Excellent at one access pattern
  • Can be much faster or cheaper there
  • Weak outside that job
  • Another system to run and learn
Which question am I asking?
Questions
Join and sum rows safely?
Get one thing by its id?
Who is connected to whom?
What happened between 2 and 3 pm?
Which docs mention these words?
What looks like this?
→↓
Usual answers
Relational
Key-value / document
Graph
Time series
Search engine
Vector
Safe, connected data Fast by key Links
A simple first guide. Real choices also depend on size, budget and team skills.
VisualizerWhich database fits my app?Pick a scenario, then tick and untick needs. Watch which type wins, and when no single type is enough.
Pick a scenario

Money must never be lost or double counted, and reports join many tables.

...or tick what your app needs
Fit for your needs (100% means the type is excellent at every need you ticked)
Relational
PostgreSQL, MySQL
100%
Document
MongoDB, Firestore
33%
Graph
Neo4j
33%
Key-value
Redis, DynamoDB
0%
Wide column
Cassandra, Bigtable
0%
Time series
InfluxDB, TimescaleDB
0%
Search engine
Elasticsearch, OpenSearch
0%
Vector
Pinecone, pgvector
0%
Needs ticked
1
Best fit
Relational
PostgreSQL, MySQL
Coverage
100%
of the best type
Relational databases (PostgreSQL, MySQL) cover every need you picked.

The fit numbers are a simple rule of thumb to build intuition, not a benchmark. Real products overlap a lot (for example, PostgreSQL can store JSON and, with extensions, vectors), so always test with your own data.

07How to Use It / Steps / Implementation

How to choose a database

  1. 1
    Write down the access patterns
    List the top queries: read by id, filter by date, search words, join three tables. Include how many per second.
  2. 2
    Estimate size and growth
    A few gigabytes fits on one machine. Many terabytes or a huge write rate may need a database that spreads out.
  3. 3
    Decide how correct it must be
    Money and stock need strong promises (see ACID). Likes and views can be a little late (eventual consistency).
  4. 4
    Start with a safe default
    For most apps, a relational database such as PostgreSQL is the best first choice. It does a lot well.
  5. 5
    Add a specialist only for a clear gap
    Search too slow? Add a search engine. Hot lookups? Add a cache or key-value store. Do not add one just to be modern.
  6. 6
    Keep one source of truth
    Pick one database as the master record. Feed the others from it, so they can be rebuilt.
  7. 7
    Test with your own data
    Benchmarks on blog posts are not your workload. Load a realistic sample and measure.
πŸ’‘A good default
If you are unsure in an interview, say: β€œI would start with a relational database, then add a cache, and a search engine if we need text search.” Then explain when you would change that.

08Key Terms to Know

TermMeaning in easy words
DatabaseA program that stores data safely and lets you query it
SchemaThe agreed shape of the data: names, types and rules
QueryA question you ask the database
Access patternHow your app reads and writes: by id, by range, by words and so on
JoinCombining rows from two tables using a shared key
IndexA shortcut structure that avoids reading everything
TransactionA group of changes that succeed or fail together
Embedding / vectorA list of numbers that captures the meaning of text, an image or a song
Polyglot persistenceUsing several kinds of database in one system
Source of truthThe one place whose data is considered correct

09Pros, Cons and Trade-offs

βœ… Pros
  • Each type is excellent at its own job
  • You can scale the part that needs it
  • Simple code when the data shape matches the database
  • Lower cost for specialised workloads
⚠️ Cons
  • Many choices to learn and compare
  • Each extra database is more to run, secure and monitor
  • Data copied between stores can drift apart
  • Cross-database transactions are hard
  • Hype can push teams to pick the wrong one

Trade-offs

ChoiceWe gainWe lose
One general databaseSimple to run, one place for dataMay be slow or costly for extreme patterns
Many specialist databasesBest speed per jobMore systems, more sync problems
Strong consistencyCorrect answers alwaysHarder to spread over regions
Flexible schemaFast changes, easy startMess can grow, fewer checks by the database
Managed cloud databaseLess operations workCost and vendor lock-in

10Common Problems and Mistakes

Common mistakes

  1. Choosing by hype. β€œEveryone uses X” is not a reason. Your access patterns are.
  2. Using one tool for everything. Full-text search in a plain table with LIKE is slow at scale.
  3. Using too many tools. Five databases for a small app is a heavy burden for a small team.
  4. Treating a cache as the main store. A cache can be wiped. Keep a durable source of truth.
  5. Ignoring consistency needs. Putting payments in a store that is only eventually consistent can lose money.
  6. Forgetting to plan for growth. A choice that works for 1,000 users may not work for 100 million.

Things to watch

πŸ”„Data drift
Copies in search or cache can go out of date. Feed them from the source of truth.
🧰Operations cost
Backups, upgrades, monitoring and alerts multiply with every new database.
πŸ”€Overlap
Products blur the lines. PostgreSQL stores JSON. MongoDB has transactions. Check current features.
πŸ”Security
Each system needs its own access rules, encryption and audit.

11Real-World Examples

🎬Streaming and social apps
Large companies mix stores: a wide column store for huge activity data, a relational one for billing, a search engine for finding titles (general idea).
πŸ›οΈE-commerce
Orders in a relational database, carts in a key-value store, search in Elasticsearch or OpenSearch.
πŸ“‘Monitoring tools
Metrics sit in a time series database such as Prometheus, InfluxDB or TimescaleDB.
πŸ‘₯Recommendation features
Graph databases help with friends and fraud rings. Vector databases help with similar items.
🏦Finance
Core ledgers usually live in relational databases because of strong transaction guarantees.
πŸ€–AI products
Embeddings are stored in a vector database, or in a relational one with a vector extension, for similarity search.
πŸ“Note
These are general patterns. Every company has its own stack, and it changes over time.

12Related Topics to Read Next

SQL vs NoSQL→

The most common split between the families.

Relational Databases→

The safe default, in detail.

Document Databases→

Flexible JSON-like records.

Key-Value Databases→

The giant dictionary.

Vector Databases→

Search by meaning.

ACID Transactions→

The safety promises behind relational databases.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”Relational: tables, joins, transactions. The safe default.
  • βœ”Document: flexible JSON records, read one whole record at a time.
  • βœ”Key-value: a giant dictionary, very fast by key.
  • βœ”Wide column: huge write volume spread over many machines.
  • βœ”Graph / time series / search / vector: links, time, words, meaning.
  • βœ”Choose by: access pattern, size, growth and needed correctness.
  • βœ”Many stores: keep one source of truth and rebuild the rest from it.
  • βœ”One line: β€œPick the database for the way you read the data, not for the brand.”
← Previous
CDN Push vs Pull
Next β†’
SQL vs NoSQL