7

I am creating a Node.js API consisting of multiple Microservices.

Each Microservice is responsible for one or more features of my application. However, my data is structured into multiple databases which each have multiple collections.

Now I need one sevice to perform atomic operations across multiple databases. If everything happened in the same database, I'd use a normal transaction. However, I don't know how to do this with multiple databases or if this is even possible?

Example:

One of the Microservices takes care of creating users. A user must be created inside two databases. However, this should happen atomically, i.e. if the user is created, it must be created in both databases.

UPDATE: MongoDB's official docs state the following:

With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.

I haven't found anything on how to perform distributed transactions with mongoose though.

I would be extremely glad if someone could give me some clarification on this topic.

2 Answers 2

3

You need to use the SAGA pattern of the microservice architecture.

The SAGA pattern is divided into two types:

  1. Choreography-based saga
  2. Orchestration-based saga

If you want to manage distributed transactions from a single service, then you can use Orchestration-based saga (2).

So with this pattern, you can implement a distributed transaction that either executes a chain of actions or rolls back along the chain, using compensating transactions.

I also recommend studying the patterns of microservice architecture on this site and recommend the book.

8
  • Thanks. Do you know if there’s a way to achieve this with mongoose? Apr 17, 2020 at 8:33
  • 1
    Mongoose.js is a tool with which each of your services communicates with the database, and you need to implement the SAGA pattern at the business logic level, this is a higher-level task. in Other words, if you use Orchestration-based saga, you will need to implement business logic in one of the services, which will start a chain of calls to other services to save data and in case of errors, implement compensating transactions. Apr 17, 2020 at 8:48
  • I'm not familiar with node.js, but this framework may be suitable for you - docs.nestjs.com/recipes/cqrs#sagas Apr 17, 2020 at 8:54
  • I get what you mean but according to MongoDB docs it is possible to run transactions over different databases, meaning that I wouldn’t have to implement the SAGA pattern. Only if Mongoose doesn’t support distributed transactions, I will have to implement your pattern. Apr 17, 2020 at 8:54
  • Another good example - medium.com/@ganeshmani009/… Apr 17, 2020 at 8:59
3
+50

EDIT: Mongoose support Distributed Transactions, because it's a client to MongoDB Server. Form Mongoose point of view, a distributed transaction is just a transaction.

According to this video, on Distributed Transactions in MongoDB the Distributed Transactions is defined above the level of mongoose, and can use it.

in the documentation of mongodb, they say:

Distributed Transactions and Multi-Document Transactions Starting in MongoDB 4.2, the two terms are synonymous. Distributed transactions refer to multi-document transactions on sharded clusters and replica sets. Multi-document transactions (whether on sharded clusters or replica sets) are also known as distributed transactions starting in MongoDB 4.2.

Here is how I would try to solve this (Divide-and-conquer):

  1. Try simple example of Distributed Transactions with MongoDB
  2. Then try using simple mongoose with Transactions (it might be that there is be no different between , Distributed Transactions and non- Distributed Transactions as far as mongoose knows, because the Transactions is in higher level – see video).
  3. Then try to combine the 2 solutions and see it this works,
  4. If does not work with mongoose, I would try to implement Distributed Transactions with MongoDB, as the video implay that they spent a lot of effort in this, and since mongoose just let you do things that you can also do with MongoDB alone. Moving from mongoose to MongoDB maybe not so simple, but implementing Distributed Transactions is very hard.
1
  • 1
    You are right. According to the issue I opened on GitHub, running regular mongoose transactions works perfectly fine. Apr 19, 2020 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.