Item Writes

Items with the same partition key are physically collocated in the same partition. Items are written to partition in sort order based on the sort key.

Write operation is atomic, meaning that all attributes writes in the operation will either succeed or fail. You MUST provide the values for table’s key attributes for write.

In case of simple primary key, you need to provide the Partition Key. and in case of composite primary key, you need to provide the Partition Key and Sort Key

Keep in mind that write will fail if item size is > that 400 KB.

Table metadata

DynamoDB service sets up the metadata for the table. The metadata has bunch of information about the table such the Partition Key attribute, Sort Key attributes etc. It also has a data structure that manages the mapping between the hash of the item’s Partition Key and the physical partition in which the item is stored.

Illustration depicts the hash-partition mapping for a table with a simple primary key. The Hash-A reperesents the has value for the item A’s primary key.

metadata-simple-pk

Write under the hood

  1. API calculates the Hash for item key
  2. Checks if item exists in the metadata hash-to-partition mapping
  3. If item already exists then it is updated
  4. If item does not exist then it is inserted
  5. Mapping data updated to record the partition information for the item

Items with common Partition Key

In case of a Composite Primary Key there will be multiple items with a common partition key. All items with the same partition key are mapped to the same partition. The item data is written physically in sort order, with the Sort Key as the key.

metadata-simple-pk

Data Durability

Data durability is acheieved by replicating the partition data across multiple availability zones. All write operations against the table are directed to the primary partition.The group of partitions in a group is referred to as the Replication Group.

replication-group

High availability

In case of failure of primary partition, one of the secondary partition becomes the primary. The illustration below depicts a scenario in which AZ-a became unavailable and partitions in AZ-b took over the role of primary. The process of partition failover and recovery is transparent to the users.

partition-failover