DEV Community

loading...
Cover image for How we moved from MongoDB to Postgres without downtime and cut our costs by 30%

How we moved from MongoDB to Postgres without downtime and cut our costs by 30%

msedzielewski profile image Mike Sedzielewski ・3 min read

Voucherify was born in 2015 as a weekend hackathon project run by our small-scale software house, rspective. Initially, it was backed up by a MongoDB database. Truth be told, this choice was random – it was the most common database we used in our projects. We already had some experience with it so Mongo was a pretty natural component at that stage. However, as Voucherify's scale grew, we’ve added a second database – PostgreSQL – that seemed to be more suitable for the upcoming features. Then for some time we kept part of our data in Mongo, and the other part in Postgres, until the day we decided to move it all to Postgres.

When we started out, we already had around five years of data collected, spread around multiple database instances, located on three continents, each dedicated to a different Voucherify cluster. Millions of voucher codes that could be updated anytime. Around a terabyte of constantly changing data. And to make matters worse, a lot of code had to be prepared for the upcoming breaking change. If presented on a timeline, we spent three months rewriting and testing new code and next three months migrating all the data.

Why then go through all this trouble? We had two valid reasons to do that.

First of all, as you can easily imagine, maintaining two different database types creates a cascade effect of doubled codebase, paradigms and concepts you have to keep in mind while adding new features. It was also the source of problems with the initial setup followed by issues popping up randomly (usually, on Friday afternoons). If one of them looks redundant, then all these issues sum up and cause tension and frustration in the engineering team.

Secondly, Compose – a SaaS platform serving MongoDB that we were using, was very expensive in comparison to alternatives. It became a significant percentage of our monthly expenses. Additionally, we were not satisfied with the quality of the support we got. Sometimes response delays could be as long as several days. In some cases, the only offered solution was to restart the database, with no good explanation of why the weird stuff happened in the first place or whether they plan to fix that in the future.

To succeed with the migration and maintain platform stability when the traffic was high, we split it into a couple of tasks – each corresponding to a different entity. Most of them were easy migrations of relatively small chunks of data that was updated rarely. Each of these tasks has its own story, but this article is going to tell the story of the last task. It was about two core entities – vouchers and campaigns – that serve as primary objects in Voucherify API. As you can imagine, these were kept in Mongo for the longest. You could say that the core of our system was built around the database that had to be replaced.

We used AWS’s Database Migration Service to help us with migration. The primary motivation was to reduce the time of preparing the setup for the migration tool, by relying on a SaaS solution tested by hundreds of developers already.

We decided to create new temporary tables for each Mongo collection, and somehow safely merge them with production tables in the next steps. The PostgreSQL database has a nice feature that helped us, called table inheritance. It gave us a possibility to join two tables together in a hierarchical order to obtain a parent table having multiple independent child tables.

Here is what we did:

  • Sanity check of data in MongoDB.
  • Creating child tables with Postgres Inheritance.
  • Applying transform triggers.
  • Running Amazon Database Migration Service scripts.
  • Sanity check of data in child tables.
  • Moving “deleted” data from child to parent table.
  • Switching applications logic to use Postgres.
  • Anomaly detection.
  • Moving “active’ data from child to parent table.
  • Stopping and removing DMS tasks.

Read the whole post on our blog

Discussion (2)

Collapse
zilti profile image
Daniel Ziltener

First of all, congrats on switching away from MongoDB!
Secondly, why use a SaaS platform for hosting your db if it is so expensive?

Collapse
msedzielewski profile image
Mike Sedzielewski Author

Hey @zilti it was about the ease of use and the promise of support. When we started we had a very limited team so we didn't want to be bothered with ops issues.

Forem Open with the Forem app