Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. - docs.aws.amazon.com
There are are 3 programming interfaces each have their own use case.
The low-level programming model wraps direct calls to the DynamoDB service. You access this model through the Amazon.DynamoDBv2 namespace.
// ---------- client
The document programming model provides an easier way to work with data in DynamoDB. This model is specifically intended for accessing tables and items in tables. You access this model through the Amazon.DynamoDBv2.DocumentModel namespace.
The object persistence programming model is specifically designed for storing, loading, and querying .NET objects in DynamoDB. You access this model through the Amazon.DynamoDBv2.DataModel namespace.
- Object Persistence Model
- Example: Query and Scan in DynamoDB Using the AWS SDK for .NET Object Persistence Model
// ---------- client and di
Where the class
MyTableSchema is defined below. Note that
DynamoDBHashKey is the partition key.
Optionally we can add
DynamoDBRangeKey as a sort key.
DynamoDBHashKey,DynamoDBRangeKey and DynamoDBProperty can also include enums using
IPropertyConverter implemented in
Two types of Primary Keys and their valid data types are
Binary. All the examples below use the table
Artists Partition/Primary/Hash key by itself, it must be unique in the table.
Combination of partition Key and a sort/range key which makes it a composite key. Individually, the partition key and the sort/range key does not need to be unique, but the combination of both which makes it into a primary key needs to be unique.
Artists Partition/Primary/Hash key and
Songtitle Sort key. This allows for a query like
Artists = 'Celine Dion' & Songtitle = 'My Heart Will Go On'
When you need to query data and the current
Partition/Primary/Hash key and/or
sort/range keys are not enough - and you dont want to perform a db scan (ie: loop though all the data in the table) the you can add additional indexes.
- Simple or composite
- Can be created after the table is created
- Max GSI per table is 20
- Results in additional writes (cost)
- Keep WCU (write capacity units) on GSI >= WCU on main table
An index with a partition key and sort key that can be different from those on the table. If you need to query on
Genre you would create a
GenreIndex and then query on
Genre is now a partition key for
GenreIndex (its a new table that is automagically kept in sync) - anything needed to be returned when querying on the index is defined with
Projection. If you ask for attributes not in the projection dynamo will do a Fetch, the default Projection is ALL.
// Music Table
- Only composite
- Must be created with the table
- Esentially extends the functionality of the
sort/range keyto other attributes so you can perform more optimal queries
- You need to know the
- Soft limit of 5 LSI
- There is no additional cost for LSI’s
An index that has the same partition key as the table, but a different sort key. So when the LSI has
Artists as partition key and
Year as sort key, you can get a list of records with
Artists = 'Celine Dion' & Year > 2000
// Music Table
The SDK provides some settings that we can simply turn on/off. Consistent Read is one of them.
Eventually consistent reads - By default is when you read data from a table, the response might not reflect the results of a recently completed write operation. The response might include some stale data. If you repeat your read request after a short time, the response should return the latest data.
Strongly consistent reads - When you request a strongly consistent read, DynamoDB returns a response with the most up-to-date data, reflecting the updates from all prior write operations that were successful.
but it has a higher latency and it uses more throughput capacity
This can be easily enabled/disabled in the table.
We can add a version number column to our table to enable optimistic locking. When updating the item, it checks whether the version of the item matches the one stored in DynamoDB. If not, it throws exception. If version is the same, DynamoDB updates the record and automatically increments the version number.
Scalar Types represent exactly one value
Can represent a complex structure with nested attributes such as a JSON document.
// List, can be a combination of data types
// Map, complext object
Can represent multiple scalar values, the types in the set need to be the same, else it needs to be a
// String Set
Thew binary sets are base64 encoded.