3

I need to update 2 tables in one transaction.

In current version RethinkDB doesn't support transactions from the box. So, how can I achieve this?

I can make update in 2 ways:

  1. Update 1st table. If success -> update second table.
  2. Update 2nd tables async.

But how can I resolve case, when 1 of 2 updates was completed well, but another no? Yes, I can check result of update and revert update if error occured. But anyway, there can be case, when something happens with application (lost connection to Rethink, or just crash of script), but one of two updates was completed. So, my data base will be in inconsistent state. And no way to resolve this.

So, is it possible to simulate transaction behavior in nodejs for RethinkDB?

3 Answers3

5

The best you can do is two-phase commit. (MongoDB has a good document on how to do this, and the exact same technique should work in RethinkDB: http://docs.mongodb.org/master/tutorial/perform-two-phase-commits/ .)

mlucy
  • 5,249
  • 1
  • 17
  • 21
3

RethinkDB supports per key linearizability and compare-and-set (document level atomicity) and it's known to be enough to implement application level transactions, more over you have several options to choose from:

  1. If you need Serializable isolation level then you can follow the same algorithm which Google use for the Percolator system or Cockroach Labs for CockroachDB. I've blogged about it and create a step-by-step visualization, I hope it will help you to understand the main idea behind the algorithm.

  2. If you expect high contention but it's fine for you to have Read Committed isolation level then please take a look on the RAMP transactions by Peter Bailis.

  3. The third approach is to use compensating transactions also known as the saga pattern. It was described in the late 80s in the Sagas paper but became more actual with the raise of distributed systems. Please see the Applying the Saga Pattern talk for inspiration.

rystsov
  • 1,868
  • 14
  • 16
2

We had a similar requirement to implement transactional support in ReThinkDB, as we wanted to have transactions extending across MySQL and ReThinkDB DB boundaries. We had come-up with this micro library thinktrans https://github.com/jaladankisuresh/thinktrans, which is a promised based declarative javascript library for RethinkDB supporting Atomic transactions. However, It is still in its alpha stages

If you have a specific requirement and you may want to understand its approach Implementing Transactions in NoSQL Databases and implement your own.

Disclaimer: I am the author of this library