user2741831
user2741831

Reputation: 2410

Can dynamoDBs write consistency guarantee transactional writes?

I have a transaction system in my web service. Basically you can send (fake) currency back and forth between accounts.
This requires a strongly consistent write model. This is the scenario i'm worried about. A has 0$ B has 0$ and C has 100$. C sends A 100$ Cs balance is currently being updated. Before the update is propagated across all dynamoDB nodes, C sends 100$ to B, since on another instance, his balance is still 100$. Now C has a balance of -100$ and A and B both have 100$. This would be an invalid state.
Since this scenario would require both consistent reads and writes (to check and update balances), are dynamoDB writes suited for this?

Upvotes: 1

Views: 1138

Answers (1)

jlucier
jlucier

Reputation: 1582

I'm not a DynamoDB expert, but I'm almost certain you can pull this off.

To make this work, make sure to use the ConditionExpression feature on the UpdateItem request. That way you can protect against what you're talking about.

Reads in DynamoDB are "eventually consistent", so it is possible, as you stated, that a reader could think that "C has $100" just after you've changed C's balance. However, I'm pretty sure using the condition expression you can fully guarantee that you never get into this bad state. You can structure your update request as a totally atomic operation that doesn't execute unless a condition is met: "Decrement C's balance by $100 only if C's balance >= $100". DynamoDB can execute that update atomically for you, and thus prevent the issue.

P.S. check out this talk. If I remember correctly the speaker covers some topics adjacent to this.

Upvotes: 2

Related Questions