DynamoDB: Come On In, the Water's Fine

What is Amazon DynamoDB

It’s a fully managed, non-relational database. You won’t need to spin up any database servers, install patches or worry about scaling and disk space. DynamoDB does it all for you. It will also replicate your data across three separate data centres giving you amazing data durability and service availability.

Superb Freebies

DynamoDB is a generous soul. It won’t charge you a penny for your first 25 GB worth of indexed data. Nor do you pay for your first few million requests each month (depending on the amount of data read or written for each request).

Enough Talk, Let Us Tinker

Assuming you have an AWS account with a default profile setup locally, then you’ll be able to create your first table in a matter of seconds:

We created a table called Todos, with the create-table operation, and we passed several options: first, we have --table-name, used unsurprisingly for defining the table’s name. Then, --attribute-definitions and --key-schema, which are necessary for defining the table’s primary key.

This table has a ‘simple’ primary key, in that it consists of a single part - the partition key (also called the hash key). DynamoDB also supports composite primary keys, made up of a partition key and a sort key - read more here.

Next, we define the table’s throughput capacity (--provisioned-throughput) for write and read operations. One read capacity unit can read up to 8 KB per second and one write capacity unit can write up to 1 KB per second. These values aren’t fixed when the table is created and can be increased or decreased as demand changes. DynamoDB also supports auto scaling, where your table’s throughput is managed for you, depending on how busy your table is.

DynamoDB is schema-less, so the number of attributes and their types can vary across items in a table. The one required attribute is the primary key, and this must be unique across all items in a table.

With the --item option, we passed the item’s attributes and values as JSON. Notice that for each attribute we have a name and an object containing the value and its type:

"Key":{"Type":"Value"}#e.g."completed":{"BOOL":false}

DynamoDB items support various attribute types, including number (N), string (S), boolean (BOOL), binary (B) and null (NULL). Collection types are also supported - set (number, string and binary), list, and map.

Reading an Item

With the get-item operation we can read an item with a particular primary key.

{"Count":2,"Items":[{"completed":{"BOOL":false},"description":{"S":"Pet the horse"},"id":{"N":"2"}},{"completed":{"BOOL":true},"description":{"S":"Feed the pet tortoise."},"id":{"N":"1"}}],"ScannedCount":2,"ConsumedCapacity":{"CapacityUnits":0.5,"TableName":"Todos"}}

We’ve used scan at its most simplest. It’s a sophisticated operation, and deserves a blog post of its own. For example, scan supports filter expressions, that allow you to describe a subset of items you want returned.