Database Management SystemUnit 1111 min read
NoSQL Databases: Types, Models & Real-World Use
Unit 11 of Database Management System: Explores NoSQL databases—non-relational systems designed for scalability, flexibility, and high-speed data handling, covering their types (document, key-value, column-family, graph), data models, query languages, and real-world applications like eSewa’s transaction logs and Daraz’
TAKEAWAYS:
- NoSQL databases are schema-less, horizontally scalable, and optimized for big data and high write/read throughput (unlike relational DBMS).
- The four main types are document, key-value, column-family, and graph databases, each suited to specific data structures and query patterns.
- NoSQL databases use query languages like MongoDB’s JSON-based queries, Cassandra’s CQL, and Neo4j’s Cypher, which differ from SQL.
- They excel in real-time analytics, IoT data, and unstructured data (e.g., social media feeds, sensor logs), but lack ACID guarantees like relational DBs.
- Trade-offs: NoSQL offers flexibility and performance but sacrifices complex transactions and strict data integrity compared to SQL.
- Companies like WhatsApp (key-value for message storage) and Pathao (graph for ride routing) rely on NoSQL for scalability and low-latency access.
1. Introduction to NoSQL Databases
NoSQL (Not Only SQL) databases are non-relational systems designed to handle large-scale, distributed data with flexible schemas and horizontal scalability. Unlike relational databases (e.g., MySQL, PostgreSQL), NoSQL databases prioritize performance, scalability, and agility over rigid schemas and complex joins.
Key Characteristics of NoSQL Databases
NoSQL databases are categorized based on their data models and query languages. Below is a comparison table highlighting their core traits:
| Feature | Relational Databases (SQL) | NoSQL Databases |
|---|---|---|
| Schema | Fixed schema (tables, rows) | Schema-less or dynamic schema |
| Scalability | Vertical scaling (upgrading hardware) | Horizontal scaling (adding nodes) |
| Data Model | Tabular (rows and columns) | Document, key-value, column-family, graph |
| Query Language | SQL (Structured Query Language) | JSON, CQL, Cypher, or proprietary |
| ACID Compliance | Strong (Atomicity, Consistency, Isolation, Durability) | Often BASE (Basically Available, Soft state, Eventually consistent) |
| Use Case | Structured data, transactions | Unstructured data, big data, real-time analytics |
Why NoSQL?
NoSQL databases are ideal for:
- Big data (e.g., social media feeds, IoT sensor data).
- High write/read throughput (e.g., e-commerce product catalogs).
- Distributed systems (e.g., WhatsApp message storage).
- Unstructured or semi-structured data (e.g., JSON logs, XML documents).
2. Types of NoSQL Databases
NoSQL databases are classified into four primary types, each with distinct data models and use cases:
a) Document Databases
Document databases store data in JSON, BSON, or XML format. Each document is a self-contained record with nested fields.
Example: MongoDB (used by eSewa for transaction logs and user profiles).
How it works:
- Data is stored as documents (e.g., a user profile with nested fields like
address,orders). - Queries use JSON-like syntax (e.g.,
{ "status": "active" }). - Supports rich queries (e.g., filtering, aggregation).
Mermaid Diagram: Document Structure
mindmap
root((MongoDB Document))
- User["{
\"_id\": ObjectId("507f1f77bcf86cd799439011"),
\"name\": \"John Doe\",
\"address\": {
\"street\": \"123 Main St\",
\"city\": \"Kathmandu\"
},
\"orders\": [
{ \"order_id\": \"ORD001\", \"amount\": 100 },
{ \"order_id\": \"ORD002\", \"amount\": 200 }
]
}"]b) Key-Value Databases
Key-value databases store data as pairs of keys and values, where the key is unique and the value can be any data type (e.g., string, binary).
Example: Redis (used by WhatsApp for caching and real-time message storage).
How it works:
- Fast read/write operations (ideal for caching).
- No querying (only retrieval by key).
- In-memory storage (high performance).
Mermaid Diagram: Key-Value Store
mindmap
root((Key-Value Store))
- Key["user_123"]
- Value["Alice"]
- Key["email"]
- Value["alice@example.com"]
- Key["product_456"]
- Value["99.99"]
- Key["price"]
- Value["Laptop"]
- Key["name"]
- Value["100"]
- Key["stock"]c) Column-Family Databases
Column-family databases store data in columns (not rows), optimized for analytical queries and large datasets.
Example: Apache Cassandra (used by Daraz for product catalogs and inventory management).
How it works:
- Data is organized by column families (e.g.,
users,products). - Queries filter by column names (not rows).
- High write throughput (ideal for time-series data).
Mermaid Diagram: Column-Family Structure
mindmap
root((Cassandra Column Family))
- Users
- user_id["user_123"]
- name["Alice"]
- email["alice@example.com"]
- age["25"]
- Products
- product_id["prod_789"]
- name["Laptop"]
- price["999.99"]
- stock["50"]d) Graph Databases
Graph databases store data as nodes, edges, and properties, ideal for relationship-heavy data.
Example: Neo4j (used by Pathao for ride routing and driver-ride matching).
How it works:
- Nodes represent entities (e.g., users, rides).
- Edges represent relationships (e.g.,
DRIVES,REQUESTS). - Properties store attributes (e.g.,
driver_id,ride_status).
Mermaid Diagram: Graph Database
graph TD
A["User Alice"] -->|"DRIVES"| B["Driver 123"]
B -->|"HAS_RIDE"| C["Ride 456"]
C -->|"TO"| D["Location: Thapathali"]
A -->|"REQUESTED"| C3. NoSQL Data Models and Query Languages
NoSQL databases use proprietary query languages tailored to their data models:
| Database Type | Example Database | Query Language | Example Query |
|---|---|---|---|
| Document | MongoDB | MongoDB Query Language | db.users.find({ status: "active" }) |
| Key-Value | Redis | Redis Commands | GET user_123 |
| Column-Family | Cassandra | CQL (Cassandra Query Language) | SELECT * FROM users WHERE user_id = '123'; |
| Graph | Neo4j | Cypher | MATCH (u:User)-[:DRIVES]->(d:Driver) RETURN u, d; |
Worked Example: Querying a Document Database (MongoDB)
Scenario: eSewa wants to find all active users who made transactions in the last 30 days.
// Sample MongoDB document
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"name": "John Doe",
"status": "active",
"transactions": [
{ "amount": 100, "date": ISODate("2023-10-01") },
{ "amount": 200, "date": ISODate("2023-10-15") }
]
}
Query:
db.users.find({
status: "active",
transactions: {
$elemMatch: {
date: { $gte: new Date("2023-09-01") }
}
}
});
Output: Returns all active users with transactions in the last 30 days.
4. Advantages and Disadvantages of NoSQL Databases
Advantages
✅ Scalability: Horizontally scales by adding nodes (e.g., Ncell’s user data). ✅ Flexibility: Schema-less design allows dynamic data changes (e.g., Daraz’s product attributes). ✅ Performance: Optimized for high-speed reads/writes (e.g., WhatsApp’s message delivery). ✅ Big Data Support: Handles unstructured/semi-structured data (e.g., NEPSE’s market trends).
Disadvantages
❌ No ACID: Lacks strong transaction support (risk of data inconsistency). ❌ Limited Querying: Complex joins are not supported (unlike SQL). ❌ Eventual Consistency: Data may not be immediately consistent across nodes.
5. Real-World Applications of NoSQL Databases
In the Real World
eSewa (Nepal)
- Uses: Document Database (MongoDB) for storing user profiles, transaction logs, and payment records.
- Why NoSQL?
- Handles millions of transactions daily with low latency.
- Flexible schema to accommodate new payment methods (e.g., UPI, credit cards).
WhatsApp (Global)
- Uses: Key-Value Database (Redis) for caching user sessions and real-time message storage.
- Why NoSQL?
- High write throughput for 2+ billion daily messages.
- Low-latency access for instant messaging.
Pathao (Nepal)
- Uses: Graph Database (Neo4j) for ride routing and driver-ride matching.
- Why NoSQL?
- Relationship-heavy data (drivers, riders, rides, locations).
- Real-time optimizations for dynamic ride assignments.
Daraz (Nepal)
- Uses: Column-Family Database (Cassandra) for product catalogs and inventory management.
- Why NoSQL?
- High write/read scalability for millions of products.
- Analytical queries for demand forecasting.
6. Comparison: NoSQL vs. SQL Databases
| Feature | NoSQL Databases | SQL Databases |
|---|---|---|
| Schema | Schema-less or dynamic | Fixed schema (tables, rows) |
| Scalability | Horizontal (add nodes) | Vertical (upgrade hardware) |
| Query Language | JSON, CQL, Cypher | SQL |
| ACID Compliance | BASE (Eventual Consistency) | Strong ACID |
| Use Case | Big data, real-time analytics | Structured data, transactions |
| Example | MongoDB, Cassandra, Neo4j | MySQL, PostgreSQL, Oracle |
7. Exam Tip
- Understand the 4 types of NoSQL databases (document, key-value, column-family, graph) and their real-world examples.
- Compare NoSQL vs. SQL in terms of schema, scalability, and query languages.
- Practice NoSQL queries (e.g., MongoDB’s JSON queries, Cassandra’s CQL).
- Relate to Nepalese companies (eSewa, Daraz, Pathao) and explain why they use NoSQL.
- Discuss trade-offs (e.g., NoSQL’s lack of ACID vs. SQL’s strong transactions).
Common Exam Questions:
- "Explain the characteristics of NoSQL databases with examples." → Answer: Schema-less, horizontally scalable, BASE model, used by eSewa for transactions.
- "Compare NoSQL and SQL databases." → Use the table above and highlight scalability vs. ACID.
- "How does MongoDB store data? Give a query example."
→ Answer: Stores as JSON documents; query example:
db.users.find({ status: "active" }).
Based on the TU BIT syllabus for Database Management System (BIT202), unit 11.
Discussion
Loading…