DEV Community

Cover image for Alternative to Amazon DynamoDB Time to Live (TTL)
Prabusah
Prabusah

Posted on • Updated on

Alternative to Amazon DynamoDB Time to Live (TTL)

Scenario:

  • Certain data lose relevance after a specified time.
  • So create records in datastore that should exists only for a specified time from the creation time.
  • Time can be anything from 1 second to hours.

Example Case: Make the record expire within 10 seconds of its creation time.

Option 1: Application based timer (Not Recommended)
Whenever Application creates a record it has to trigger a timer as shown below JavaScript example timer:

/*
setTimeout's not the right way to keep track of timer... Let's assume it works for explaining this option.
*/
setTimeout(function() {
    purgeRecord(recordId);
}, 10000);
Enter fullscreen mode Exit fullscreen mode

Problem: An Application receiving thousands of request per second has to create those number of timers. This option is not scalable.

Option 2: Amazon DynamoDB - (Not Recommended)
DynamoDB per-partition scanner background process automatically and continuously evaluates the expiry status of items in the table.

Problem:
Amazon DynamoDB TTL typically deletes expired items within 48 hours of expiration. Scan & Filter by timestamp not a scalable option.
This option works best if your TTL is minimum 48 hours.
Not matching with our scenario of expiring items in 10 seconds.

Option 3: Amazon Elasticache (Recommended)
Redis compatible Amazon Elasticache honours TTL by seconds.
Code example:

redis_client.set('key', 'value', {
  EX: 10
});
Enter fullscreen mode Exit fullscreen mode

Matches with our 10 seconds expiring scenario.

Please let me know your thoughts or other options in comments.

Image by Sadia from Pixabay

Top comments (0)