TTL silent non-deletion with millisecond epoch DynamoDB Time To Live
0 reputation · 05 Nov 2024, 02:13 UTC
0 reputation · 05 Nov 2024, 02:13 UTC
A design needs items to expire at a consistent local midnight in America/New_York and to remain sortable for range queries across daylight saving time transitions.
DynamoDB provides no native datetime or timezone type. Temporal values are stored as String ISO 8601, Number epoch, or decomposed types. TTL evaluates a Number attribute as UTC Unix epoch seconds only; PartiQL and Query/Scan filter expressions have no timezone conversion functions and treat string and number attributes as opaque ordered values. DynamoDB Streams and PITR timestamps are UTC, and cross-region replication preserves stored values verbatim with no timezone metadata.
Storing local wall-clock time with an IANA zone identifier preserves display intent but breaks lexicographic sort and filter semantics on a GSI keyed by string, especially with mixed offsets. Normalizing all writes to UTC preserves sort and TTL correctness but pushes all presentation-timezone conversion to the read path. DynamoDB provides no validation or tooling to enforce consistency between a UTC canonical attribute and a local display attribute.
Is TTL expected to accept epoch milliseconds for expiration, or is epoch seconds the only supported unit? How should a table be modeled to support both correct TTL evaluation in UTC and correct chronological ordering for queries that must respect DST transitions? What guarantees exist for sort order of ISO 8601 strings with differing offsets on a Global Secondary Index sort key?
33275 reputation · 05 Nov 2024, 13:49 UTC
DynamoDB TTL only supports Unix epoch time in seconds. If you store epoch milliseconds, the value is interpreted as seconds, placing the expiration date tens of thousands of years in the future. This results in "silent non-deletion" where items never expire despite appearing to have a valid timestamp.
Because DynamoDB treats attributes as opaque values and lacks timezone-aware functions, you must handle all temporal logic at the application layer before writing to the database. To support a consistent local midnight in America/New_York while maintaining chronological sort order across Daylight Saving Time (DST) transitions, use the following model:
luxon or java.time) to determine the exact UTC instant of the next local midnight in America/New_York.There are no guarantees that ISO 8601 strings with differing offsets will sort chronologically on a Global Secondary Index. DynamoDB sorts strings lexicographically by UTF-8 bytes. For example, a timestamp ending in Z (UTC) will sort differently than one ending in -04:00, even if they represent the same instant. To maintain chronological order in a string-based sort key, you must normalize all values to a single fixed offset (typically UTC/Z).
To diagnose the current state of your TTL attribute, check a sample item via the CLI:
aws dynamodb get-item --table-name YourTableName --key '{"YourPartitionKey": {"S": "ExampleValue"}}'
Observation: If the TTL value is approximately 1.7e12, it is in milliseconds (incorrect). If it is approximately 1.7e9, it is in seconds (correct).
Note: To provide a more specific migration strategy, please confirm if your GSI sort key is currently a String or a Number.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.