DynamoDB
AWS
Conditional Write
NoSQL
Database Operations

How to add item to dynamodb if a field does not exist or matches a condition?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

DynamoDB is a NoSQL database service provided by AWS that offers seamless scalability, low latency, and high throughput for serverless applications. One of the powerful features of DynamoDB is its conditional operations, which allow you to add or update items only if certain conditions are met. In this article, we will explore how to add an item to a DynamoDB table only if a specified attribute does not exist or matches a particular condition.

Conditional Operations with DynamoDB

DynamoDB's PutItem and UpdateItem operations can be extended with conditions to avoid overwriting existing data. This is done using the ConditionExpression parameter, which enforces certain conditions that must be satisfied for the operation to succeed.

Using PutItem with Condition

The PutItem operation can be used to add a new item to a DynamoDB table. To use this operation conditionally, you can specify a ConditionExpression to add the item only if a specific attribute does not already exist. The attribute_not_exists function is especially useful for this purpose.

Example: Adding an Item if a Field Does Not Exist

Suppose you have a table named Users with a primary key UserId, and you want to add a new user only if their email address does not exist in the table.

python
1import boto3
2from botocore.exceptions import ClientError
3
4# Create a DynamoDB client
5dynamodb = boto3.resource('dynamodb')
6
7# Specify the table
8table = dynamodb.Table('Users')
9
10# Define the item to be added
11item = {
12    'UserId': 'u12345',
13    'Name': 'John Doe',
14    'Email': '[email protected]'
15}
16
17# Define the condition expression
18condition_expression = 'attribute_not_exists(Email)'
19
20# Try to add the item with the condition
21try:
22    response = table.put_item(
23        Item=item,
24        ConditionExpression=condition_expression
25    )
26    print("Item added successfully.")
27except ClientError as e:
28    if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
29        print("Item not added as the condition was not met.")
30    else:
31        print("An unexpected error occurred:", e)

Matching a Specific Condition

If you want to add an item only if an existing item's field matches a specific value, you can use other functions or logical operators in your ConditionExpression.

Example: Adding an Item if a Field Matches a Condition

Suppose you want to add a new user only if their Status field is set to "inactive".

python
1# Define the condition expression
2condition_expression = 'attribute_exists(Email) AND Status = :inactive'
3
4# Define expression attribute values
5expression_attribute_values = {
6    ':inactive': 'inactive'
7}
8
9# Try to add the item with the condition
10try:
11    response = table.put_item(
12        Item=item,
13        ConditionExpression=condition_expression,
14        ExpressionAttributeValues=expression_attribute_values
15    )
16    print("Item added successfully.")
17except ClientError as e:
18    if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
19        print("Item not added as the condition was not met.")
20    else:
21        print("An unexpected error occurred:", e)

Key Concepts

Here are some key concepts and functions used in condition expressions for DynamoDB:

ConceptDescription
attribute_not_existsChecks if an attribute does not exist.
attribute_existsChecks if an attribute exists.
=Verifies if an attribute matches a specific value.
AND, OR, NOTLogical operators for combining multiple conditions.
ExpressionAttributeValuesMap of placeholders to the actual values used in expressions.

Handling Conditional Check Failures

When a conditional check fails, DynamoDB raises a ConditionalCheckFailedException. You should handle this exception appropriately in your application to ensure users are informed about the failure without causing disruptions.

Subtopics for In-depth Understanding

Attribute Value Types

Ensure that the values in your ExpressionAttributeValues map correctly match the data types of the attributes in your table. DynamoDB supports several data types, including String, Number, Binary, Boolean, Map, List, among others.

Optimistic Locking with Conditional Writes

Conditional operations can also be used for implementing optimistic locking in DynamoDB. By including a version number or timestamp in your condition expressions, you can prevent conflicts and ensure sequential updates to an item.

Cost Implications

While conditional operations are invaluable for data integrity, be mindful of their cost implications. Each conditional write that fails still incurs read capacity unit costs since DynamoDB needs to read the item to check the condition.

In conclusion, DynamoDB's conditional expressions offer robust mechanisms for handling atomic operations in your applications. Whether you're ensuring data integrity through existence checks or complex condition matching, understanding how to leverage these features effectively can significantly enhance the reliability and predictability of your applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design