Add non existing element to array in DynamoDB
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Amazon DynamoDB is a fully managed NoSQL database service by AWS, which provides fast and predictable performance with seamless scalability. It is optimized for applications that require high throughput and low latency. One of the data types supported by DynamoDB is the "Set," which can be a useful tool for various operations, including maintaining unique collections or performing set-based operations such as adding or removing items. This article will focus on how to add non-existing elements to a Set in a DynamoDB, particularly within arrays or object lists, ensuring that we maintain the uniqueness property that the Set guarantees.
Adding Non-Existing Element to an Array in DynamoDB
When you work with arrays (lists) in DynamoDB, the operation to add a non-existing element is not as straightforward as the "Put" operation for single items. Here's a detailed walkthrough of how to achieve this.
Using DynamoDB Expression to Add a Non-Existing Element
In DynamoDB, you can use the ADD operation in combination with DynamoDB expressions to ensure that an item is added only if it does not already exist. The ADD command is specifically designed for numeric and set data types and ensures the uniqueness constraint of Sets.
Example: Using an ADD operation on a Set
Consider a DynamoDB table named Users with a primary key as UserID, which stores user information along with a Skills attribute of type SS (String Set). The goal is to add a new skill to the user's skill set without introducing duplicates.
Use Case:
To add a new skill, "Java", to the Skills set:
In this example, the ADD operation ensures that "Java" is added to the Skills set only if it is not already present.
Considerations
- Idempotency: By using the set data type with the
ADDoperation, the operation becomes idempotent, meaning multiple executions lead to the same result. This is beneficial in reducing accidental duplications. - Error Handling: Ensure proper error handling is in place, especially for conditional checks, to provide feedback when operations fail.
- Data Types: Remember that
ADDworks efficiently with Set and Number data types, but not with List or Map types.
Key Points Summary
| Feature/Process | Details |
ADD Operation | Works efficiently with Set and Number data types |
| Ensures Uniqueness | By using ADD, duplicates are automatically avoided for sets |
| Idempotency | Multiple executions reach the same final state due to set properties |
| Error Handling | Important for managing exceptions and providing meaningful feedback |
| Data Type Limitations | Use with SS (String Set), NS (Number Set), not compatible with List |
Additional Topics
Conditional Writes
When you're working to maintain consistency and avoid overwrites or concurrent write conflicts, using conditional expressions inside your update_item operations can provide additional safeguards.
Example of a Conditional Update
Limitations
While the ADD operation is powerful for sets, for Lists, developers must manually check for existence before appending an element to the list. This involves reading the item, modifying it in the client application, and writing it back to ensure item-level atomicity and integrity.
Conclusion
Adding a non-existing element to a Set in DynamoDB requires using the ADD operation, which effectively prevents duplication while maintaining atomic operations. By understanding and utilizing these features, developers can take full advantage of DynamoDB's performance capabilities and robust features in their applications.

