The Architecture: Why Polyglot Persistence?

In real-world systems, no single database is best for everything. ShopStream, our e-commerce marketplace, uses three databases to solve different problems optimally:

PostgreSQL (OrdersDB) — Stores customer data, orders, and transactions. Strong ACID guarantees prevent money from disappearing. Relational structure enforces referential integrity.

MongoDB (ProductCatalog) — Stores products with flexible schemas. Groceries have different attributes than electronics. Documents are nested, making product reviews and ratings easy.

Redis (SessionCache) — In-memory cache for shopping carts, session data, and product views. Blazingly fast. Data expires automatically (TTL).

This approach is called polyglot persistence — using the right database for the right job. You'll see this at Stripe (PostgreSQL + Redis), Netflix (Cassandra + Redis), and Uber (PostgreSQL + Cassandra + Redis).

Docker Compose Setup

Before writing code, spin up all three services in one command. Save this as docker-compose.yml:

yaml

Run with: docker-compose up -d

PostgreSQL Layer: Orders & Customers

First, create the relational schema for financial data. Transactions must be atomic, consistent, isolated, durable.

Initialize Tables

sql

Python Functions

python

Output Examples

Create customer: customer_id 102
Get customer: [102, "alice.testuser@shopstream.com", "Alice TestUser", "Austin"]
Get orders: 3 orders (statuses: delivered, delivered, returned)

MongoDB Layer: Product Catalog

MongoDB stores products with flexible schemas. Different categories have different attributes. Reviews are nested documents.

Python Functions

python

Output Examples

Create product: ObjectId returned
Search Fruits: 5 products (Avocados, Bananas, Mangoes, Oranges, Pineapple)
Top products: Fairy Dish Soap (5.0), Cabbage (4.9), Bay Leaves (4.9)

Redis Layer: Sessions & Cart Cache

Redis is an in-memory data store. Shopping carts expire after 24 hours (TTL). Product views are tracked for analytics.

Python Functions

python

Output Examples

Create session: {customer_id: "1", created_at, last_active}
Add to cart: {product_1: 2, product_2: 1}
Product views: 3
Rate limiting: Requests 1-3 allowed, 4-5 blocked

Unified ShopStreamAPI: Cross-Database Coordination

Now orchestrate transactions across all three databases. The checkout flow validates session, fetches cart, retrieves products, creates order, clears cart.

Checkout Flow

python

Running the Full Integration Test

Put it all together:

python

Expected Output

Created customer: 102
Created session: session_102_1713288000
Checkout result: {status: success, order_id: 1, total_amount: 8.57, items_count: 2}
Customer orders: [{order_id: 1, total_amount: 8.57, status: pending, created_at: ...}]
Dashboard: {customer_id: 102, orders: [{...with product names...}]}
Request 1-3: Allowed
Request 4-5: Blocked

Test Your Understanding

Question 1 of 5
Why use PostgreSQL for orders instead of MongoDB?
Question 2 of 5
Why use MongoDB for products instead of PostgreSQL?
Question 3 of 5
Why use Redis for shopping carts instead of a database?
Question 4 of 5
When coordinating a transaction across PostgreSQL, MongoDB, and Redis, what is the biggest challenge?
Question 5 of 5
What are consistency guarantees with polyglot persistence?
0
/5
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You've mastered the full database stack. Ready for real projects?

Join the Best AI Program: The AI-Era Data Engineer — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Data Engineer →