🚀 I wanted to know: Which database inserts 1M records faster—MongoDB or PostgreSQL?
So I built a benchmark pipeline from scratch—and the results surprised me.
💡 Why I Built This
I've always loved both Mongo and Postgres. But when it comes to high-volume writes...
💭 Which one actually performs better?
To find out, I created a controlled benchmark:
- Installed both on Docker locally
- Used realistic e-commerce data
- Ran single, batch, and concurrent insert tests
- Collected performance data: insert speed, CPU, memory
🧪 Step 1: Generate Realistic Data
I used Python + Faker to simulate real-world e-commerce activity:
- 50,000 customers
- 50,000 products
- 900,000 orders
Each record includes nested objects, timestamps, and varied fields. Think: preferences, inventory, shipping, JSON blobs.
python data_generator.py --customers 50000 --products 50000 --orders 900000
🛠️ Step 2: Run the Benchmark
I tested 3 insertion styles for both Mongo and Postgres:
- Single inserts – one record at a time
- Batch inserts – 100 to 10,000 records per operation
- Concurrent inserts – up to 8 threads with batching
Metrics collected:
- 🕐 Elapsed time
- 🔁 Records per second
- 🧠 CPU usage
- 📦 Memory usage
⚔️ What I Found
🚀 Overall Performance
Database | Avg Throughput (records/sec) |
---|---|
MongoDB | 46,012 |
PostgreSQL | 8,900 |
MongoDB was 5.17x faster on average.
📦 Batch Inserts (Customers)
- MongoDB peaked at 78K/sec
- PostgreSQL topped at 29K/sec
Batching unlocks massive gains for both—but Mongo scales faster.
🧵 Concurrent Inserts (Orders)
- MongoDB: 38K → 110K/sec (with 8 threads)
- PostgreSQL: 14.6K/sec (plateaus early)
Mongo loves parallelism. Postgres has limits here.
🔍 Single Inserts
- PostgreSQL wins in single insert performance
- Its row-level optimization outperforms Mongo’s write path
⚙️ Resource Usage
- Postgres used less CPU & memory overall
- Mongo burned hotter under concurrency
🧠 TL;DR: Who Wins?
Category | Winner |
---|---|
🏎️ Overall Speed | MongoDB |
🎯 Single Inserts | PostgreSQL |
📦 Batch Scaling | MongoDB |
🧵 Concurrency Scaling | MongoDB |
💡 Resource Efficiency | PostgreSQL |
🔧 If you need blazing speed, go MongoDB.
🛡️ If you need strict schema & efficiency, go PostgreSQL.
🧪 Try It Yourself (GitHub Repo)
Want to run your own tests?
# Clone the project
git clone https://github.com/hassancs91/mongo-vs-postgres
cd mongo-vs-postgres-benchmark
# Start databases
docker run -d --name mongo -p 27017:27017 mongo
docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres
# Generate data
python data_generator.py --customers 50000 --products 50000 --orders 900000
# Run benchmarks
python benchmark.py --data-dir data --output-dir results --postgres-host localhost ...
# Generate report
python report_generator.py --results-dir results --output-file results/case_study_report.md --charts-dir results
📁 You’ll get:
- JSON results
- PNG charts
- Full Markdown report
🔭 What I'm Testing Next
- Complex query performance (joins, aggregations)
- Updates and deletes
- Mixed workloads (read + write)
- 10M+ record datasets
💬 Let’s Talk
Which database has performed better for you in real-world write-heavy apps?
Have you ever hit a scaling wall with one of them?
I'd love to hear your stories 👇
📥 Full report + visuals + code:
👉 https://github.com/hassancs91/mongo-vs-postgres
🧑💻 Happy benchmarking!