ProgramGuru

Relational vs NoSQL Databases: A Beginner’s Guide

Keep the information your application needs

One order, two ways to organise it

Related tables

Customers
IDName
C7Maya
Orders
IDCustomerTotal
O42C710

Customer ID connects the records.

Order document

orderId: O42customerId: C7total: 10
itemsTea × 1

Useful order fields stay together. Decide which values to repeat and how to update them.

Related tables connect customers and orders through an identifier. A document can keep one order and its useful fields together. The application’s access patterns decide what is convenient.

A shopping application must remember customers, products and orders after a server restarts. A database stores that information and provides ways to read and change it. The right choice depends on the questions the application asks and the promises each write must keep.

Relational databases organise information into tables with relationships. NoSQL is an umbrella term for several other models, including documents and key-value records. It is not one product, and it does not mean that every such database has the same guarantees.

What you’ll learn

  • Model orders using related tables or a document.
  • Explain how lookup paths and transactions influence a database choice.
  • Compare real products using access patterns, correctness, operating effort and measurements.

Tables, documents and the questions you ask

A table stores rows with named columns. A customer row may have an ID and name; an order row may store that customer’s ID. A join combines related rows when you need their information together. This can keep one customer address in one place rather than repeating it in every order.

A document stores a group of named fields, often with nested items. Keeping an order and its line items together can make reading that order convenient. Repeated data may need extra updates when it changes. Flexible documents still need validation; the absence of one fixed table layout does not remove the need for a clear data model.

List the application’s access patterns: find an order by ID, list one customer’s orders, and total sales by date. These concrete reads and writes are more useful starting points than choosing a database because it is fashionable.

Find one record: scan or use a lookup directory

Look up a stored order

Eight sample orders in browser memory
O1Total 10
O2Total 12
O3Total 8
O4Total 25
O5Total 6
O6Total 18
O7Total 14
O8Total 9

Choose an order. A scan visits records in order; the directory maps an ID to a record.

Highlighted records show what the scan inspected. The directory is a teaching map, not a model of a particular database’s index internals.

Choose an order ID and compare a sequential scan with a lookup directory. The records stay visible and the result explains the work performed.

The browser example contains eight orders. A scan checks records one at a time until it finds the requested ID. A lookup directory points to the record for that ID. Send a query using each approach and compare the highlighted records and work counted.

This is a small in-memory teaching model. A real database index has its own structure and cost; one directory lookup here does not mean every indexed database query takes one operation or constant time. Real engines may choose a scan when it is cheaper.

Indexes speed some reads and add work to writes

An index is an additional structure that helps locate records for a particular query. An index on customer ID may help list that customer’s orders; it does not automatically make every report faster. It uses storage and must be maintained when indexed values change.

CREATE INDEX orders_customer_id_idx
ON orders (customer_id);

This PostgreSQL example creates an index for a column used in customer lookups. Use EXPLAIN to inspect how an actual query is planned, and measure it with representative data. Read the index documentation before creating an index on a busy live table.

Measure read latency, write latency, data size and storage growth. A read improvement can carry a write and storage cost, so keep only indexes that serve useful access patterns.

Keep a multi-step change consistent

One purchase, a consistent result

PurchaseOne intended database change
Database transaction
Reserve stockSave order

Commit both, or roll back both

Consistent database resultConcurrency rules still protect the last item
Reserving stock and saving an order belong to one coordinated database change. A failed transaction must not leave only half of that database change committed.

Buying the last available ticket may require reserving stock and recording the order together. A transaction groups changes so they commit together or are rolled back. Concurrent buyers also need an appropriate locking, conditional-write or isolation design so both cannot claim the same last ticket.

SQL versus NoSQL alone does not settle this requirement. PostgreSQL supports transactions; DynamoDB also provides transactions, with its own scope and limits. Check the product’s actual guarantees and design the write correctly. A transaction cannot make an external payment and an unrelated database write magically one atomic action.

Durability means accepted data can survive the failures covered by the database’s configured guarantees. Replication keeps additional copies; backups and tested restore procedures address other recovery needs. Neither a product label nor one healthy replica replaces a recovery plan.

Real databases and their operating choices

Representative products and licensing sources checked 6 October 2026. No product has one universal reads/s or writes/s capacity independent of data, indexes, hardware and consistency settings.

  • PostgreSQL is free, open-source relational database software. It is a useful candidate for relationships, transactions and varied queries; self-hosting leaves maintenance, backups and recovery with your team.
  • MongoDB stores documents. Its server licence is SSPL; do not treat all database licences as interchangeable. Atlas offers managed hosting with service-specific prices and responsibilities.
  • Amazon DynamoDB is a managed key-value and document database. Its key design and access patterns matter; it also supports transactions. Evaluate request sizes, consistency, partition behaviour and billing.

Free software and paid hosting are separate decisions. Managed PostgreSQL is another possible operating model; managed hosting does not change tables into a different data model. Compare backup support, upgrade work, failure recovery and cost as well as query speed.

Choose around the reads and writes

Orders, customers and changing reports

Start by evaluating PostgreSQL when relationships, coordinated changes and varied queries are central. Compare it with alternatives using the actual workload. The trade-off includes schema changes, index maintenance and the team’s operating work.

Documents usually read as a whole

Evaluate MongoDB when an order-like document closely matches how the application reads and updates data. Consider document growth, repeated fields and cross-document changes. Choosing documents should simplify the access pattern, rather than hide relationships the application still needs.

Known key-based requests in AWS

Evaluate DynamoDB when the important requests are known and fit a deliberate key design. Managed operation can help a small team, while unfamiliar queries, uneven hot keys and usage costs need planning. “NoSQL scales” is not a substitute for designing the keys.

For each candidate, measure representative reads and writes, errors and latency during a failure. Check growth and restore requirements before making the choice.

Make concurrent transactions obey the business rule

See the responsibility

Concurrent callersTry to reserve the same seat
Storage ruleConstraint, conditional write or isolation
Committed resultPreserves the booking invariant
Conflict handlingRetries or returns a truthful rejection
Concurrent callers: Try to reserve the same seat → Storage rule: Constraint, conditional write or isolation → Committed result: Preserves the booking invariant → Conflict handling: Retries or returns a truthful rejection

ACID describes atomicity, consistency under defined rules, isolation and durability. Atomicity means a transaction’s changes commit together or are rolled back. It does not make a payment API call part of the database transaction. Durability depends on the actual storage and acknowledgement configuration.

Isolation determines which concurrent changes a transaction can observe and which interference is prevented. PostgreSQL Read Committed uses a new snapshot for each command; stronger isolation levels have different rules and may require retries. Product names such as SQL or NoSQL alone do not identify the isolation contract.

For the last seat in a booking, “read available, then write reserved” is unsafe if two callers can both pass the check. Use a suitable constraint, conditional write, lock or isolation strategy, and handle conflicts deliberately. Read PostgreSQL isolation documentation and continue with cross-service transactions.

Recognise more than tables and documents

Match a model to its question

Key lookupRetrieve one value
Partitioned queryRead a planned column range
Relationship traversalFollow connected entities
RequirementsVerify guarantees and cost
Key lookup: Retrieve one value → Partitioned query: Read a planned column range → Relationship traversal: Follow connected entities → Requirements: Verify guarantees and cost

A key-value store retrieves a value by a key and can fit simple access patterns. A wide-column database organizes data around partition keys and columns; designs such as Cassandra require deliberate query and partition planning. A graph database represents entities and relationships and can fit relationship traversal, such as finding connected accounts.

Relational tables can also model graphs and documents; the model alone does not decide the product. Compare the operations you need, transaction boundaries, indexing, concurrency and operating skills. Redis, Cassandra and Neo4j are examples in these families, not interchangeable recommendations.

Normalization keeps facts under clear ownership and avoids redundant copies where appropriate. Denormalization duplicates or precomputes selected facts for reads and needs an update rule. Read the indexing lesson before trading consistency work for a faster query.

Choose from the access pattern

Customers, orders and products have relationships, and reporting questions often change. Which is a reasonable starting candidate?
Choose one answer. You can try again.

Answer and explanation

Evaluate a relational database such as PostgreSQL using the actual queries.

Relationships and varied queries make a relational model a useful starting point; measurement still decides the final fit.

Account for the index trade-off

What does a useful index usually add alongside faster matching reads?
Choose one answer. You can try again.

Answer and explanation

Storage use and maintenance work when indexed data changes.

The database must store and update the additional lookup structure.

Keep the promise of the write

What should you verify when a purchase needs stock reservation and an order record together?
Choose one answer. You can try again.

Answer and explanation

The actual transaction and concurrency guarantees, plus the write design.

Correctness depends on product guarantees and how the application coordinates concurrent changes.

Check the added design rule

Two callers can both read “seat free” before either writes. What should protect the booking rule?
Choose one answer. You can try again.

Answer and explanation

A suitable storage constraint, conditional operation or concurrency strategy.

The implemented write boundary must prevent two successful reservations.

Match storage to questions and promises

Start from the information you store, the reads you need and the guarantees each write must keep. Tables and documents organise information differently; actual product features matter more than broad labels.

Indexes trade storage and write work for useful lookups. Transactions and concurrency rules protect coordinated changes. Measure realistic workloads, plan growth, and practise restoring data.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Database Replication and Read Guarantees →