๐ง NoSQL Databases
The Senior Mindset: NoSQL is not a replacement for SQL; it is a specialized tool. Choosing NoSQL is a trade-off: you often sacrifice ACID compliance and complex joins in exchange for horizontal scalability, high availability, and schema flexibility.
๐ Document Stores (e.g., MongoDB, CouchDB)
Section titled โ๐ Document Stores (e.g., MongoDB, CouchDB)โData is stored in JSON-like documents.
- Best for: Content management, user profiles, and catalogs where the schema evolves rapidly.
- Key Concept: Data Locality. You store related data together in one document to avoid joins.
- Trade-off: Harder to perform cross-document analytics.
๐ Key-Value Stores (e.g., Redis, DynamoDB, Riak)
Section titled โ๐ Key-Value Stores (e.g., Redis, DynamoDB, Riak)โThe simplest form of NoSQL. Data is a blind โblobโ indexed by a unique key.
- Best for: Caching, session management, and real-time leaderboards.
- Key Concept: O(1) Access. Retrieval is incredibly fast because there is no query parsing logic.
- Trade-off: You cannot query by the โvalueโ efficiently; you must know the โkey.โ
๐๏ธ Wide-Column Stores (e.g., Cassandra, ScyllaDB, HBase)
Section titled โ๐๏ธ Wide-Column Stores (e.g., Cassandra, ScyllaDB, HBase)โData is stored in column families rather than rows.
- Best for: Massive datasets (petabytes), time-series data, and high-write throughput (logging, IoT).
- Key Concept: Partition Keys. Data is distributed across nodes based on a partition key, allowing for linear horizontal scaling.
- Trade-off: Requires a deep understanding of your query patterns before designing the schema (Query-First Design).
๐ธ๏ธ Graph Databases (e.g., Neo4j, Amazon Neptune)
Section titled โ๐ธ๏ธ Graph Databases (e.g., Neo4j, Amazon Neptune)โFocuses on the relationships between data points (Nodes and Edges).
- Best for: Social networks, recommendation engines, and fraud detection.
- Key Concept: Index-free Adjacency. Navigating a relationship doesnโt require an index lookup; you just follow a pointer to the next node.
- Trade-off: Not suited for bulk data processing or simple CRUD operations.
โ๏ธ The NoSQL Decision Matrix (The Senior Perspective)
Section titled โโ๏ธ The NoSQL Decision Matrix (The Senior Perspective)โ| Feature | SQL (Relational) | NoSQL (Non-Relational) |
|---|---|---|
| Scaling | Vertical (Bigger Server) | Horizontal (More Servers) |
| Schema | Rigid / Fixed | Flexible / Dynamic |
| Joins | Native / Complex | Application-side or Denormalized |
| Transactions | Strong ACID | Usually Base (Basically Available, Soft state, Eventual consistency) |
When to stay with SQL?
Section titled โWhen to stay with SQL?โIf your data is highly relational and requires strict consistency (e.g., Ledger/Banking), SQL is almost always the safer bet. Donโt use NoSQL just to avoid writing a migration script.
When to go NoSQL?
Section titled โWhen to go NoSQL?โ- Scale: When your write volume exceeds what a single primary SQL node can handle.
- Unstructured Data: When you donโt know the schema in advance.
- Specific Access Patterns: When your queries perfectly map to a Graph or a Wide-Column structure.
๐ก Seniority Note: Beware of Eventual Consistency. In many NoSQL systems, a โWriteโ might take a few milliseconds to propagate to all nodes. If a user refreshes the page immediately, they might see old data. Designing the UX to handle this (or choosing โStrong Consistencyโ at the cost of latency) is a key architectural decision.
๐ Related Links
Section titled โ๐ Related Linksโ- [[Relational-Databases]]
- [[Arquitetura-Sistemas-Escalaveis]]
- [[Caching-Redis-CDN]]