Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling MongoDB transactions without replica sets or with least possible configuration

[Some background information - possibly skippable]

To begin with, I have barely any understanding of database management and just shallow experience with mongoose and node in the backend realm(a couple of udemy courses). Udemy courses made me believe that mongodb was still a viable choice for a database with relational properties and off I went working on a backend for a forum-like website. After learning about transactions, I attempted to implement them in my backend, since it seemed perfectly necessary to implement a rollback feature when executing an array of queries. However it turns out that transactions are only possible on replica sets - which also appeared to require a minimum of 2. 3 databases for a startup MVP was obviously considered an overkill.

[The question]

  1. Is it possible to implement transactions with only 1 database? If so how?

  2. If the above is not possible, how would one launch a mongodb database with minimum configuration while implementing transactions, with the fact that the database is for a startup MVP in consideration.

  3. (For anyone who has experience in implementing a production-level mongo database in a similar scenario) If not using transactions was considered, how to send queries editing/creating multiple documents to a mongoDB database safely without transactions, while not spraying every bit of code with try-catches consisting of queries to rollback every point of failure(which I considered as way too much overhead)

I have a tight deadline, and have already done a substantial bit of groundwork and a couple of routes using mongoose, which means ditching mongodb for a relational database will be a difficult option at the moment.

I think I've googled everything related to the subject at matter and even tried blogs / articles in the second page of a google search(which many including myself consider as the dark web /s). Yet I do think I may have missed what I was looking for and an answer consisting of just links is also welcome.

Thank you for reading!

like image 890
fly Avatar asked Jun 12 '20 11:06

fly


People also ask

Why do we need replica set in MongoDB?

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. This section introduces replication in MongoDB as well as the components and architecture of replica sets.

Can MongoDB be used for transactional applications?

MongoDB added support for multi-document ACID transactions in version 4.0, and MongoDB expanded that support to include distributed transactions in version 4.2. You can implement a transaction in a variety of programming languages using any of MongoDB's official drivers.

Which can be used to check the replica set configuration in MongoDB?

You can access the configuration of a replica set using the rs. conf() method or the replSetGetConfig command.


Video Answer


1 Answers

You need a replica set[*] to use transactions, but you can create a single-node replica set for testing purposes.

The full procedure is described in documentation, for a single-node RS you follow that as written but only configure a single member.

Briefly, you need to pass --replSet argument to mongod and then connect to it through mongo shell and run rs.initiate().

Note that transactions aren't a magic solution to all problems. There are scenarios where they are appropriate and scenarios where MongoDB provides other functionality that would be a better fit.

[*] or a sharded cluster with MongoDB 4.2+, but this involves more setup work.

like image 50
D. SM Avatar answered Oct 11 '22 04:10

D. SM