What are Conditional Writes in AWS DynamoDB

Munish Chandel | July 28, 2018 at 10:17 AM | 99 views | Amazon DynamoDB

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. DynamoDB provides mechanism to implement conditional writes that ensures no other write request interferes with the current operation. Conditional writes are of great importance for banking application that do not tolerate over-counting or under-counting.

First of all we need to configure properties for accessing dynamoDB ./src/main/resources/application.yml

  accessKey : <your-aws-access-key>
  secretKey : <your-aws-secret>
  region : <dynamo-db-region>
  dynamodb :

Step 2. Configure DynamoDB

We need to create a bean for AmazonDynamoDBClient and DynamoDB so that we can utilize them as and when required.

public class DynamoConfig {
    private static final Logger logger = LoggerFactory.getLogger(DynamoConfig.class);

    AmazonDynamoDBClient dbClient(@Value("${aws.dynamodb.endpoint}") String amazonDynamoDBEndpoint, AWSSettings awsSettings) {
        AmazonDynamoDBClient dbClient = new AmazonDynamoDBClient(
                new BasicAWSCredentials(awsSettings.getAccessKey(), awsSettings.getSecretKey()));
        logger.info("DB Endpoint is " + amazonDynamoDBEndpoint);
        if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
        if (awsSettings.getRegion().equalsIgnoreCase("local")) {
        return dbClient;

    DynamoDB dynamoDB(AmazonDynamoDBClient client) {
        return new DynamoDB(client);

Step 3. Create DAO layer

DAO layer for condtional writes utlizes UpdateExpression and ConditionExpression as shown in the below code.

Conditional Write based on current value of attribute
public void incrementViewsUsingConditionalExpression(String contentId, long newValue, long expected) {
    Table table = dynamoDB.getTable("local-content");
    UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("id", contentId)
            .withUpdateExpression("SET #fn = :newval")
            .withConditionExpression("#fn = :currval")
            .withNameMap(new NameMap()
                    .with("#fn", "views")
            .withValueMap(new ValueMap()
                    .withNumber(":newval", newValue)
                    .withNumber(":currval", expected)

    UpdateItemOutcome outcome = table.updateItem(updateItemSpec);

The above code will only update attribute named views to newval if expected value is equal to currval.

That’s all

Download Source

You can download source code for this article from Git Repository

Buy DRM Free PDF for Complete Collection of Interview Questions
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers

This ebook discusses 120 plus real problems and their solutions for Spring microservices architecture based on Spring Boot, Spring Cloud, Cloud Native Applications. It covers core concepts of microservices architecture, various design patterns, interview questions & answers, security in microservices, testing strategies and best practices in distributed system design.

Free Email Updates
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Similar Articles:
  1. What is Eventual Consistency in DynamoDB?
  2. How to implement Atomic Counters in DynamoDB for high throughput
  3. How to automatically Retry DynamoDB Write on ProvisionedThroughputExceededException
  4. Amazon DynamoDB Java Interview Questions
  5. How will you ensure that no two threads update the same db record in parallel in amazon DynamoDB
  6. What are Best Practices for Using Amazon DynamoDB?
  7. Implementing Adhaar Card Service using DynamoDB
  8. scan all records of a Amazon DynamoDB table using a Java Code
  9. What are Conditional Writes in AWS DynamoDB

This website uses cookies to ensure you get the best experience on our website. more info