muhihsan
muhihsan

Reputation: 2350

Scanning DynamoDb while updating some items

When I run scan operation on DynamoDb, I can use LastEvaluatedKey that is returned as part of the response to get the scan the next items available. And I can repeat this process until it doesn't return LastEvaluatedKey anymore and that indicates there is no more items to scan.

My question is, if while in the middle of that scan operation, some items that have been retrieved from the scanning process are updated (put), should I expect those items to appear again?

Upvotes: 4

Views: 840

Answers (1)

Nadav Har'El
Nadav Har'El

Reputation: 13731

No - if you have already retrieved an item, you will not retrieve it again in the same Scan operation even if it is modified. Moreover, if your modification adds a new item, this new item may or may not be returned by the ongoing scan - you don't know.

To understand why this is the case, you need to understand how Scan actually works:

Although DynamoDB doesn't guarantee any sorting order between the partition keys, in their internal implementation there is some order for this partition keys (based on a hash function of the key - this is why these keys are also known in some DynamoDB documentation as hash keys). A Scan iterates over the partition keys in this hash order, and doesn't go back. If the scan passed some position in the hash values, it won't pass it again, and in particular the same item with the same partition key will not be retrieved again, and new items will or won't be retrieved depending on whether the new item's partition key is before or after the current position of the scan.

If you really need to get all new modifications as they happen, you should consider using DynamoDB's "Stream" feature instead of - or in addition to - Scan. DynamoDB's Stream feature lets you read all the modifications to the database as their happen. Some applications combine both a Scan to read existing items, and a stream to read items modified after the Scan started.

Upvotes: 3

Related Questions