Shikasta_Kashti
Shikasta_Kashti

Reputation: 771

Amazon DynamoDB Atomic Writes

I have a list of Lambda worker functions (say 1000), each running simultaneously and doing its job. To be able to figure out the end result of all workers I have come up with this idea.

Before starting the job and spawning the Lambda worker functions, I save a record in DynamoDB, for example two attributes:

On finish of each Lambda worker function it will go and increment the attribute jobs_completed by one. Then read the record and check if total_number_of_jobs equals to jobs_completed and if it is, put a record in SQS.

My questions are:

  1. Is this a good idea?
  2. Would the updates be consistent and atomic? Could there be any race conditions?
  3. Any better solution than this?

I would update the counter, jobs_completed, in an UpdateItem API call like this:

SET jobs_completed = jobs_completed + :incr_by where incr_by would be equal to 1.

Upvotes: 4

Views: 7010

Answers (1)

Mark B
Mark B

Reputation: 200436

As long as you use DynamoDB atomic counters, like your example shows, and you check the return value of the UpdateItem call instead of running a separate query, then your proposed solution should work fine.

Upvotes: 5

Related Questions