Understanding DynamoDB Write Capacity Units: PutItem vs UpdateItem
Tags: dynamodb, aws, serverless, database
Published: 2024-03-02 14:00:00

**Introduction**

DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS). When using DynamoDB in provisioned capacity mode, you need to specify the required read and write capacity units when creating a table. Understanding how these capacity units work is key to designing cost-effective and high-performance DynamoDB applications. 

In this article, we'll focus on write capacity units and how they relate to two critical write operations in DynamoDB - PutItem and UpdateItem. We'll look at how item size and data patterns affect capacity unit consumption, along with considerations around concurrency and network usage.

**Write Capacity Units Overview** 

DynamoDB allocates throughput capacity in units of read and write capacity:

- One write capacity unit allows one write per second for an item up to 1KB in size. 

- For items larger than 1KB, DynamoDB rounds up the size to the next 1KB increment. For example, writing a 2.5KB item will consume 3 write capacity units.

- Write capacity is provisioned at the table level based on your expected peak needs.

**PutItem vs UpdateItem**

Now let's compare how PutItem and UpdateItem consume write capacity units:

PutItem:

- Overwrites an entire item if it already exists, otherwise writes a new item.

- Consumes capacity units based on the larger of the new item size or old item size (if overwritten).

- Requires sending the full item data in the request.

UpdateItem: 

- Updates specific attributes of an existing item.

- Consumes capacity units based on the larger of item size before and after the update. 

- Only modified attributes need to be sent in the request.

**Example Scenario** 

Consider a Users table with attributes like UserID, Name, Email, Address. Each item is 1KB in size. 

If we call PutItem to overwrite a user item, it will consume 1 write capacity unit since the item size is 1KB. 

If we call UpdateItem to only change the Email attribute, it will still consume 1 write capacity unit because the larger item size before and after update is 1KB.

Key Differences

- With UpdateItem, only changing data needs to be sent over the network compared to the full item with PutItem. This can improve efficiency and reduce costs.

- PutItem risks overwriting other attributes updated concurrently by a different process, leading to lost updates. UpdateItem directly modifies specific attributes avoiding this issue. 

- UpdateItem provides atomic updates avoiding conflicts when incrementing/decrementing values or manipulating sets.

---


When to Use Each 

- Use PutItem when replacing the entire item, such as when data needs to be re-loaded periodically.

- Use UpdateItem for targeted updates to specific attributes, especially if updates are frequent.

- Combine both as needed - use PutItem less frequently to reload entire items, and use UpdateItem to modify portions of those items in between.

**Concurrency Considerations** 

With increased concurrent access, the risk of lost updates or race conditions rises. Strategies to handle this:

- Use conditional writes to fail an update if certain conditions are not met, implementing an optimistic locking approach.

- Use DynamoDB Transactions to coordinate updates across multiple items atomically.

- Structure items to isolate frequently updated attributes, minimizing item sizes being overwritten. 

- Implement business logic retries/backoff to handle update conflicts.

By understanding how PutItem and UpdateItem consume write capacity and addressing concurrent access patterns, you can optimize DynamoDB usage for efficiency, performance and cost-effectiveness.

Conclusion

DynamoDB write operations carry implications for throughput provisioning, network usage, data consistency, and application design. By considering how PutItem and UpdateItem differ in their consumption of write capacity units, you can choose the most appropriate operation for your data and access patterns.