简体   繁体   中英

DynamoDB Transaction - Read and Write

I have two DynamoDB tables. I have a use-case where I need to increment an attribute (say rank) in table 1 and use the updated rank value to create an entry in table 2.

If I use transaction write feature in Dynamodb, I don't think there is a way to return the updated value from transaction item 1 and use it to transaction item 2.

Can anyone please let me know how to solve this?

Example: Table 1 has the record - <UniqueId, 2>

During transaction update, I want to update Table 1 record as <UniqueId, 3> and create an entry in Table 2 with record as <CustomerId, 3>

Thanks in advance.

You cannot do this as a single transaction.

You can use use this pattern:

  1. Get the latest unique id
  2. Calculate the next value (on the client-side)
  3. Write a transaction with two parts: set the latest unique id to the new value and use Optimistic Concurrency Control (OCC) that uses a ConditionExpression to fail the write if the value has changed since your read, and then also in the same tx write to the second table using the new value
  4. If the OCC failed then goto 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM