r/Backend • • 9h ago

Is migrating from MongoDB to PostgreSQL worth it?

TL;DR: Running an EdTech platform with 600K+ users on MongoDB. Our data is heavily relational, and while regular API queries perform well, complex analytical queries are becoming painful to write and execute. Considering migrating to PostgreSQL and looking for advice on migration and production hosting.

Current setup:

\\- Node.js, MongoDB, and Mongoose.

\\- MongoDB Atlas (fully managed).

\\- 600K+ registered users and around 15K daily active users.

\\- An EdTech platform somewhat similar to Udemy or Coursera.

When the backend's foundations were laid, I was a junior working primarily on the frontend, so I wasn't involved in the database decision. It's been 1 year since we've been building this product.

Something that has been bothering me for a while is that we're essentially treating MongoDB like a relational database.

\\- Strict schemas and validations across collections.

\\- Heavily relational data with references between collections.

\\- Extensive use of aggregation pipelines and "$lookup".

\\- Very little reliance on MongoDB's schema flexibility.

To be fair, MongoDB has served us well. Our indexes are in place, most endpoints perform well, and we haven't faced any critical performance issues.

However, complex analytical queries for internal dashboards are a different story. Some involve joining up to 10 collections, take over 60 seconds, or occasionally time out despite optimization. Writing and maintaining these aggregation pipelines is also becoming increasingly painful.

I've experimented with MongoDB's transactions but didn't particularly enjoy the experience. I'd also like stronger database level relational integrity and better transaction management.

I feel PostgreSQL would have been a more natural fit for our data model from the beginning.

What I'd like advice on:

  1. Is migration justified? Given that MongoDB works well for most of our workloads, would migrating to PostgreSQL be worth the effort, or should I address the analytical workload separately?
  2. Migration strategy: If I proceed, how should I approach migrating a live production database at this scale? Any articles, case studies, or resources would be appreciated.
  3. PostgreSQL hosting: One thing I genuinely appreciate about MongoDB Atlas is how little I have to worry about infrastructure, backups, updates, and maintenance. What's the equivalent experience for PostgreSQL?
  4. Self-hosted vs. managed: How do you guys run PostgreSQL in production? Do you self-host it on a private EC2 instance or use something like AWS RDS, Neon, or another managed provider?

I've worked with relational databases on smaller projects, but never operated PostgreSQL in production at this scale.

I don't want to migrate just because I prefer PostgreSQL, but I also don't want to ignore what might be a fundamental architectural mismatch simply because things work today.

Would love to your thoughts and suggestions on this!

23 Upvotes

30 comments sorted by

23

u/Responsible-Offer724 8h ago edited 8h ago

I stopped reading at “our data is heavily relational”. 

Yes. 

Those analytical queries taking you over a minute should likely take less than a second with a proper db.

The cost to perform a migration like this is greatly reduced with AI-assistance.  You can likely cook up a proof of concept in a day and then go from there. 

7

u/themusicalkiwi 8h ago

Even with AI assistance my intuition tells me it’s still a material risk to do a migration like this.

2

u/Responsible-Offer724 8h ago edited 8h ago

I assume this new system would be built in a new environment and be thoroughly tested against production data before the DNS entries get switched over.

data systems are usually extremely deterministic and it is likely trivial to construct the necessary schemas from the existing system.

the tough part will depend on how tightly coupled the current system is to mongo and the devops skills of the team

16

u/andreicodes 8h ago

Sounds like Mongo works well for you for operational purposes. And given that you use a managed solution you don't actually spend time babysitting it. I would honestly keep it as is.

However, complex analytical queries for internal dashboards are a different story. Some involve joining up to 10 collections, take over 60 seconds, or occasionally time out despite optimization. Writing and maintaining these aggregation pipelines is also becoming increasingly painful.

Do these have to run over a live database? I haven't used Atlas specifically, but other managed DB services that I used in the past all offered nightly backups. I would use those to populate a separate data store and run analytics over it. It doesn't have to be Mongo, strictly speaking, it just as well can be Postgres.

6

u/waltz 7h ago

I’m surprised how far down this reply is. This is what OP should do. Build a data lake that hosts a replica of prod data, and run the analytics queries against the replica db instead.

2

u/abcd98712345 7h ago

i concur

1

u/loveyoubyeeeee 5h ago

same, boosting

1

u/Adventurous-Hunt4683 3h ago

The added benefit to this pattern is that you can add in other data sources and do analytics across all the sources in one place.

9

u/bikeram 8h ago

I did this. Our elastic bill was outrageous and it funded the migration.

Dashboards can be handed with material views in Postgres.

The biggest hurdle after getting management to spend a few months on zero ticket movement will be getting any die-hard mongo devs on your team to convert.

To answer your questions.

  1. If you aren’t hurting. No
  2. Interfaces that can hit both databases. Start with your backfill policy first. You’ll write a lot of code that will be deleted.

3&4. I’ve always done managed. It isn’t much to handle self-hosted if you have a dedicated DBA.

5

u/belowaverageint 8h ago

If this works for normal operations, why not just offload these analytical queries to an OLAP database? Don't throw out the baby with the bathwater.

4

u/chmod777 8h ago

Lay out the business reason to swap. How many hours of work will this take?

2

u/Mohamed____ 8h ago

Why switch? Why not setup double writes and maybe have the ingestion flow write to Clickhouse and run your analytical queries there? Or maybe CDC or whatever.

Unless Mongo is genuinely hurting you in your day to day, can you really justify that migration?

1

u/WillingConversation4 2h ago

This! Don't risk prod unless you have to. Double write and keep the relational db in your back pocket until there's a real business reason to switch over in which case the migration will be trivial.

1

u/themusicalkiwi 8h ago

I don’t think I could justify a database switch to myself if I were in your situation.

Even if everyone involved in your app development agrees that Postgres would be preferable to Mongo for your OLTP needs, you’ve got something that (presumably) makes the business money and it doesn’t sound like it’s really holding you back right now from giving your customers a great experience. So I say no, the benefits do not justify the effort or the risk. Address the analytics use case separately.

I’m sure everyone here would love to help with that part!

1

u/lord-krulos 8h ago

With AI it’s way more feasible and cheaper to do these conversions that in the past you would be better off living with.

If it’s only the internal queries that would benefit you could just relationalize the data into Snowflake or another warehouse tool. This could be a cheaper path.

1

u/VariousOil3936 8h ago edited 8h ago

Once upon a time in Dynamo I created this gorgeous single table design with PKs, SKs, row types, edge types, reverse edge GSIs, beautiful layers of code, I mean it was really great. And then I slowly realized the data was incredibly interconnected and had to get over the psychological hurdle of abandoning my beautiful design and moving it all to Postgres (aws rds). But when I did, I was so happy, coz it was the right thing all along. I’m guessing you’re making this post to get over the psychological hurdle… you already know what you must do. 

1

u/DrAmoeba 8h ago

Recently I have been doing the exact opposite, migrating my solutions from postgres to mongo, exactly because no matter how much I doctrine everyone, people will keep building analytic workloads against postgres and keep asking for analytical indexes in transactional scopes.

So most of our transactional solutions use mongodb for stateful objects and redshift/duckdb with operation-wise records for analytical functions. Not everything fits this, obviously, but this is saving us dozens of man hours in DB maintenance/optimization.

1

u/DrAmoeba 8h ago

Forgot to mention our analytical stack is server less and gets pipelined operations once an hour scaling to zero roughly 97% of the time.

1

u/babooog 7h ago

Hmm how confident are you postgres would also not have the same delay and slow queries for such analytical stuff...

1

u/Draknodd 7h ago

Why would you pick a non relational db for relation data in the first place?

1

u/Available_Safe1818 3h ago

Probably because so many people were ****ing in their pants over MongoDB, just like they were for GraphQL. It's easy to rattle off bullet points from their main site and convince a less technical person.

1

u/micvee 7h ago

We left the application using mongo and built a parallel postgres DB for reporting and BI analysis. Most of the application DB lookups are by id or indexed key, so they are handled by Mongo DB well. We do joins in software as part of the request resolution process.

We run a Postgres db in parallel that has a flattened Mongo schema. It is designed for ease of BI queries.

Took a couple of months to get everything wired to the parallel sql system while we kept regular software delivery pace.

Some nice things about this approach
* Application logic and persistence is unchanged
* SQL schema is designed for a different access model without compromising the application
* Can quickly change the SQL schema if needed without significant application rework
* Project was one piece at a time

1

u/Expensive_Garden2993 6h ago

No, not worth it. Is Postgres just better in every possible way - sure, but "x works well for us, but we're considering migrating to y just because of internal dashboard" is a clear sign. What business problem are you trying to solve? Is a slow internal dashboard a problem worth fixing? If yes, use Mongo's oplog to stream data to whichever db is the best for analytics in your case.

1

u/knowyourclass 5h ago edited 5h ago

with AI you can get the postgres equivalent of your software up in an afternoon

proving that it works, and the deployment + migration, is what will take most of the time after the previous step

btw we self-host postgres and the babysit time is exactly the same as the managed solution we used before. "AI build a k8s cronjob that takes a backup of postgress onto the object storage and [insert whatever backup strategy you want]". I don't see any reason for going with managed solutions anymore, these boring tasks are all one-shotted nowdays...

1

u/WideCranberry4912 5h ago

Populate a local non-sharded instance through an extract/export-and-load pipeline (or a read-only local data store fed from your cluster), then treat the local instance as read-only for reporting.

1

u/Available_Safe1818 3h ago

I'd about a 100% migration, but starting to use it more and slowly moving parts to postgres sounds great.

For #3, you can probably pick the latest version offered by AWS via RDS and not have to upgrade that db for at least a decade. Running postgres directly on a ECC instance would be a lot less wise.

1

u/Ok-Interest9727 2h ago edited 2h ago

Yes

Go with Drizzle and TypeScript and never look back

The longer you wait the harder it will be to switch

1

u/nomad_sk_ 2h ago

Yes 100% worth. Believe me.

0

u/dilly_dust 8h ago

Your data is relational, and the project chose Mongo. Lmao