Bird
Raised Fist0
HLDsystem_design~7 mins

NoSQL database types (document, key-value, column, graph) in HLD - System Design Guide

Choose your learning style10 modes available
Problem Statement
Traditional relational databases struggle to handle large volumes of diverse and rapidly changing data structures. This leads to slow queries, rigid schemas, and difficulty scaling horizontally across many servers.
Solution
NoSQL databases use different data models tailored for specific use cases. They store data as documents, key-value pairs, wide columns, or graphs, allowing flexible schemas and efficient scaling. Each type optimizes data storage and retrieval for particular patterns of access and relationships.
Architecture
┌───────────────┐       ┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Document DB   │       │ Key-Value DB  │       │ Column DB     │       │ Graph DB      │
│ ┌─────────┐  │       │ ┌─────────┐  │       │ ┌─────────┐  │       │ ┌─────────┐  │
│ │ JSON    │  │       │ │ Key     │  │       │ │ Column  │  │       │ │ Nodes   │  │
│ │ Document│  │       │ │ Value   │  │       │ │ Families│  │       │ │ & Edges │  │
│ └─────────┘  │       │ └─────────┘  │       │ └─────────┘  │       │ └─────────┘  │
└─────┬────────┘       └─────┬────────┘       └─────┬────────┘       └─────┬────────┘
      │                      │                      │                      │       
      │                      │                      │                      │       
      │                      │                      │                      │       
      └───────────────┬──────┴───────────────┬──────┴───────────────┬──────┘       
                      │                      │                      │              
               Flexible schema         Simple key-value       Wide column store   Graph relationships
               for nested data         for fast lookup       for large tables    for connected data

This diagram shows four NoSQL database types with their core data structures and typical use cases.

Trade-offs
✓ Pros
→
Document DBs allow flexible, nested data storage ideal for JSON-like objects.
→
Key-Value DBs provide very fast lookups for simple key-based access patterns.
→
Column DBs efficiently store and query large sparse datasets with many columns.
→
Graph DBs excel at representing and querying complex relationships and networks.
✗ Cons
→
Document DBs can have slower joins and complex queries compared to relational DBs.
→
Key-Value DBs lack query flexibility beyond key-based access.
→
Column DBs require careful schema design to optimize query performance.
→
Graph DBs can be less efficient for simple key-value or document workloads.
Use Document DBs for flexible, hierarchical data like user profiles or content. Use Key-Value DBs for caching or session stores with simple access. Use Column DBs for analytics on large datasets with many attributes. Use Graph DBs for social networks, recommendation engines, or fraud detection where relationships matter.
Avoid Document DBs when strict ACID transactions and complex joins are needed. Avoid Key-Value DBs when querying by anything other than key is required. Avoid Column DBs for small datasets or simple key-value needs. Avoid Graph DBs for flat data without complex relationships.
Real World Examples
Amazon
Uses DynamoDB (Key-Value) for fast, scalable session management and shopping cart data.
Netflix
Uses Cassandra (Column) to store large volumes of user viewing history for analytics.
Facebook
Uses a Graph database to model and query social connections and interactions.
Alternatives
Relational Database
Uses fixed schemas with tables and joins instead of flexible or specialized data models.
Use when: When strong consistency, complex transactions, and structured query language are required.
NewSQL
Combines relational model with horizontal scalability and distributed architecture.
Use when: When you need relational features with cloud-scale performance.
Summary
NoSQL databases use different data models to handle diverse and large-scale data efficiently.
Document, Key-Value, Column, and Graph databases each optimize for specific data and query patterns.
Choosing the right NoSQL type depends on your application's data structure, query needs, and scalability requirements.

Practice

(1/5)
1. Which NoSQL database type stores data as flexible JSON-like documents, allowing nested structures?
easy
A. Document database
B. Key-value database
C. Column database
D. Graph database

Solution

  1. Step 1: Understand document database structure

    Document databases store data in JSON-like documents that can have nested fields and arrays.
  2. Step 2: Compare with other NoSQL types

    Key-value stores use simple key and value pairs, column stores organize data by columns, and graph databases focus on relationships.
  3. Final Answer:

    Document database -> Option A
  4. Quick Check:

    Nested JSON-like data = Document database [OK]
Hint: Documents = JSON-like nested data [OK]
Common Mistakes:
  • Confusing key-value stores with document stores
  • Thinking column stores store JSON documents
  • Mixing graph databases with document databases
2. Which of the following is the correct way to describe a key-value store?
easy
A. Stores data as interconnected nodes and edges
B. Stores data in tables with rows and columns
C. Stores data as simple pairs of keys and values
D. Stores data as nested JSON documents

Solution

  1. Step 1: Define key-value store

    Key-value stores save data as pairs: a unique key and its associated value, like a dictionary.
  2. Step 2: Eliminate other options

    Graphs use nodes and edges, column stores use tables with columns, and document stores use nested JSON.
  3. Final Answer:

    Stores data as simple pairs of keys and values -> Option C
  4. Quick Check:

    Key-value = key and value pairs [OK]
Hint: Key-value = simple pairs [OK]
Common Mistakes:
  • Confusing graph databases with key-value stores
  • Thinking key-value stores use tables
  • Mixing document stores with key-value stores
3. Given a graph database storing people and their friendships, which query result would you expect from a query asking for all friends of 'Alice'?
medium
A. A list of key-value pairs with friend names
B. A set of nodes connected to 'Alice' by edges labeled 'friend'
C. A table with columns for friend names and ages
D. A JSON document with nested friend details

Solution

  1. Step 1: Understand graph database query output

    Graph queries return nodes and edges; friends of 'Alice' are nodes connected by 'friend' edges.
  2. Step 2: Compare with other data formats

    Key-value pairs, tables, and JSON documents are typical of other NoSQL types, not graph databases.
  3. Final Answer:

    A set of nodes connected to 'Alice' by edges labeled 'friend' -> Option B
  4. Quick Check:

    Graph query = nodes and edges [OK]
Hint: Graph = nodes connected by edges [OK]
Common Mistakes:
  • Expecting JSON documents from graph queries
  • Thinking graph data is tabular
  • Confusing key-value pairs with graph edges
4. You wrote a query to retrieve data from a column store but got an error. Which mistake likely caused this?
medium
A. Used SQL SELECT syntax without specifying columns
B. Used key-value pairs instead of column names
C. Tried to traverse edges like in a graph database
D. Tried to query nested JSON documents directly

Solution

  1. Step 1: Understand column store query requirements

    Column stores require specifying columns in queries; missing columns causes errors.
  2. Step 2: Analyze other options

    Nested JSON is for document stores, key-value pairs are for key-value stores, and edge traversal is for graph databases.
  3. Final Answer:

    Used SQL SELECT syntax without specifying columns -> Option A
  4. Quick Check:

    Column store queries need columns specified [OK]
Hint: Specify columns in column store queries [OK]
Common Mistakes:
  • Using document store syntax on column stores
  • Trying graph traversal in column stores
  • Confusing key-value pairs with columns
5. You need to design a social network app that quickly finds mutual friends between users. Which NoSQL database type is best suited and why?
hard
A. Column database, because it stores data in columns
B. Key-value store, because it is the fastest for any query
C. Document database, because it stores nested user data
D. Graph database, because it efficiently handles relationships

Solution

  1. Step 1: Identify the data need for mutual friends

    Finding mutual friends requires exploring relationships between users efficiently.
  2. Step 2: Match data need to NoSQL type

    Graph databases specialize in storing and querying relationships like friendships quickly.
  3. Step 3: Evaluate other options

    Document stores handle nested data but not relationship queries efficiently; key-value stores are simple but not optimized for relationships; column stores focus on columnar data.
  4. Final Answer:

    Graph database, because it efficiently handles relationships -> Option D
  5. Quick Check:

    Relationship queries = Graph database [OK]
Hint: Use graph DB for relationship-heavy apps [OK]
Common Mistakes:
  • Choosing document DB for relationship queries
  • Assuming key-value stores handle complex queries
  • Confusing column stores with relationship handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard