0
votes

In a Cloudant database, what is the expected behavior of calling PUT on a document that doesn't exist with a revision defined?

The documentation says:

To update (or create) a document, make a PUT request with the updated JSON content and the latest _rev value (not needed for creating new documents) to https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID.

I had assumed that if I did provide a revision, that the db would detect that it was not a match and reject the request. In my test cases I have inconsistent behavior. Most of the time I get the expected 409, Document update conflict. However, occasionally, the document ends up getting created (201), and assigned the next revision.

My test consists of creating a document and then using that revision to update a different document.

  1. POST https://{url}/{db} {_id: "T1"} - store the returned revision
  2. PUT https://{url}/{db}/T2 {_rev: }

So if the revision returned was something like 1-79c389ffdbcfe6c33ced242a13f2b6f2, then in the cases where the PUT succeeds, it returns the next revision (like 2-76054ab954c0ef41e9b82f732116154b).

EDIT

If I simplify the test to one step, I can also get different results.

  1. PUT https://{url}/{db}/DoesNotExist {_rev: "1-ffffffffffffffffffffffffffffffff"}
1

1 Answers

1
votes

Cloudant is an eventually consistent database. You're seeing the effects of that. Most of the time the cluster has time to reach a consistent state between your two api calls and you'll get the expected update conflict. Sometimes you hit the inconsistency window, as your first call has not yet been replicated around the cluster and you hit a different node. It's a valuable insight: it's not safe to read your writes.