SkillSprout
Chapter 14 · Lesson 2
Beginner
3 min read

NoSQL and Choosing a Database

Overview

Relational databases are great, but they are not the best fit for everything. NoSQL databases trade some relational features for flexibility and easy scaling. This lesson shows the four families and how to pick.

Detailed Notes

What is NoSQL

NoSQL ("not only SQL") is a group of databases that do not use fixed tables and joins as their main model. Most have a flexible schema, are designed to spread data across many machines, and offer simpler queries in exchange for speed and scale.

The problem it solves

Relational databases shine at correctness, but some workloads strain them: huge write volumes, data whose shape varies from item to item, or simple lookups at enormous scale. NoSQL stores are built for these.

They often follow BASE instead of ACID: Basically Available (the system keeps answering), Soft state (data may be in flux), Eventually consistent (copies agree after a short while). In other words, they give up some strictness to stay fast and available. (How this trade-off works is covered later.)

How it works

The four main families:

  • Key-value: a unique key maps to a value. Like a giant dictionary. Very fast and simple. Use for caches, sessions, shopping carts. Examples: Redis, DynamoDB.
  • Document: a key maps to a self-describing document, usually JSON, and each document can have different fields. Use for catalogues and user profiles. Examples: MongoDB, Firestore.
  • Wide-column: rows with flexible groups of columns, built for huge write volume. Use for time-series and event logs. Examples: Cassandra, Bigtable.
  • Graph: nodes and the relationships between them. Use for social networks, recommendations and fraud detection. Examples: Neo4j, Amazon Neptune.

Choosing. Start from the access pattern, not the brand:

  • Need transactions, relationships and flexible queries? Relational.
  • Need simple lookups by key at massive scale? Key-value.
  • Data shape varies per item and is read as a whole? Document.
  • Heavy writes over time? Wide-column.
  • Questions about connections ("friends of friends")? Graph.

Large systems often use several databases at once, each for what it does best. This is called polyglot persistence. Keep one system of record (the main trusted copy, often relational); other stores such as caches or search indexes are derived from it.

A simple example

A relational table is a tidy filing cabinet with printed forms: every folder has the same fields. A document store is a set of free-form folders: each can hold different papers. A key-value store is a coat check: hand over a ticket, get your coat.

System design example

A shop keeps orders and payments in a relational database (system of record), the shopping cart and sessions in Redis (key-value), product pages as documents in a document store, and a recommendation engine in a graph database. Each store is chosen for its access pattern.

Summary

  • NoSQL means flexible schemas and easy scaling, with simpler queries.
  • Four families: key-value, document, wide-column, graph.
  • NoSQL often follows BASE (eventual consistency) instead of ACID.
  • Choose by access pattern and consistency needs.
  • Polyglot persistence uses several databases, with one system of record.

Architecture Diagram

Polyglot persistence with a system of record
Architecture
App Services

Data stores

Relational DBsystem of record
Key-valuecarts, sessions
Cache

Each store serves one access pattern.

  1. 1Identify the access pattern for each feature.
  2. 2Keep important, relational data in the system of record.
  3. 3Use key-value or document stores for simple, high-volume lookups.
  4. 4Treat caches and other copies as derived from the main data.

Common Interview Questions

Chapter quiz

Databases · 5 questions

  1. 1.Which ACID property guarantees all steps of a transaction happen or none do?

  2. 2.What links a row in one table to a row in another?

  3. 3.Which database type suits storing shopping carts looked up by a user ID at huge scale?

  4. 4.Which database family is best for 'friends of friends' questions?

  5. 5.What does BASE emphasise compared with ACID?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course