ISO 8601 Strings vs Unix Epoch for Time-Series Sort Keys
0 reputation · 24 Aug 2020, 18:42 UTC
0 reputation · 24 Aug 2020, 18:42 UTC
When designing a DynamoDB table for time-series data, the lack of a native date type requires a choice between storing timestamps as ISO 8601 strings or Unix Epoch numbers. The primary goal is to maintain efficient range queries while ensuring data remains manageable.
ISO 8601 strings provide human-readability and native lexicographical sorting, which simplifies debugging via the AWS Console. However, Unix Epoch timestamps offer a smaller storage footprint and are the only format compatible with the DynamoDB Time to Live (TTL) feature for automatic item expiration.
If a design requires both chronological sorting for queries and automated expiration of old records, the trade-off involves either managing two separate attributes or sacrificing readability for storage efficiency.
Using a numeric Unix Epoch timestamp as a Sort Key does not significantly impact query performance compared to an ISO 8601 string. In DynamoDB, both numeric and string types support efficient range queries (e.g., BETWEEN, >, <) with similar latency and throughput characteristics.
While performance is comparable, the functional capabilities differ based on the data type:
YYYY-MM-DDTHH:mm:ssZ). Mixing timezones or varying the precision of fractional seconds will break the chronological sort order.It is architecturally sound to store a redundant ISO 8601 attribute for readability while using a numeric Epoch for the Sort Key and TTL. This pattern separates the operational requirement (efficient sorting and automated cleanup) from the observability requirement (human-readable debugging).
To implement this, follow these steps:
timestamp_iso) as a String for display purposes.To verify the behavior of your chosen implementation, you can use the AWS CLI to test range boundaries:
# Example: Querying a range of epoch timestamps
aws dynamodb query \
--table-name YourTimeSeriesTable \
--key-condition-expression "PartitionKey = :pk AND SortKey BETWEEN :start AND :end" \
--expression-attribute-values '{ ":pk": {"S": "sensor_01"}, ":start": {"N": "1700000000"}, ":end": {"N": "1700086400"} }'
Note: To provide a more specific recommendation on storage costs, please clarify the expected volume of items per partition and the total item size.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.