0
0
HLDsystem_design~7 mins

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

Choose your learning style9 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.