Week 9

Types of Databases

Databases: Comparison, Choosing & Polyglot

jsonb: Concepts & Setup

jsonb: querying updating

jsonb: Indexes, Tradeoffs & Exercise

Object Storage

Practice

Assignment

Backend Track

Comparison: Relational vs Document vs Key-Value

Relational (PostgreSQL) Document (MongoDB) Key-Value (Redis)
Data model Tables, rows, columns JSON-like documents Key → value pairs
Schema Strict, enforced Flexible, per-document None
Query language SQL MongoDB Query Language Simple commands (GET, SET…)
Relationships Foreign keys + joins Embedded or by reference Not supported
Speed Fast Fast Extremely fast (in-memory)
Persistence Always on disk Always on disk Optional
Transactions Full ACID Limited (single document) Limited
Scalability Vertical (primarily) Horizontal (sharding) Horizontal
Best for Structured, relational data Flexible / nested data Caching, sessions, speed
Typical use Orders, users, invoices Catalogs, CMS, profiles Cache, sessions, counters

Thinking About It Differently

Each database type optimises for a different thing:


Choosing the Right Database

There is no single "best" database. The right choice depends on your data shape and how you access it. These questions will guide you:

Decision Framework

Step 1 — How structured is your data?

Step 2 — Do you need to query across multiple related entities?

Step 3 — What are the speed requirements?

Step 4 — Does the data expire?

Step 5 — How large is the dataset?

Scenario Quick Reference

Scenario Best choice Why
User accounts and login credentials PostgreSQL Structured, needs integrity constraints
Order history and payments PostgreSQL Transactions, joins across tables
Product catalog (varying attributes per type) MongoDB Different fields per product type
Blog posts with embedded comments MongoDB Nested, self-contained documents
User session / login token Redis Fast lookup, must auto-expire
Cache for expensive DB query results Redis In-memory speed, TTL
Rate limiting (requests per minute) Redis Atomic counters, TTL
Real-time game leaderboard Redis Sorted sets, instant updates
Financial transactions PostgreSQL Full ACID guarantees required
Rapidly changing API response data MongoDB Schema can evolve without migration

Polyglot Persistence

What is Polyglot Persistence?

Polyglot persistence means deliberately using more than one type of database in a single application - choosing each one based on what it does best for that specific part of the system.

<aside> 💡

The word polyglot means "speaking multiple languages." Polyglot persistence means your system "speaks" multiple database languages, SQL for structured data, document queries for flexible data, Redis commands for speed.

</aside>

The core idea: no single database excels at all workloads. Rather than forcing all data into one database and accepting the compromises that come with it, you match each dataset to its natural home.


A Real-World Example

Think about what happens in those few seconds when you open Netflix:

All four happen simultaneously, seamlessly, to deliver your homepage. That is polyglot persistence in production.


An E-commerce System

Here is how a backend might distribute data across multiple databases:

image.png

Each database handles only what it is good at:

Data Database Reason
Users, orders, payments PostgreSQL Structured, needs transactions and joins
Product catalog MongoDB Different product types have different fields
User sessions Redis Fast lookup, auto-expires after inactivity
Recently viewed items (cache) Redis Avoid hitting MongoDB on every page load
Password reset tokens Redis Short-lived, must auto-expire after 15 minutes

The Benefits

Performance — each database is optimised for its specific workload, so nothing is compromised. PostgreSQL is not slowed down by unstructured data. Redis is not burdened with complex joins.

Independent scaling — if your product catalog grows to millions of items, you scale MongoDB independently. If your session volume spikes, you add more Redis memory. You never have to scale everything together.

Flexibility — if a better database for a specific use case emerges, you can adopt it for that one part without rewriting your whole data layer.


The Trade-offs — It is Not Free

Multiple databases mean multiple systems to install, configure, monitor, backup, secure, update, and maintain. Developers will need to understand multiple database systems, their query languages, and their APIs.

In practical terms:


When Polyglot Persistence Makes Sense

Polyglot persistence becomes necessary when application data needs vary significantly across features or services. It is common in large platforms, distributed systems, and products handling high traffic and diverse workloads.

Good signals that you might need it:

Bad reasons to add a new database:

<aside> 💡

</aside>