Skip to content

Provide transactions to MongoDB with keeping MongoDB queries. This is a partially rewritten version from my research prototype.

Notifications You must be signed in to change notification settings

hhorii/mongo-tx

Repository files navigation

mongo-transaction (MongoTx)

MongoTx aims to provide transactional support for MongoDB. MongoTx was introduced in MongoDB World 2016 (If you want to know more detail, please contuct horii at jp.ibm.com).

MongoTx works as a client stub that uses the original MongoDB driver with documents stored by existing applications. Therefore, MongoTx doesn't need any additional server, doesn't limit storage engines of MongoDB, and doesn't require data migration of existing applications to process transactions.

There are three main classes in MongoTx.

  • Tx manages transactions. By calling its commit() or rollback(), application can process transactions.
  • TxCollection provides transactional access of similar methods to MongoCollection.
  • TxDatabase provides TxCollection and Tx instances.

Currently, MongoTx supports only Java. Leave comments if you need supports of the other languages.

For example, a transfer transaction in https://docs.mongodb.org/manual/tutorial/perform-two-phase-commits can be implemented as follows.

MongoClient client; MongoDatabase db;

TxDatabase txDB = new LatestReadCommittedTxDB(client, db);
TxCollection accounts = txDB.getCollection("ACCOUNT");

// start transaction
Tx tx = txDB.beginTransaction();

Document accountA = accounts.find(tx, new Document("_id", "A")).first();
Document accountB = accounts.find(tx, new Document("_id", "B")).first();
accounts.replaceOne(tx, new Document("_id", "A"), accountA.append("balance", accountA.getInteger("balance") - 100));
accounts.replaceOne(tx, new Document("_id", "B"), accountA.append("balance", accountB.getInteger("balance") + 100));

// commit transaction
tx.commit();

You can use MongoCollection that provides consistent access to your MongoDB. TxCollection.getBaseCollection(staleness) returns its base MongoCollection that guarantees a bounded staleness, which means the MongoCollection processes queries based on the latest documents at [current time - staleness] or the later.

MongoClient client; MongoDatabase db;

TxDatabase txDB = new LatestReadCommittedTxDB(client, db);
TxCollection accounts = txDB.getCollection("ACCOUNT");
MongoCollection accountsBase = accounts.getBaseCollection(100L);

accountsBase.insertOne(new Document());
accountsBase.deleteOne(new Document());
...

###Atomicity Multiple manipulation of TxCollection instances via the same Tx instance are processed in atomic. When a transaction fails, a TxRollback exception is thrown.

###Consistency Guaranteed consistency is based on an implementation of TxDatabase. The above LatestReadCommittedTxDB guarantees transactions to read the latest committed data. We will support snapshot read in near feature.

###Isolation MongoTx uses optimistic concurrency control. When multiple transactions simultaneously write the same document, one transaction can be committed and the others will be rolled back.

###Durability Durability depends on the underlying MongoDB.

How to use

download latest build and mongo-java-driver (3.0.4 or later).

How to build

Clone the repository and build MongoTx

$ git clone https://github.com/hhorii/mongo-tx.git
$ cd mongo-tx
$ mvn -Dmaven.test.skip=true install

This will generate a binary package ./target/com.ibm.research.mongotx-VERSION-SNAPSHOT.jar that enables application to use transactions.

To run tests, MongoDB (3.0 or later) must run wit 27017 TCP/IP port.

###Supported MongoDB features

  • Query: Query operators are supported. Update operators will be supported in near feature.
  • Index: Indexes created DatabaseCollection are enabled while processing transactions.
  • Sharding: Application can access documents across multiple shards.
  • Replication: MongoTx assumes [strict consistency] (https://en.wikipedia.org/wiki/Consistency_model#Strict_Consistency). Configurations of MongoDB's replication need to guarantee the consistency.

###Developing features

  • Snapshot read
  • Update operators (updateMany methods, etc)
  • Aggregation functions
  • Other APIs

How to run DayTrader

see DT3 README

License

Code licensed under the Apache License Version 2.0.

Code has not been thoroughly tested under all conditions. We, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. The code is provided "AS IS", without warranty of any kind. We shall not be liable for any damages arising out of your use of the code.

About

Provide transactions to MongoDB with keeping MongoDB queries. This is a partially rewritten version from my research prototype.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published