Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are DynamoDB conditional writes transactional?

I'm having trouble wrapping my head around the dichotomy of DDB providing Condition Writes but also being eventually consistent. These two truths seem to be at odds with each other.

In the classic scenario, user Bob updates key A and sets the value to "FOO". User Alice reads from a node that hasn't received the update yet, and so it gets the original value "BAR" for the same key.

eventual consistency

If Bob and Alice write to different nodes on the cluster without condition checks, it's possible to have a conflict where Alice and Bob wrote to the same key concurrently and DDB does not know which update should be the latest. This conflict has to be resolved by the client on next read.

But what about when condition write are used?

User Bob sends their update for A as "FOO" if the existing value for A is "BAR". User Alice sends their update for A as "BAZ" if the existing value for A is "BAR".

cond write

Locally each node can check to see if their node has the original "BAR" value and go through with the update. But the only way to know the true state of A across the cluster is to make a strongly consistent read across the cluster first. This strongly consistent read must be blocking for either Alice or Bob, or they could both make a strongly consistent read at the same time.

So here is where I'm getting confused about the nature of DDBs condition writes. It seems to me that either:

  1. Condition writes are only evaluated locally. Merge conflicts can still occur.
  2. Condition writes are evaluated cross cluster.

If it is #2, the only way I see that working is if:

  1. A lock is created for the key.
  2. A strongly consistent read is made.

Let's say it's #2. Now where does this leave Bob's update? The update was made to node 2 and sent to node 1 and we have a majority quorum. But to make those updates available to Alice when they do their own conditional write, those updates need to be flushed from WAL. So in a conditional write are the updates always flushed? Are writes always flushed in general?

There have been other questions like this here on SO but the answers were a repeat of, or a link to, the AWS documentation about this. The AWS documentation doesn't really explain this (or i missed it).

like image 903
micah Avatar asked Apr 21 '26 13:04

micah


2 Answers

DynamoDB conditional writes are "transactional" writes but how they're done is not public information & is perhaps proprietary intellectual property.

DynamoDB developers are the only ones with this information.


Your issue is that you're looking at this from a node perspective - I have gone through every mention of node anywhere in DynamoDB documentation & it's just mentions of Node.js or DAX nodes not database nodes.

While there can be outdated reads - yes, that would indicate some form of node - there are no database nodes per such when doing conditional writes.

User Bob sends their update for A as "FOO" if the existing value for A is "BAR". User Alice sends their update for A as "BAZ" if the existing value for A is "BAR".

Whoever's request gets there first is the one that goes through first.

The next request will just fail, meaning you now need to make a new read request to obtain the latest value to then proceed with the 2nd later write.

The Amazon DynamoDB developer guide shows this very clearly.

Note that there are no nodes, replicas etc. - there is only 1 reference to the DynamoDB table:

enter image description here


Condition writes are probably evaluated cross-cluster & a strongly consistent read is probably made but Amazon has not made this information public.

like image 170
Ermiya Eskandary Avatar answered Apr 23 '26 08:04

Ermiya Eskandary


Ermiya Eskandary is correct that the exact details of DynamoDB's implementation aren't public knowledge, and also subject to change in the future while preserving the documented guarantees of the API. Nevertheless, various documents and especially video presentations that the Amazon developers did in the past, made it relatively clear how this works under the hood - at least in broad strokes, and I'll try to explain my understanding here: Note that this might not be 100% accurate, and I don't have any inside knowledge about DynamoDB.

  1. DynamoDB keeps three copies of each item.
  2. One of the nodes holding a copy of a specific item is designated the leader for this item (there isn't a single "leader" - it can be a different leader per item). As far as I know, we have no details on which protocol is used to choose this leader (of course, if nodes go down, the leader choice changes).
  3. A write to an item is started on the leader, who serializes writes to the same item. Note how DynamoDB conditional updates can only read and update the same item, so the same node (the leader) can read and write the item with just a local lock. After the leader evaluates the codintion and decides to write, it also sends an update to the two other nodes - returning success to the user only after two of the three nodes successfully wrote the data (to ensure durablity).
  4. As you probably know, DyanamoDB reads have two options consistent and eventually-consistent: An eventually-consistent read reads from one of the three replicas at random, and might not yet see the result of a successful write (if the write wrote two copies, but not yet the third one). A consistent read reads from the leader, so is guaranteed to read the previously-written data.

Finally you asked about DynamoDB's newer and more expensive "Transaction" support. This is a completely different feature. DynamoDB "Transactions" are all about reads and writes to multiple items in the same request. As I explained above, the older conditional-updates feature only allows a read-modify-write operation to involve a single item at a time, and therefore has a simpler implementation - where a single node (the leader) can serialize concurrent writes and make the decisions without a complex distributed algorithm (however, a complex distributed algorithm is needed to pick the leader).

like image 34
Nadav Har'El Avatar answered Apr 23 '26 09:04

Nadav Har'El