Amazon DynamoDB
Developer Guide (API Version 2011-12-05)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Working with Items in Amazon DynamoDB

In Amazon DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute value can be a number, a string, a number set, or a string set. When you add an item, the primary key attribute(s) are the only required attributes. For more information, see Amazon DynamoDB Data Model . In addition to the primary key, an item can have any number of attributes, although there is a limit of 64 KB on the item size. An item size is the sum of lengths of its attribute names and values (binary and UTF-8 lengths). So it helps if you keep attribute names short.

Except for the primary key, there is no predefined schema for the items in a table. For example, to store various product information, you can create a ProductCatalog table and store various product items in it such as books and bicycles. The following table shows two items, a book and a bicycle, that you can store in ProductCatalog table. Note that the example uses JSON-like syntax to show the attribute value.

Id (Primary Key)Other Attributes
101
{                                        
   Title = "Book 101 Title"
   ISBN = "111-1111111111"
   Authors = "Author 1"
   Price = -2
   Dimensions = "8.5 x 11.0 x 0.5"
   PageCount = 500
   InPublication = 1
   ProductCategory = "Book" 
}                                    
201
{
   Title = "18-Bicycle 201"
   Description = "201 description"
   BicycleType = "Road"
   Brand = "Brand-Company A"
   Price = 100
   Gender = "M"
   Color = [ "Red", "Black" ]
   ProductCategory = "Bike"
}

Amazon DynamoDB provides API to add, retrieve, update, and delete items. In addition, it also provides an API to retrieve a batch of items from one or more tables. The create, update, and delete APIs support conditional update. For example, when you put an item and if the item exists, Amazon DynamoDB replaces that item, by default. However, you can use the "exists" condition to determine whether you want the item replaced. Similarly, when you delete an item, you might want Amazon DynamoDB to display certain attribute values before deleting an item.

Conditional Writes

In a multi-user environment it is possible that multiple clients access the same item and attempt to update its attiribute values at the same time not realizing that the item has been updated by some other client. This is shown in the following illustration in which client 1 and client 2 get a copy of an item (Id=1). Client 1 updates the price from $10 to $8. Later client 2 updates the same item price to $12 and the update made by client 1 is lost.

To help clients coordinate updates, Amazon DynamoDB supports conditional writes in which case it applies the update only if the current item attribute(s) meet the specified condition(s). When you send a conditional write request, Amazon DynamoDB performs the operation only if the specified condition is met, otherwise it returns an error. For example, the following illustration shows a conditional update where Client 1 and Client 2 both get a copy of an item (Id=1). Client 1 first updates the item price to $8 with a condition that the existing item price on server must be $10 and the operation succeeds. Client 2 then attempts to update price to $12 with a condition that the existing item price on the server be $10 and the operation fails.

Note that the conditional update is an idempotent operation, you can send the same request multiple times and it has no further effect on the item after the first time Amazon DynamoDB performs the specified update. For example, suppose you send a conditional update request to update the price of a book item by 10% only if the current price is $20. However, before you get a response, a network error occurs and you don't know whether your request was successful or not. Because a conditional update is an idempotent operation, you can send the same request again and Amazon DynamoDB updates the price only if current price is still $20.

Atomic Counters

Amazon DynamoDB also supports atomic counters that allow you to increment or decrement the value of an existing attribute without interfering with another simultaneous write requests. You can use the Amazon DynamoDB update API to increment/decrement an attribute value, instead of replacing the value. For example, a web application might want to maintain a counter per visitor to their site. In this case, the client wants to increment a value regardless of what the current value is. Note that, unlike the conditional update operation described in the preceding section, this is not an idempotent operation. That is, you might retry this operation in the event you don't know if your previous request was successful or not; however, you risk applying the same update twice. So this feature is great for a web site counter because you can tolerate with slightly over or under counting the visitors. However, in a banking application, it would be safer to use conditional updates.

Consistent vs Eventually Consistent Read

Amazon DynamoDB maintains multiple copies of each item to ensure durability. For each successful write request, Amazon DynamoDB ensures that the write is durable on multiple servers. However, it takes time for the update to propagate to all copies. The data is eventually consistent, meaning that your read request immediately after a write might not show the change. You can optionally request the most up to date version of the data, which takes additional read capacity units for Amazon DynamoDB to get data for the latest item. An eventually consistent get request consumes half the read capacity units as the consistent request. So it is good to design applications to take advantage of eventual consistent read where possible. Consistency across all copies of the data is usually reached within a second.

Capacity Units Consumed by Various Item Operations

When you create a table you specify your read and write capacity unit requirements. When you send requests such as get, update or delete an item, you consume the capacity units set for the table. For more information about how Amazon DynamoDB computes the capacity units consumed by your operation, see Capacity Units Calculations for Various Operations.

You can use the Amazon DynamoDB API to work with items or use the AWS SDK libraries for Java, .NET, PHP, and Ruby. Click one of the links provided at the beginning of this section to learn more about how the specific AWS SDK library APIs support the item operations. All these sections provide working samples.

To learn more about Amazon DynamoDB items and other data model concepts, see Amazon DynamoDB Data Model .