share / sharedb-mongo

MongoDB database adapter for ShareDB
MIT License
152 stars 65 forks source link

Breaking suggestion: add support for transactions #127

Open alecgibson opened 2 years ago

alecgibson commented 2 years ago

MongoDB 3.x was end-of-lifed in April 2021, which means that all currently-supported versions of MongoDB now support transactions.

sharedb-mongo currently uses an optimistic write mechanism to deal with the fact that it has to write both an op and a snapshot as part of a single "commit". This results in some non-canonical ops being committed to the database, and means we need to do some painful work to get a range of canonical ops.

If we moved to using MongoDB transactions, we would know that all committed ops are canonical (since a conflicting transaction can be made to roll back), which means we could vastly simplify op range fetches.

This would be a breaking change, since it would require MongoDB >= 4.X.

We'd also want to do some thinking about how to enable this feature on already-established databases. For example, if you turn on transactions in sharedb-mongo, any documents that were created after the change can be considered to only have canonical ops, but any "legacy" documents created before that time would not. We could potentially set a flag on the snapshots?

alecgibson commented 2 years ago

According to the MongoDB docs:

In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design.

We probably don't want to incur a performance penalty in the mainline case (committing to the database) in order to get a performance gain for a lesser used case (fetching ops).

It's not all necessarily doom and gloom. There are in theory some benefits to latency if committing several ops in the same transaction (although I'm not sure we'd be able to leverage this?).

ericyhwang commented 2 years ago

Other official resources to look at, from doing a bit of searching:

https://www.mongodb.com/docs/manual/core/transactions/ https://www.mongodb.com/docs/manual/core/transactions-production-consideration/ https://www.mongodb.com/docs/manual/core/write-operations-atomicity/

I haven't personally worked with Mongo transactions before, so I don't know what the performance implications of them would be, especially for high-volume writes.