Skip to article content
FrontendXLesson 19 / 24

Data integrity across SQL and NoSQL

Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.

advanced · about 50 minutes · 2 exercises

  • Explain
  • Interview

What you will be able to do

#
  • Compare transactional guarantees of relational and document databases.
  • Use idempotency keys to make writes safe to retry.
  • Choose a store based on access patterns and consistency needs.

Overview

#

Relational databases give multi-row ACID transactions by default. MongoDB gives single-document atomicity and optional multi-document transactions. The choice is about access patterns and where invariants live.

Keeping invariants

If an invariant spans two documents or two tables, either use a transaction or redesign so the invariant lives in one place. For example, store the balance and its ledger entry together, or use a transaction that writes both.

Idempotency keys

A retried write must not create a duplicate. Store a client-supplied key with a unique constraint; on conflict, return the original result instead of writing again.

snippet.sql
CREATE TABLE payments (
  id BIGSERIAL PRIMARY KEY,
  idempotency_key TEXT UNIQUE NOT NULL,
  amount_cents INTEGER NOT NULL
);

Choosing a store

Use a relational database when relationships and multi-row invariants dominate. Use a document store when the aggregate is read and written as one unit and the shape varies. Many systems use both.

Common pitfalls

  • Assuming a document store gives the same cross-collection guarantees as a relational database.
  • Retrying a payment write with no idempotency key and charging twice.
  • Splitting an invariant across stores with no coordination.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Interview

    You must charge a customer exactly once even if the client retries. Describe the design.

    starter.text
    POST /payments with a client-generated key
    

    Reveal the reference answer
    reference.text
    The client sends an Idempotency-Key header. The server inserts a row with that key under a unique constraint in the same transaction as the charge. If the insert conflicts, the server returns the stored result for that key instead of charging again. The key and result are stored so a retry returns the original response.

    Authored expected output: A design using a unique idempotency key stored with the result, returning the original response on retry.

    A unique constraint turns a duplicate retry into a lookup rather than a second charge.

  2. Exercise 2Explain

    Give one access pattern that favors a document store and one that favors a relational database.

    starter.text
    // two contrasting access patterns
    

    Reveal the reference answer
    reference.text
    A product catalog where each product is read and written as a whole, with a flexible attribute set, favors a document store. A billing system with many related tables and multi-row invariants such as double-entry accounting favors a relational database with transactions.

    Authored expected output: One aggregate-oriented pattern and one relationship-heavy pattern with a justification.

    The deciding factor is whether the unit of access is a self-contained aggregate or a set of related rows.

Common misconceptions

#
  • NoSQL means no consistency: document stores offer configurable consistency and single-document atomicity.
  • A transaction fixes a bad model: transactions cannot repair an invariant split across stores with no shared transaction.

Follow-up practice

#
  • backend-databases-authentication

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.