Make the right trade-off / CHAPTER 40
SQL versus NoSQL data stores
Choose a data model and guarantees from access patterns rather than database stereotypes.
Builds on Data partitioning, Functional and non-functional requirements.
- Compare relational, document, key-value, and wide-column models.
- Identify transaction and query requirements.
- Explain partitioning and indexing costs across models.
01A family name is not a guarantee
A shop stores products, orders and reviews. Choosing a database begins with the operations those records must support.
- Relational tables can store an
Orderrow and relatedOrderLinerows. SQL is a query language commonly used to retrieve and combine such records. - A document can store one product's display fields together as one object.
- A key-value store retrieves a value by a known key, such as a session ID.
- Wide-column models commonly group rows under a partition key and order them by additional keys, supporting planned range queries.
- Graph models store relationships that queries follow between records.
NoSQL is an umbrella label for several of these models, not one alternative with one guarantee. Relational systems can distribute data, and some NoSQL systems support multi-record transactions and current reads. Check the concrete operation, supported records and failure behavior instead of assuming SQL means “cannot scale” or NoSQL means “no transactions.”
02Start with queries and invariants
Write the shop's requests and correctness rules first:
- Product page: load one product and a bounded page of reviews. Do not assume every review can be embedded forever in one growing product record.
- Purchase: accept an order only if the saved available quantity covers the requested quantity and that quantity is reserved for this order. Preserve one logical purchase across request retries.
- Search: find matching products, then load current records before making freshness-sensitive purchase decisions.
A rule that must always hold, such as no negative stock after accepted allocation, is an invariant. An access pattern describes the keys, filters and ordering a request needs.
| Candidate | Natural operation | Extra work to evaluate |
|---|---|---|
| Relational | Join an order to its lines and customer. | Indexes, joins, concurrent transactions and partition placement affect cost. |
| Document | Read one product's grouped display fields. | A growing review list and updates across separate documents need care. |
| Key-value | Fetch a session or product by ID. | Title search requires another index or an expensive scan. |
| Wide-column | Read a planned ordered range within a chosen partition. | Key choice and a very busy partition can limit the query. |
| Graph | Follow defined relationships. | Depth and number of visited neighbors determine work. |
A join combines related records while reading. A secondary index maps a searchable field to matching records; it speeds selected reads but must be maintained on relevant writes.
For transactions, check which records can commit together and how concurrent updates are isolated. ACID transactions group changes with atomicity, consistency, isolation and durability guarantees; the later ACID chapter examines them. Neither a SQL interface nor a NoSQL label specifies the implementation's transaction participants by itself.
03Worked example: orders and browsing
Follow purchase request K8 for product 42:
- Browsing returns display fields. A document or relational query can provide title, attributes and image references. Its cached stock hint is not a reservation.
- The buyer submits account, product, quantity and
K8. The service verifies identity and the requested operation. - The purchase service checks stock and records the purchase. In a supported transaction, check that available stock is at least the requested quantity, subtract it, and save the order, lines and
K8's original request/result together. The stock check and subtraction must be one guarded change, so another buyer cannot spend the same units between them. If the required changes cannot share a transaction, explain an explicit reservation workflow rather than pretending independent writes are atomic. - A matching retry looks up
K8. It returns the saved order. A different quantity under that key conflicts instead of silently creating another purchase. - Search receives product changes later. Even if it still displays one available item, checkout must use the current stock-allocation rule.
A relational schema might include Order(orderId,accountId,requestKey), OrderLine(orderId,productId,quantity) and Inventory(productId,available). A transactional document/key-value system can also work if its supported operations cover the same records and conditions. Putting display fields together does not extend a transaction to stock stored elsewhere.
Now let one product accumulate millions of reviews. Loading or updating one ever-growing object becomes expensive and concentrates traffic. Store review pages separately when needed. The correct choice follows the actual read and update, not a family-wide claim that one model makes all queries cheap.
SQL versus NoSQL data stores — decision flow
Follow the numbered arrows
- Shop application → Relational optiontest the required reads and writes
- Shop application → Document optiontest grouped reads and separate updates
- Shop application → Key-value optiontest keyed reads and conditional changes
- Relational option → Compare measured resultscheck actual guarantees and cost
- Document option → Compare measured resultscheck actual guarantees and cost
- Key-value option → Compare measured resultscheck actual guarantees and cost
Trace the diagram in words
- Write the product queries and stock/order rules before choosing a model.
- Check which records each candidate can query efficiently and commit together.
- Rebuild search from saved product records; checkout still verifies and allocates current stock.
04Choose the smallest defensible data architecture
Start with the smallest data architecture that meets the actual requirements:
- Use one capable familiar store when its queries, guarantees and growth limits fit. Do not add a second store merely because a different model has a favorable benchmark.
- Add specialized search only for a concrete need. It also adds delayed updates, permission checks, deletion handling, backup and repair work.
- For every copied view, identify its original records. State acceptable delay and how to rebuild the copy after loss.
- Test realistic popularity. One viral product can overload a partition in either family. Distributing unrelated records does not divide that one operation.
- Compare equal correctness. A faster option that allows an oversell does not meet the same purchase requirement.
Show record keys and one read/write trace before naming database products. Verify indexed queries, payload sizes, concurrent updates and cross-partition costs against the chosen implementation.
DRAW. CALCULATE. EXPLAIN.
Choose models from operations
20 minutes · guided practice
A shop needs atomic stock reservation/order creation, product reads by ID, paginated reviews and full-text search. Propose a minimal initial model, identify a derived view, and explain a stale search result during checkout.
Compare the worked solution
- Model orders, lines, inventory and bounded review pages with keys for the actual queries. A capable relational store is a reasonable minimal start.
- Save required stock reservation, order and retry result together in a supported transaction. A document/key-value alternative must demonstrate the same covered records and conditions.
- Use sufficient built-in text search initially; add a separate search engine only for a needed capability or measured benefit.
- Search results may lag. Checkout checks current stock rather than treating the displayed count as permission to buy.
- Record how search updates, deletes and rebuilds from the original product records.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01What selects a data model?
- List required reads, writes, record shapes and correctness rules.
- Compare actual query and transaction capabilities.
- SQL/NoSQL labels alone do not establish which records commit together or how efficiently required queries run.
CARD 02What does a read optimization cost?
- An index or copied view can save selected reads.
- Writes must maintain it.
- Count extra writes, storage, popular keys and repair before deciding the faster read is worth it.
CARD 03What must multiple stores define?
- Identify which records accept original changes.
- State allowed copy delay and deletion handling.
- Explain rebuilding.
Checkout still checks actual stock; an old search result is not permission to allocate it.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.