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:
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
Python Functions
Output Examples
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
Output Examples
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
Output Examples
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
Running the Full Integration Test
Put it all together:
Expected Output
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
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 →