I see that DynamoDB is essentially limited to three data types: String, Number, and Binary. That seems to leave two options for storing a date or timestamp:
I will need to 'sort' results by using this date/timestamp as a range key and using scanIndexForward. Is it possible to say which approach would be more efficient?
This takes out the guesswork of provisioning read and write units and you simply pay for what you use. All that to say, DynamoDb can work well for time series data, and can be much faster at querying the data, but you need to carefully consider how you need to access it.
There are multiple ways to represent a timestamp in DynamoDB. Probably the most common is to use a Number type to represent the timestamp with the value as a Unix timestamp (seconds or milliseconds). Additionally, you can store the timestamp as a String type with the value as an ISO 8601 formatted string.
DynamoDB supports eventually consistent and strongly consistent reads. When you read data from a DynamoDB table, the response might not reflect the results of a recently completed write operation.
Amazon DynamoDB Time to Live (TTL) allows you to define a per-item timestamp to determine when an item is no longer needed. Shortly after the date and time of the specified timestamp, DynamoDB deletes the item from your table without consuming any write throughput.
I don't know that it's necessarily more efficient, but we have had good success using a Unix timestamp as range keys. It's been fast (under 20ms latency, sometimes under 10ms) for queries and lets us sort "earliest to latest" or "latest to earliest" using the scanIndexForward
flag.
The downside is that when looking at the raw tables it can be tough to compare two timestamps at a glance, but it's easy enough to convert a timestamp into a date-type in your language of choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With