Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-increment counter in Dynamo DB

I am trying to implement the auto -increment counter for hash key in dynamo db and my code fails for concurrent transactions. any help to implement the feature will be appreciated. I am new to stack over flow might be not able to specify it correctly. Any implementation will be helpful.

like image 641
Sumit Gulati Avatar asked Jul 04 '16 23:07

Sumit Gulati


People also ask

What is atomic counter in DynamoDB?

You can use the UpdateItem operation to implement an atomic counter—a numeric attribute that is incremented, unconditionally, without interfering with other write requests. (All write requests are applied in the order in which they were received.) With an atomic counter, the updates are not idempotent.

Does DynamoDB auto generate ID?

DynamoDB does not support auto-incrementing IDs for items. They can be a performance bottleneck at scale, and most workloads are better off with UUIDs anyway. However, there are use cases where auto-incrementing integers are required.

Does Amazon DynamoDB support both increment and decrement atomic operations?

Does Amazon DynamoDB support both increment and decrement atomic operations? No, neither increment nor decrement operations.

Does DynamoDB support in place atomic updates?

Q: Does DynamoDB support in-place atomic updates? Amazon DynamoDB supports fast in-place updates. You can increment or decrement a numeric attribute in a row using a single API call. Similarly, you can atomically add or remove to sets, lists, or maps.


2 Answers

You have two options, using dynamo db atomic counters or using optimistic locking at the time of incrementing the counter.

Documentation for atomic counters: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters

Documentation for optimistic locking: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html

like image 105
Shibashis Avatar answered Sep 28 '22 16:09

Shibashis


Like other SOers rightly pointed out, you can either use AtomicCounters or ConditionalUpdate. NOV 2018 onwards you have Transactions from DynamoDB (Disclaimer: Havent tested transactions personally, not sure it applies for the auto-increment usecase)

This is a quote from the official doc on AtomicCounter

you can use the UpdateItem operation to implement an atomic counter—a numeric attribute that is incremented, unconditionally, without interfering with other write requests. (All write requests are applied in the order in which they were received.) With an atomic counter, the updates are not idempotent. In other words, the numeric value increments each time you call UpdateItem.

You might use an atomic counter to track the number of visitors to a website. In this case, your application would increment a numeric value, regardless of its current value. If an UpdateItem operation fails, the application could simply retry the operation. This would risk updating the counter twice, but you could probably tolerate a slight overcounting or undercounting of website visitors.

Transactions

Two new DynamoDB operations have been introduced for handling transactions:

TransactWriteItems, a batch operation that contains a write set, with one or more PutItem, UpdateItem, and DeleteItem operations. TransactWriteItems can optionally check for prerequisite conditions that must be satisfied before making updates. These conditions may involve the same or different items than those in the write set. If any condition is not met, the transaction is rejected.

TransactGetItems, a batch operation that contains a read set, with one or more GetItem operations. If a TransactGetItems request is issued on an item that is part of an active write transaction, the read transaction is canceled. To get the previously committed value, you can use a standard read.

like image 42
so-random-dude Avatar answered Sep 28 '22 16:09

so-random-dude