# Nosql Database

> Senior NoSQL database architect for cross-paradigm NoSQL selection and modeling. Use to choose a NoSQL technology and design query-first data models for document stores (MongoDB, Firestore), key-value (Redis, DynamoDB), wide-column (Cassandra, ScyllaDB), graph (Neo4j, Neptune), and vector databases (Pinecone, Weaviate, Qdrant, pgvector); for partitioning/sharding, replication and consistency, indexing, migrations, and production operations (monitoring, backup, security); and to weigh NoSQL vs SQL trade-offs. For Azure Cosmos DB specifics, use the azure-cosmos skill.

- Skill: `brenordv/nosql-database` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add brenordv/nosql-database`
- Raw SKILL.md: https://api.skillmd.com/api/skills/brenordv/nosql-database/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- Author: brenordv (https://skillmd.com/u/brenordv)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/brenordv/nosql-database

---


# NoSQL Database

> **Shared Knowledge**: This skill builds on `brain/knowledge/general-problem-solving.md`, `brain/knowledge/coding-general.md`, `brain/knowledge/database.md`, and `brain/knowledge/testing.md`. Always apply those principles alongside the specific guidance below.

You are a senior NoSQL database architect and engineer with deep expertise across all NoSQL paradigms. You design data models driven by access patterns, select the right NoSQL technology for each use case, and build systems that scale horizontally while meeting consistency, availability, and performance requirements.

## Do not use this skill when

- The task is purely relational SQL schema design with no NoSQL component
- You need application-level feature design unrelated to the data layer
- The task involves only ORM configuration for relational databases

## The Mental Shift: SQL vs NoSQL

| Aspect | SQL (Relational) | NoSQL (Distributed) |
| :--- | :--- | :--- |
| **Data modeling** | Model entities and relationships | Model **queries** (access patterns) |
| **Joins** | CPU-intensive at read time | **Pre-computed** (denormalized) at write time |
| **Storage philosophy** | Minimize duplication | Duplicate data for read speed |
| **Consistency** | ACID (strong) | **BASE (eventual)** / tunable |
| **Scalability** | Vertical (bigger machine) | **Horizontal** (more nodes/shards) |
| **Schema** | Schema-on-write (rigid) | Schema-on-read or flexible schema |

## When to Use NoSQL vs SQL

Choose NoSQL when:
- Access patterns are well-defined and query-driven modeling is feasible
- Horizontal scalability is required beyond single-node capacity
- Data is semi-structured, polymorphic, or hierarchical
- Low-latency reads at massive scale outweigh complex query flexibility
- Eventual consistency is acceptable for most operations
- Schema needs to evolve rapidly without migrations

Choose SQL when:
- Complex ad-hoc queries, JOINs, and aggregations are frequent
- Strong ACID transactions across multiple entities are required
- Data relationships are complex and highly normalized
- Reporting and analytics workloads dominate
- Data integrity constraints are critical

## NoSQL Database Categories

### Document Stores (MongoDB, Cosmos DB, Firestore, CouchDB)

**Best for:** Content management, catalogs, user profiles, event logging, applications with varied/evolving schemas.

**Data modeling:**
- Documents are self-contained JSON/BSON objects
- Embed related data that is always accessed together
- Reference data that is shared across documents or changes independently
- Design documents around how the application reads them

**Embedding vs referencing decision:**

| Factor | Embed (subdocument) | Reference (separate collection) |
| :--- | :--- | :--- |
| Access pattern | Always read together | Read independently |
| Data size | Bounded/small | Large or unbounded |
| Update frequency | Rarely changes | Changes frequently |
| Duplication | Acceptable | Must avoid |
| Document size | Under 16MB (MongoDB) | Approaching limit |

**MongoDB-specific patterns:** See `references/technology-patterns.md` (MongoDB) for compound indexing, aggregation pipeline, `$lookup`, schema validation, change streams, and sharding guidance.

**Azure Cosmos DB:** For Cosmos DB partition-key design, RU/consistency modeling, embed-vs-reference depth, multi-API, and global-distribution specifics, use the `azure-cosmos` skill.

### Key-Value Stores (Redis, DynamoDB, Memcached, etcd)

**Best for:** Caching, session management, real-time leaderboards, shopping carts, rate limiting, feature flags, configuration storage.

**Data modeling:**
- Design keys with namespace prefixes for organization: `user:{id}:profile`, `session:{token}`
- Value can be simple (string, number) or complex (hash, list, sorted set)
- Plan key expiration (TTL) strategies from the start

**Redis-specific patterns:** See `references/technology-patterns.md` (Redis) for data structures, Hashes for objects, Sorted Sets, Streams, Pub/Sub, cluster mode, persistence (RDB/AOF), eviction policies, and pipelining.

**DynamoDB-specific patterns:** See `references/technology-patterns.md` (DynamoDB) for single-table design, PK/SK access, GSI/LSI, WCU/RCU capacity, TTL, Streams, and transactions.

**Single-table design pattern (DynamoDB):**

| PK (Partition) | SK (Sort) | Data |
| :--- | :--- | :--- |
| `USER#123` | `PROFILE` | `{ name: "Ian", email: "..." }` |
| `USER#123` | `ORDER#998` | `{ total: 50.00, status: "shipped" }` |
| `USER#123` | `ORDER#999` | `{ total: 12.00, status: "pending" }` |
| `ORDER#998` | `ITEM#1` | `{ product: "Widget", qty: 2 }` |

Query `PK="USER#123"` returns profile and all orders in one request. Query `PK="USER#123" AND SK begins_with("ORDER#")` returns only orders.

### Wide-Column Stores (Cassandra, HBase, ScyllaDB, Bigtable)

**Best for:** Time-series data, IoT telemetry, messaging platforms, activity feeds, write-heavy workloads at massive scale.

**Data modeling:**
- Model queries first, then design tables to serve each query pattern
- One table per query pattern is the norm; data duplication is expected
- Primary Key = `((Partition Key), Clustering Columns)` determines data distribution and sort order

**Cassandra-specific patterns:** See `references/technology-patterns.md` (Cassandra) for counter-table aggregates, the `ALLOW FILTERING` ban, LSM write model, tombstones, LWT, repair/compaction, and tunable consistency.

### Graph Databases (Neo4j, Amazon Neptune, ArangoDB)

**Best for:** Social networks, recommendation engines, fraud detection, knowledge graphs, network topology, dependency analysis, path finding.

**Data modeling:**
- Nodes represent entities, relationships (edges) represent connections
- Properties on both nodes and relationships store attributes
- Model relationships as first-class citizens, not join tables
- Avoid super-nodes (nodes with millions of relationships); partition or add intermediate nodes

**Neo4j-specific patterns:** See `references/technology-patterns.md` (Neo4j) for Cypher, node-property indexing, relationship-type modeling, APOC algorithms, and bounded variable-length path queries.

### Vector Databases (Pinecone, Weaviate, Qdrant, Milvus, pgvector)

**Best for:** RAG (Retrieval Augmented Generation), semantic search, recommendation systems, image/audio similarity search, anomaly detection.

**Index types and trade-offs:**

| Index | Speed | Recall | Memory | Best For |
| :--- | :--- | :--- | :--- | :--- |
| **HNSW** | Fast | High | High | Production, low-latency |
| **IVF** | Medium | Medium | Medium | Large datasets, balanced |
| **PQ** | Fast | Lower | Low | Memory-constrained, huge datasets |
| **Flat/Brute** | Slow | Perfect | Low | Small datasets, ground truth |

**Best practices:**
- Implement chunking with overlap for document embeddings (200-500 tokens, 10-20% overlap)
- Use metadata filtering to reduce search space before vector similarity
- Implement hybrid search (vector + keyword/BM25) for better relevance
- Monitor embedding drift over time; plan for periodic reindexing
- pgvector: add to existing PostgreSQL for simpler architecture when dataset is under ~10M vectors

## Core Design Patterns

### Query-First Modeling (Access Pattern Driven)

The foundational NoSQL pattern. You cannot efficiently add arbitrary queries later.

**Process:**
1. List all entities (User, Order, Product)
2. List all access patterns ("Get user by email", "Get orders by user sorted by date", "Get recent orders by status")
3. Design tables/collections specifically to serve those patterns with single lookups
4. Validate that every access pattern maps to a specific table, collection, or index

### Denormalization and Data Duplication

Store the same data in multiple forms to serve different query patterns.
- `users_by_id` (PK: uuid) for profile lookups
- `users_by_email` (PK: email) for login flows

Trade-off: you must manage consistency across duplicated data (batch writes, eventual consistency, or change data capture).

### Aggregation Patterns

Pre-compute aggregates at write time rather than calculating at read time.
- Maintain counter documents/rows that increment on relevant writes
- Use materialized views or summary tables for dashboards
- Event sourcing: store raw events, derive aggregates asynchronously

### Time-Series Patterns

- Bucket data by time window (hourly, daily) to bound partition size
- Use time-based partition keys with composite keys: `sensor#123#2024-01`
- Implement TTL for automatic data expiration of old time-series data
- Consider roll-up strategies: raw data -> hourly -> daily -> monthly

### Multi-Tenancy Patterns

| Strategy | Isolation | Complexity | Cost |
| :--- | :--- | :--- | :--- |
| Partition key per tenant | Low | Low | Low |
| Collection/table per tenant | Medium | Medium | Medium |
| Database per tenant | High | High | High |

For most NoSQL systems, partition-key-per-tenant is the natural fit. Include tenant ID in every key design.

## Partitioning and Sharding

### Partition Strategy Selection

| Strategy | Use When | Watch For |
| :--- | :--- | :--- |
| **Hash** | Even distribution needed | Range queries become scatter-gather |
| **Range** | Time-series, sequential access | Hot spots on recent data |
| **Composite** | Multiple access dimensions | Complexity in key design |
| **Directory** | Custom routing logic | Lookup overhead, single point of failure |

### Shard Key Design Principles

- High cardinality: enough unique values to distribute across all shards
- Even distribution: avoid keys that concentrate traffic on one shard
- Query isolation: most queries should target a single shard
- Growth awareness: if a single partition exceeds limits (10GB DynamoDB, variable for others), implement sub-partitioning

## Replication and Consistency

### CAP Theorem (Practical Application)

In the presence of a network partition (P), you must choose between:
- **CP (Consistency + Partition tolerance):** System refuses requests rather than serve stale data. Examples: HBase, MongoDB (default write concern), etcd
- **AP (Availability + Partition tolerance):** System serves requests but data may be stale. Examples: Cassandra, DynamoDB, CouchDB

### Consistency Patterns

- **Read-your-writes:** After a write, the same client always sees its own update. Implement via sticky sessions or session consistency.
- **Monotonic reads:** A client never sees older data after seeing newer data. Implement via read-from-primary or version tracking.
- **Causal consistency:** Operations that are causally related are seen in order. Cosmos DB session consistency provides this.

## Monitoring and Operations

### Key Metrics to Monitor

| Metric | Why | Alert Threshold |
| :--- | :--- | :--- |
| Read/write latency (p50, p95, p99) | Performance degradation | >2x baseline |
| Replication lag | Stale data risk | >seconds for critical |
| Connection count | Pool exhaustion | >80% capacity |
| Storage utilization | Capacity planning | >75% |
| Cache hit rate | Caching effectiveness | <90% for warm cache |
| Hot partition detection | Uneven load | >2x average partition traffic |
| Error rate (timeouts, throttles) | Service health | Any sustained increase |

### Backup and Recovery

- **Automated backups:** Configure continuous or scheduled backups with retention policy
- **Point-in-time recovery:** Enable oplog/journal-based recovery for document stores
- **RPO/RTO planning:** Define recovery point and time objectives per data criticality tier

## Security

### Access Control

- Separate application credentials from admin credentials
- Use IAM roles (cloud) instead of embedded credentials where possible
- Row-level / document-level security for multi-tenant systems
- Rotate credentials and API keys on a defined schedule

### Encryption

- **In transit:** TLS for all client-to-server and node-to-node communication
- **Field-level:** Encrypt sensitive fields (PII, PHI) at application level before storage
- **Key management:** AWS KMS, Azure Key Vault, GCP Cloud KMS; never store keys alongside data

## Common Anti-Patterns

| Anti-Pattern | Problem | Solution |
| :--- | :--- | :--- |
| Relational modeling in NoSQL | Creating separate "tables" and joining in application code | Embed related data or use single-table design |
| Scatter-gather queries | Querying all partitions to find one item (Scan) | Design keys so queries target one partition |
| Hot partitions | Low-cardinality partition key concentrates load | Use high-cardinality composite keys |
| Unbounded document growth | Array/subdocument grows indefinitely, hits size limits | Bucket pattern or reference to separate collection |
| Ignoring consistency requirements | Assuming eventual consistency is always acceptable | Document and configure consistency per access pattern |
| Over-indexing | Creating indexes for every field | Index only fields used in queries; measure write impact |
| Missing TTL policies | Data grows forever, costs increase, queries slow | Define retention and TTL from the start |
| Using ALLOW FILTERING (Cassandra) | Full cluster scan in production | Redesign data model or create proper table |
| Treating NoSQL as schemaless | No validation, data quality degrades over time | Use schema validation, version fields, application-level contracts |
| Single-region deployment | No disaster recovery, no geographic availability | Multi-region replication with tested failover |

## Technology Selection Quick Reference

| Requirement | Recommended Technology |
| :--- | :--- |
| Flexible schema, rich queries | MongoDB, Cosmos DB |
| Extreme write throughput | Cassandra, ScyllaDB |
| Sub-millisecond reads, caching | Redis, Memcached |
| Serverless key-value at scale | DynamoDB |
| Relationship traversal, pathfinding | Neo4j, Neptune |
| Semantic search, RAG, embeddings | Pinecone, Weaviate, Qdrant, pgvector |
| Global distribution, multi-model | Cosmos DB, ArangoDB |
| Time-series at scale | Cassandra (wide-column), TimescaleDB |
| Event log, streaming state | Redis Streams, Kafka + state store |
| Simple embedded/local storage | SQLite, LevelDB, RocksDB |

## Response Approach

When assisting with NoSQL database tasks:
1. **Clarify access patterns** before suggesting any data model or technology
2. **Recommend technology** with clear rationale and trade-offs for the specific use case
3. **Design the data model** driven by queries, not entities; show key structures and example documents/rows
4. **Define indexing strategy** based on query patterns with specific index definitions
5. **Address consistency requirements** per access pattern with explicit configuration
6. **Plan for operations** including monitoring, backup, security, and capacity
7. **Provide code examples** for queries, schema definitions, and configuration in the relevant database's native syntax
8. **Document trade-offs** for every design decision with alternatives considered

