Comparison

MongoDB vs PostgreSQL: Data Model and Performance

Compare MongoDB and PostgreSQL for document modeling, constraints, indexing and performance. Use real HN cases and a staging exercise to make the choice.

Editorial update

Decision in brief

Start with PostgreSQL when correctness depends on relationships between entities or product teams often query data in new ways. Shortlist MongoDB when most requests read or write a bounded document as a unit and the team can design around those access patterns. Use PostgreSQL jsonb if only part of the data is flexible. Keep an existing engine if it meets your targets and the proposed replacement cannot show a material gain.

Relate or embed: PostgreSQL — Related records. MongoDB — Whole documents. JSONB — Flexible fields.
Decide what must change together and how the application queries it. Test the proposed design under your actual access patterns.

See discussion trends ↓

Choose by the shape of your reads and writes

Start with the operation that changes the greatest amount of state, then list what it must update atomically. Draw the boundaries on paper: data owned entirely by one customer profile is a different problem from inventory shared by thousands of orders.

WorkloadStart withWhat to prove
Orders, inventory and payments with shared invariantsPostgreSQL with relational tables and constraintsTest duplicate checkout and concurrent stock reservations. A failed transaction should leave no inconsistent state.
Product listings read and updated per SKU, with bounded variationMongoDB with an aggregate per SKUMeasure document size at the tail and define the update path for shared attributes. If lists can grow without bounds, test references or a different model.
Relational business data with optional provider fieldsPostgreSQL typed columns plus jsonb for the variable payloadPromote frequently joined or constrained values to typed columns. Add targeted indexes for the JSON predicates you actually run.
Established document service considering a rewriteKeep MongoDB until a specific blocker is reproducedCompare throughput, on-call burden and recovery on matching topologies before pricing the rewrite.

Document schemas, indexing and transaction boundaries

MongoDB can embed related fields or reference separate documents. Embed data that is usually accessed together and has a clear size bound; reference shared or unbounded data. Add schema validation where downstream code requires field types. Single-document updates are atomic; multi-document transactions are available on supported replica set and sharded deployments, but frequently crossing an aggregate boundary deserves a separate contention and latency test.

PostgreSQL jsonb supports indexing and can sit beside foreign keys and unique constraints. Indexing every variable attribute increases write and storage work; build indexes around actual predicates. For a lookup-heavy comparison, inspect both engines' execution plans for scanned rows or documents, and measure latency during continuous updates.

What the Hacker News cases add

Drawize creator lombarovic reported reaching 100 million drawings and described running PostgreSQL and MongoDB on one dedicated database server. The report shows that a mixed architecture can fit a small deployment footprint. The app also uses local asset prefetching and persistent WebSockets, so its responsiveness cannot be attributed to either database.

HN commenter cyberpunk reported that a MongoDB replica set handled the bank application's multi-zone write workload better than their PostgreSQL reproduction. They described roughly 175,000 writes per second and said the system was not yet sharded. The comment leaves data shape, exact durability and replication settings, hardware, and percentile latency unspecified. Recreate the transaction mix and acknowledged-write guarantees before borrowing the result.

Run the same correctness and performance trial

Treat a missing constraint, an unsafe failover or an unbounded document as a blocker before scoring speed. Use the same business rules on both implementations, while letting each use its natural schema and indexes.

  1. Seed both staging environments with the same sanitized records, including the largest real aggregates, the most skewed tenants and shared entities. Record engine versions, instance limits, replica topology and settings governing when each engine acknowledges a write.
  2. Exercise four paths with a fixed replay: read one aggregate, update a shared field, reject a duplicate, and run one cross-entity report. Add two concurrent workers competing to update the same logical record or reserve the last unit. Compare business outcomes before timing.
  3. Replay at normal and peak concurrency with the same client location and resource budget. Save p95 and p99 per operation, storage and memory use, retry counts, and query plans. In a staging failover, count missing acknowledged writes and time until writes resume.
  4. For a migration, test a backfill plus attributable changes after a recorded watermark. Reconcile totals and invariant violations. Keep the source and define when to stop or reverse cutover if the target cannot meet correctness, latency or restore targets.

Related articles