Engineering Playbook
Databases

NoSQL Patterns

DynamoDB, Single Table Design, and Aggregation Pipelines.

NoSQL Patterns

NoSQL isn't just "SQL without schema." It's a fundamental shift in data modeling. In SQL, you model the data (3rd Normal Form). In NoSQL, you model the queries.

DynamoDB (Key-Value / Wide Column)

The Mental Shift

You cannot do joins. You cannot do flexible WHERE clauses. You must know exactly how you will access the data before you create the table.

Single Table Design

Instead of Users table and Orders table, you put everything in One Table.

  • Partition Key (PK): Generic (e.g., USER#123).
  • Sort Key (SK): Generic (e.g., PROFILE or ORDER#999).

The Magic:

  • Fetch User Profile: GET PK=USER#123 SK=PROFILE
  • Fetch User Orders: QUERY PK=USER#123 SK begins_with(ORDER#)

This allows you to fetch a User AND their Orders in a single request (pre-joined by how data is stored on disk).


MongoDB (Document)

Flexibility is the selling point. But schema design still matters.

Embedding vs. Referencing

  • Embedding: Store Addresses inside the User document.
    • Pros: One read to get everything. Atomic updates.
    • Cons: Document size limits (16MB). Can't query "Find all users in NY" easily if indexing is tricky.
  • Referencing: Store Addresses in separate collection with user_id.
    • Pros: unlimited growth.
    • Cons: Application-level joins (slower).

Aggregation Pipeline

Mongo's superpower. A Linux-pipe style processing engine. $match -> $group -> $project -> $sort. It allows you to do complex analytics (Group by Year, Sum Revenue) on the server side.