You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Scott Shumaker <ss...@gmail.com> on 2009/04/03 08:09:24 UTC

New bulk docs behavior - transactional

We've been using CouchDB in our new web startup for the past few
months to represent a very rich set of interconnected data.  We have
tons of code modeling different types of objects in Javascript, and a
run-time serialization layer that serializes objects to CouchDB
(including serializing 'pointers' to other couchdb docs, instantiating
Javascript classes on load, etc), all done in the client-side
run-time.

The recent removal of the ability to treat a bulk_write as a single
atomic transaction (that is rejected if any conflicts happen) is
pretty much a deal killer for us, though.

There are plenty of times when you have two separate documents that
need to be changed atomically.   Everything from sorted lists,
reference counted shared objects, and so on.  Our whole reason to use
CouchDB vs. a traditional database was that it let us model our
documents around the way we're actually using them, from a UI and
design perspective - not being forced into some bizarre contortions to
try to deal with arbitrary limitations of the underlying storage
system.

I understand the motivations for removing the transactional semantics
- they're hard to do right, and can be very expensive in a sharded
solution.  But there are some times when it's almost essential to have
them - even if the cost is higher.

That said, it may be possible to come up with some compromises on
transactional qualities that make it easier to implement.

How about something like the following?

During an 'transactional bulk write':

- For each document in the transaction,

   - Attempt to create a new rev of the document, specially flagged so
it doesn't show up in views, document fetches, or replications (return
the last version of this documents instead).
   - This rev SHOULD be examined when checking for conflicts with
subsequent writes (so no new writes to the document can happen while
the transaction is in progress - they return with a conflict error).

- If all of the documents in the transaction are created successfully
(pass document validation, don't conflict with earlier revs), remove
the flags from these revisions (therefore becoming standard revs in
the database).  If not, remove these newly created revs.

This would sort of give a loose form of two-phase commit that should
work over sharded databases.  There's obviously a higher performance
cost - it's equivalent to two writes (once to write these 'shadow'
revisions, and once again to remove the flags on success or prune the
revisions on failure).  And clients won't be able to write to any of
the documents in the transaction until it completes.  But it would
probably achieve the effect that most people want.  And naturally, it
wouldn't be the default bulk_write behavior (given the cost and
limitations).

Would this approach work?

Scott

Re: New bulk docs behavior - transactional

Posted by Scott Shumaker <ss...@gmail.com>.
Effectively, yes.  But I feel that representing this as a hidden rev
fits more into the current model.  Instead of a lock, you just have a
new hidden rev that the clients can't see, but there would probably be
a way to retrieve it, in the same kind of way you can retrieve
conflicted versions of a document.

For Multi-master replication, this really shouldn't change anything.
The hidden rev won't replicate to other masters, bug if another master
attempts to replicate a version over the top of the hidden rev, it's
treated exactly the same way as a standard multi-master conflict; it
will be flagged as conflicted, and one of them will show up in views.

Although as an aside, I'm not sure how multi-master replication could
really work in a production system for most kinds of data, even in the
standard (non-transactional) case.  The fact that 'an arbitrary rev'
shows up in views as a result of the conflict seems like it would have
unacceptable consequences most of the time.  To see why, examine the
behavior you typically need when you're doing a simple write and get a
conflict (because another client got there first).  It turns out that
the conflict resolution behavior is very case-specific.  Sometimes you
need to just refresh the latest version.  Other times, you choose one
based on a timestamp.  Sometimes you need to merge the two, and
present interface to the client to do so (like Amazon.com's merge
shopping carts screen).   Noticing conflicts later on with some sort
of cronjob often doesn't give you enough information to decide what to
do - and if you require user intervention to resolve the conflict,
this definitely won't work!  So the fact that replication conflicts
don't have symmetric behavior with standard write conflicts makes
multi-master replication untenable for most needs.


On Sun, Apr 5, 2009 at 12:46 PM, Brian Candler <B....@pobox.com> wrote:
> On Thu, Apr 02, 2009 at 11:09:24PM -0700, Scott Shumaker wrote:
>> How about something like the following?
>>
>> During an 'transactional bulk write':
>>
>> - For each document in the transaction,
>>
>>    - Attempt to create a new rev of the document, specially flagged so
>> it doesn't show up in views, document fetches, or replications (return
>> the last version of this documents instead).
>>    - This rev SHOULD be examined when checking for conflicts with
>> subsequent writes (so no new writes to the document can happen while
>> the transaction is in progress - they return with a conflict error).
>
> In that case, is this new magic rev not just the same as a lock?
>
> Implementing a per-document lock within one database is simple enough, and
> indeed it could be done in a sharded environment too. However it would be
> extremely difficult to implement if you allow multi-master replication too.
> You would also need procedures to recover locks due to transactions which
> have stalled or silently aborted (remembering that HTTP is a stateless
> protocol)
>
> Regards,
>
> Brian.
>

Re: New bulk docs behavior - transactional

Posted by Brian Candler <B....@pobox.com>.
On Thu, Apr 02, 2009 at 11:09:24PM -0700, Scott Shumaker wrote:
> How about something like the following?
> 
> During an 'transactional bulk write':
> 
> - For each document in the transaction,
> 
>    - Attempt to create a new rev of the document, specially flagged so
> it doesn't show up in views, document fetches, or replications (return
> the last version of this documents instead).
>    - This rev SHOULD be examined when checking for conflicts with
> subsequent writes (so no new writes to the document can happen while
> the transaction is in progress - they return with a conflict error).

In that case, is this new magic rev not just the same as a lock?

Implementing a per-document lock within one database is simple enough, and
indeed it could be done in a sharded environment too. However it would be
extremely difficult to implement if you allow multi-master replication too.
You would also need procedures to recover locks due to transactions which
have stalled or silently aborted (remembering that HTTP is a stateless
protocol)

Regards,

Brian.