DEV Community

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

Posted on • Edited on

4 3

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs