Passer au contenu

DynamoDB Atomic Counters

0

Greetings!

I have an application that utilizes a DynamoDB table to manage counters. When we update the value of the counter, we consume the returned value - this returned value is the old value of the counter. This counter is then used to retrieve a value in an array, it is effectively an index.

I currently increment these counters with Lambda functions - it's highly likely that many Lambdas may attempt to update the value of a given counter at the same time. It's important that each Lambda function receives a different value of the counter.

Utilizing DynamoDB atomic counters, can I expect that each Lambda function will receive a different value for the counter?

Example:

  • 10 Lambdas attempt to update value of a counter where its current value is 0
  • The Update Expression is "ADD count :incr" where ":incr" is 1
  • Each Lambdas update is processed
  • The returned value from each update is unique per Lambda and the values are 0 - 9
  • The final value of the counter is 10
demandé il y a 4 ans11,1 k vues
2 réponses
2

Yes, this is exactly how atomic counters work. You can find more info here

AWS
EXPERT
répondu il y a 4 ans
AWS
EXPERT
vérifié il y a 4 ans
0

It is true that with an atomic counter the updates do not interfere with each other. At the same time you must be aware that the updates are not idempotent.

I the case in which the UpdateItem is unsuccessful, and you get a Network Error for example, you can't be sure that your update was executed or not. Then you probably will retry from your lambda function and the possibility of overcounting exists. So you may end up with the final value of your counter greater than 10 in your example.

If that is acceptable, then great! For scenarios where accuracy is important I advice using transaction with a client request token!

répondu il y a un an

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.