vimgolf icon indicating copy to clipboard operation
vimgolf copied to clipboard

Would you consider migrating away from MongoDB?

Open filbranden opened this issue 6 years ago • 9 comments

cc @Hettomei

Based off of some benchmarks in #293, I started looking for alternative approaches.

One that occurred to me was to use store the rank in MongoDB and use transactions to keep them up to date, but after initial testing I quickly realized that transactions in MongoDB require specific setups of MongoDB, for instance it doesn't seem to work with the local development/testing setup explained in the readme...

So I started thinking... Why keep using MongoDB? The data itself is quite well suited for a relational database and that's something even easier to support in Rails (pretty much natively) than using MongoDB...

I also saw that Postgres and Sqlite both support a RANK() function that we could use for figuring out the rank of each solution, which is also pretty nice...

Other advantages of using Postgres + Sqlite are that it's much easier to run a local development environment (pretty much stock rails does everything) and I imagine it might be simpler/cheaper to host the solution without the addition of MongoDB.

I started working on this, and while I can see that it's a considerable amount of effort, I think it's pretty doable and I'd be willing to put some time into making that work...

But before I take the plunge... Would you consider migrating away from MongoDB to something like Postgres + Sqlite?

filbranden avatar Aug 26 '20 20:08 filbranden

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.68. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

issue-label-bot[bot] avatar Aug 26 '20 20:08 issue-label-bot[bot]

Hi,

I think the only person that can answer you is @igrigorik

Since I dig deeper in the code, I was convinced that a relational DB would be better for this game. But I also don't have enough knowledge in database to have good jugdment.

I didn't know about Rank() , well suited here :)

I started working on this, and while I can see that it's a considerable amount of effort,

Ho yes !

Hettomei avatar Aug 26 '20 20:08 Hettomei

I don't think there is any technical reason why the site can't run in a relational setup. This site was born as a weekend project and document structure offered by MongoDB was a really simple and intuitive way to structure things (Challenge has a set of entries, etc). Also, this project has grown far further than I expected and, I admit, I've not kept up with all the latest Mongo best practices on how to best set up the data for what we're after. My intuition is that, for someone who knows their stuff with Mongo, there should be no reason why it can't be made to work fast and efficiently.

I'm not against moving to alternate setup if someone can prove that it will run smoother, and willing to do the work.. The real complexity here is that you're basically looking at complete rewrite if you want to migrate to alternate data store.

igrigorik avatar Aug 26 '20 22:08 igrigorik

Quick update here: I'm fairly close to a working Postgres version. Some workflows already working as expected.

Right now, looking into:

  • Data migration (more specifically, the issues with binary data and UTF-8 encoding, see #319).
  • Producing mongo-style ids for the challenges, since those are visible from the URL. My idea is to come up with a similar scheme for Postgres-native challenges, while keeping the old URL ids in a "legacy" column. I have the idea, just need to finish implementing it...
  • Getting all rspec tests to pass and test browsing some more.

I'm probably about two days away from a working version I can share.

I can also show some small-ish challenges on the staging instance. The db I have there has a limitation on total number of rows (free tier), so I won't be able to show large challenges, but I expect to be able to have them locally, so I can see how good performance gets, using the Postgres window functions for ranking entries by score and user...

filbranden avatar Apr 26 '21 02:04 filbranden

I'm really close now. Mostly fixing test cases and some small tweaks.

I have the proof of concept running under http://vimgolf-staging.herokuapp.com/ and using a Postgres database as a backend. Since I used the free tier (limit of 10,000 rows), I only have a limited set of challenges imported, but locally I could confirm a full import of a MongoDB dump from a few weeks back and it worked all fine.

I'll work on getting the test cases passing and produce a PR, hopefully in the next couple of days. Looking forward to merging it and doing a migration here!

filbranden avatar Apr 27 '21 23:04 filbranden

👏🏻 👏🏻 👏🏻

igrigorik avatar Apr 28 '21 06:04 igrigorik

I performed migration from MongoDB to Postgres under ActiveRecord earlier this morning. I used a maintenance window of ~1.5 hours to complete the process.

Running the migration script on Heroku failed, I imagine this was a memory/resources issue of running on that constrained environment. (Or possibly a timeout of the connection to MongoDB Atlas.)

I, [2021-05-06T18:16:30.405622 #4]  INFO -- : MongoImporter: Progress Report

--------------------------------------------------
Users..............:   30852 /   30852 (100%)
Challenges.........:     101 /     514 ( 20%)
Entries............:   73098
Comments...........:     601

Total Objects......:  104652
Elapsed............:  16min 55sec
--------------------------------------------------

rails aborted!
Mongo::Error::OperationFailure: cursor id 2978047921493277914 not found (43)
/app/vendor/bundle/ruby/2.6.0/gems/mongo-2.5.3/lib/mongo/operation/result.rb:258:in `validate!'

In any case, migration was taking longer than my local runs... So I fell back to running the migration locally, by dumping the production database with mongodump, loading it locally, then running the migration script. That worked fine.

I, [2021-05-06T11:35:00.758008 #1181690]  INFO -- : MongoImporter: Migration *COMPLETED*!

--------------------------------------------------
Users..............:   30867 /   30867 (100%)
Challenges.........:     514 /     514 (100%)
Entries............:  373119
Comments...........:    3143

Total Objects......:  407643
Elapsed............:  15min 24sec
--------------------------------------------------

I was then able to load the database on Heroku from this local data, with:

$ heroku pg:push vimgolf postgresql-xyz-123 --app vimgolf

(pg:push is a front-end to pg_dump locally + pg_restore on Heroku.)

At that point, I was able to take the site off of maintenance and browsing it I got to check that everything was OK. I submitted an entry to an existing challenge using the vimgolf put command-line, which worked just fine. I was able to create a new challenge. In the few past hours, I see some entries for the new challenge I created, which leads me to believe it's working for others, so hopefully for everyone...

I'm glad migration is done and the site is now functional! There are going to be some clean up items, I will file separate issues for those. Thanks everyone for the support helping me get this through the finish line!

filbranden avatar May 06 '21 21:05 filbranden

Someone give that man a medal.

b-layer avatar May 08 '21 04:05 b-layer

What an hard work you put into vimgolf !!

Thank you.

Hettomei avatar May 18 '21 05:05 Hettomei