DynamoDB Conditional Operations and Atomic Counters

Updating items in DynamoDB has two special cases - Conditional Operations and Atomic Counters.  

 

Conditional Operations and Expressions

By default, the DynamoDB write operations (PutItem, UpdateItem, DeleteItem) are unconditional: each of these operations will overwrite an existing item that has the specified primary key. In certain cases such as multiple users attempting to modify the same item, this might create problems.

With conditional operations, you can specify a condition that must be satisfied for a put, update, or delete operation to be completed. Conditional operations allow users to implement optimistic concurrency control systems on DynamoDB.

A ConditionExpression can combine multiple conditional clauses, including nested clauses. A ConditionExpression is constructed from the following:

  1. Boolean functions: ATTRIBUTE_EXIST, CONTAINS, and BEGINS_WITH

  2. Comparison operators: =, <>, <, >, <=, >=, BETWEEN, and IN

  3. Logical operators: NOT, AND, and OR.

You can specify an expression as part of the Query API call to filter results based on values of primary keys on a table using the KeyConditionExpression parameter. You can use expressions for both partition and partition-sort keys.

Conditional writes are idempotent. Read more here

 

Atomic Counters

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 will increment each time you call UpdateItem.

Read more here.

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream