Things to remember about AWS DynamoDB
This short compendium will help in preparing for the AWS certification exams
Indexes
A secondary index allows you to perform more flexible querying on DynamoDB. It allows you to query on an attribute which is not the primary key. This can be done using
- Global Secondary Index
- Local Secondary Index
You select the columns you want to include in the index and run your searches on the index instead of the entire dataset.
Scan and Query API calls
QUERY
A query operation finds items in your table based on the primary key attribute, and a distinct value to search for. A query can be refined by using an optional sort key name and value. By default a query returns all the attributes. You can limit the attributes to the specific attributes you want by using the ProjectionExpression
parameter.
Results are always sorted by the sort key.
You can reverse the order of the result by setting the ScanIndexForward
attribute to False.
REMEMBER: ScanIndexForward
is applicable only to queries and NOT to scans, despite the name
By default all queries are eventually consistent.
SCAN
A scan operation examines every attribute in the table. Again, you can use the ProjectionExpression
parameter to limit the attributes returned.
A query is more efficient than Scan.
Improving performance
- Set smaller page size
- Avoid scans if possible
- By using parallel scan. A scan by default operates in a sequential manner. But you can configure your DynamoDB to use parallel scans by logically dividing a table or index into segments and scanning each segment in parallel.
- Isolate scan operations to specific tables and segregate them from mission critical data
Example 2:
Imagine you have an application that needs to read 80 items per second. Each item is 3KB in size and you need strongly consistent reads. How many read capacity units will you need?
Ans:
Number of 4KB read capacity units needed per item = 3KB/4KB = 0.75 ~= 1
Number of items = 80
Therefore read capacity units needed = 80 x 1 = 80 for STRONGLY CONSISTENT
What if you need Eventually consistent reads?
= #Strongly Consistent / 2
= 80/2
= 40 for EVENTUALLY CONSISTENT
Example 3:
Imagine you need to write 100 items per second to your DynamoDB table. Each item size 512 bytes. Each write capacity unit gives 1 1KB write per second. How many write capacity units will you need to provision?
Ans:
- Number of write capacity of 1KB/sec needed per item = 512/1024 bytes = 0.5 ~= 1 write capacity unit per item
- Number of writes required per second = 100
- Therefore, number of write capacity units required = 100 x 1 write capacity unit per item = 100
DynamoDB Accelerator (DAX)
It's a fully managed, clustered in memory cache for DynamoDB.
But only for Read performance. Ideal for read heavy or bursty read applications.
DAX is a write through caching service.
Data is written to the cache and the backend store at the same time
This allows you to point your DynamoDB API calls at the DAX cluster
*Limitations*
1. It caters to applications that need Eventual Consistency. Not suitable for Strongly consistent read needs
2. Not suitable for write-intensive applications
DynamoDB Streams
it's an ordered sequence of item level modifications.
These are stored as logs. These logs are encrypted at rest and stored for 24 hours only.
They can be used for triggering events based on certain transactions. Great for serverless architectures.
They can also be used for replicating data across multiple tables.