Databases: Comparison, Choosing & Polyglot
jsonb: Indexes, Tradeoffs & Exercise
| 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 |
Each database type optimises for a different thing:
There is no single "best" database. The right choice depends on your data shape and how you access it. These questions will guide you:
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 | 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 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.
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.
Here is how a backend might distribute data across multiple databases:

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 |
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.
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:
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>