LearnastraTHE DESIGN COURSE

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.

Intermediate1h of deliberate study10 questions

Builds on Data partitioning, Functional and non-functional requirements.

By the end, you can
  • 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 Order row and related OrderLine rows. 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:

  1. 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.
  2. 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.
  3. 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:

  1. Browsing returns display fields. A document or relational query can provide title, attributes and image references. Its cached stock hint is not a reservation.
  2. The buyer submits account, product, quantity and K8. The service verifies identity and the requested operation.
  3. 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.
  4. A matching retry looks up K8. It returns the saved order. A different quantity under that key conflicts instead of silently creating another purchase.
  5. 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

SQL versus NoSQL data stores — decision flowWrite 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. Numbered arrows correspond to the connection key below. All connections are directional.Shop applicationProduct queries andstock/order rulesRelational optionTables, related rowsand constraintsDocument optionGroup product fieldsin one objectKey-value optionFind records by theirknown keysCompare measuredresultsRequired guarantees,query cost and growth123456SQL versus NoSQL data stores — decision flowWrite 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. Numbered arrows correspond to the connection key below. All connections are directional.Shop applicationProduct queries andstock/order rulesRelational optionTables, related rowsand constraintsDocument optionGroup product fieldsin one objectKey-value optionFind records by theirknown keysCompare measuredresultsRequired guarantees,query cost and growth123456SQL versus NoSQL data stores — decision flowWrite 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. Numbered arrows correspond to the connection key below. All connections are directional.Shop applicationProduct queries andstock/order rulesRelational optionTables, related rowsand constraintsDocument optionGroup product fieldsin one objectKey-value optionFind records by theirknown keysCompare measuredresultsRequired guarantees,query cost and growth123456

Follow the numbered arrows

  1. Shop application → Relational optiontest the required reads and writes
  2. Shop application → Document optiontest grouped reads and separate updates
  3. Shop application → Key-value optiontest keyed reads and conditional changes
  4. Relational option → Compare measured resultscheck actual guarantees and cost
  5. Document option → Compare measured resultscheck actual guarantees and cost
  6. Key-value option → Compare measured resultscheck actual guarantees and cost
Start with product reads and purchase rules. Compare actual transaction and query support; search copies cannot allocate current stock.
Trace the diagram in words
  1. Write the product queries and stock/order rules before choosing a model.
  2. Check which records each candidate can query efficiently and commit together.
  3. 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.

Your self-check
Compare the worked solution
  1. Model orders, lines, inventory and bounded review pages with keys for the actual queries. A capable relational store is a reasonable minimal start.
  2. 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.
  3. Use sufficient built-in text search initially; add a separate search engine only for a needed capability or measured benefit.
  4. Search results may lag. Checkout checks current stock rather than treating the displayed count as permission to buy.
  5. 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?
  1. List required reads, writes, record shapes and correctness rules.
  2. Compare actual query and transaction capabilities.
  3. SQL/NoSQL labels alone do not establish which records commit together or how efficiently required queries run.
CARD 02What does a read optimization cost?
  1. An index or copied view can save selected reads.
  2. Writes must maintain it.
  3. Count extra writes, storage, popular keys and repair before deciding the faster read is worth it.
CARD 03What must multiple stores define?
  1. Identify which records accept original changes.
  2. State allowed copy delay and deletion handling.
  3. Explain rebuilding.

Checkout still checks actual stock; an old search result is not permission to allocate it.

Practise this topic in the revision deck ↗

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.

1 / 10Which information should drive the shop’s first database comparison?
2 / 10A document contains product display fields but stock is elsewhere. What must purchase design still establish?
3 / 10Search says one item remains, but the inventory service’s current stock is zero. Which decides whether a purchase may proceed?
4 / 10What can a new secondary index add?
5 / 10Why might an ever-growing embedded review list become a poor fit?
6 / 10Can a relational database distribute data?
7 / 10Can a NoSQL system provide an ACID transaction?
8 / 10A key-value option makes ID reads easy but title search difficult. What should be compared?
9 / 10When should another specialized store be added?
10 / 10K8’s order committed but its response was lost. Which property matters on retry?
0 of 10 answered

CLOSE THE LOOP

Explain it without the page.

Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.

Go to the source