Modeling NoSQL Data Structures
1. Understanding NoSQL Data Models
| Model | Example |
|---|---|
| Document | MongoDB, Couchbase, Firestore |
| Key-Value | Redis, DynamoDB, RocksDB |
| Column-Family (wide-column) | Cassandra, ScyllaDB, HBase, BigTable |
| Graph | Neo4j, Neptune, JanusGraph |
| Search | Elasticsearch, OpenSearch |
| Time-Series | InfluxDB, TimescaleDB |
2. Designing Schemaless Collections
| Principle | Detail |
|---|---|
| Schema-on-read | Validate at app or read time |
| Optional schema | $jsonSchema validator |
| Embed schema_version | Branch logic per version |
| Convention over enforcement | Naming + docs |
3. Modeling Embedded Documents
| When | Detail |
|---|---|
| 1:1, 1:few | Embed for read locality |
| Always read together | Single round-trip |
| Independent lifecycle | Reference instead |
| Doc size limit | MongoDB 16 MB |
4. Modeling Document References
| Pattern | Detail |
|---|---|
| Manual reference | Store ObjectId; app joins |
| DBRef | {$ref, $id, $db} — rarely used |
| $lookup | Server-side join in aggregation |
| Extended reference | Embed key fields + reference |
5. Implementing Denormalization
| Trade-off | Detail |
|---|---|
| Read perf | Pre-joined data, single fetch |
| Write cost | Update many copies on change |
| Eventual consistency | Use change streams to propagate |
| Idempotent updates | Safe to retry |
6. Designing for Access Patterns
Access-Pattern-First Modeling
- List all read & write patterns with frequency
- Identify primary access keys
- Design partition / collection layout
- Add secondary indexes / GSIs for alternates
- Validate with synthetic load
7. Modeling Many-to-Many Relationships
| Pattern | Detail |
|---|---|
| Two-way arrays | Author has books[]; book has authors[] |
| Junction collection | Like SQL bridge table |
| Adjacency list (DynamoDB) | Single-table with item types |
| Limit array growth | Subset pattern when large |
8. Handling Schema Migrations in NoSQL
| Strategy | Detail |
|---|---|
| Lazy migration | Upgrade doc on read; write back new shape |
| Background batch | Scan + transform |
| Dual-write | Write old + new fields, then switch |
| Schema version field | Branch code paths |
9. Modeling Aggregates
| Concept | Detail |
|---|---|
| Aggregate root | Top-level entity; transactional boundary |
| Embed within aggregate | Children loaded with parent |
| Reference across aggregates | By ID only; eventual consistency |
| Small aggregates | Reduce contention & doc size |
10. Choosing Document Granularity
| Question | Guidance |
|---|---|
| Always read together? | Combine into one doc |
| Independent updates? | Separate docs |
| Unbounded growth? | Split (subset/bucket) |
| Frequent partial reads? | Smaller docs or projections |