Data Systems
NoSQL Deep Dive
NoSQL databases trade ACID guarantees for horizontal scalability and flexible data models. Master the four categories of NoSQL databases and their optimal use cases.
- Scalability β Horizontal scaling across commodity servers
- Flexibility β Schema-less or schema-on-read data models
- Performance β Optimized for specific access patterns
NoSQL is not "No SQL"βit's "Not Only SQL." Choose the right tool for the job.
The Four Categories of NoSQL
Document Databases (MongoDB)
MongoDB Internals
| Component | Purpose |
|---|---|
| WiredTiger | Storage engine with document-level locking |
| B-tree indexes | Primary index structure |
| Replica Set | Primary-secondary replication for HA |
| Sharding | Horizontal scaling across clusters |
| Aggregation Pipeline | Server-side data processing |
Data Modeling Patterns
Key-Value Databases (Redis, DynamoDB)
Redis Data Structures
| Structure | Use Case | Example |
|---|---|---|
| String | Cache, counters | SET user:123 "Alice" |
| Hash | Object storage | HSET user:123 name "Alice" age 30 |
| List | Message queues | LPUSH queue "msg1" "msg2" |
| Set | Tags, unique items | SADD tags "python" "system-design" |
| Sorted Set | Leaderboards | ZADD leaderboard 100 "player1" |
| Stream | Event sourcing | XADD events * type "click" page "/home" |
DynamoDB Partitioning
Column-Family Databases (Cassandra)
Cassandra Data Model
Cassandra Query Patterns
| Pattern | Description | Example |
|---|---|---|
| Partition lookup | Get all data for a partition key | WHERE user_id = 'abc' |
| Range within partition | Query by clustering key | WHERE user_id = 'abc' AND timestamp > '2024-01-01' |
| Time-series | Latest events for a user | ORDER BY timestamp DESC LIMIT 10 |
Graph Databases (Neo4j)
Graph Query Patterns
| Query Type | Description | Example |
|---|---|---|
| Path finding | Shortest path between nodes | Find 3-degree connections |
| Pattern matching | Find specific subgraphs | Users who bought X and Y |
| Centrality | Most connected nodes | Influencer detection |
| Community detection | Cluster related nodes | Social group identification |
NoSQL Comparison Matrix
| Criteria | Document | Key-Value | Column-Family | Graph |
|---|---|---|---|---|
| Data model | JSON docs | KeyβValue | Wide columns | Nodes + edges |
| Query flexibility | High | Low (key only) | Moderate | High (traversals) |
| Write throughput | Good | Excellent | Excellent | Moderate |
| Read throughput | Good | Excellent | Good | Depends on query |
| Horizontal scaling | Good | Excellent | Excellent | Hard |
| Consistency | Configurable | Configurable | Tunable | Strong |
| Best use case | Content management | Caching, sessions | Time-series, IoT | Social networks |
Practice Exercises
-
Data Modeling: Design the MongoDB schema for a blogging platform with users, posts, comments, and tags. Decide which fields to embed vs reference. Justify your choices.
-
Key-Value Design: Using Redis, design a rate limiter that allows 100 requests per minute per user. What data structures would you use? How do you handle expiration?
-
Column-Family Design: Design the Cassandra table schema for a time-series IoT sensor data system. What is the partition key? What is the clustering key?
-
Graph Query: Given a social network graph, write the Cypher query to find "friends of friends who live in the same city and share at least 3 interests."
What to Learn Next
-> SQL Deep Dive PostgreSQL, MySQL, indexing strategies, and query optimization.
-> MongoDB Deep Dive Advanced MongoDB features, aggregation pipeline, and sharding.
-> Redis Deep Dive Redis data structures, persistence, clustering, and use cases.
-> Cassandra Deep Dive Cassandra architecture, data modeling, and operational patterns.
-> Choosing the Right Database Systematic framework for database selection.
-> DynamoDB Deep Dive DynamoDB internals, partitioning, and global tables.