amazon-web-servicesspring-bootamazon-dynamodbttl

Would DyanomoDB delete item immediately based on TTL, how can I achieve deleting an item from table after 5 minutes?


I have a login session valid for 5 minutes from the last activity. If there is any activity I update the time-to-live attribute in the item by adding 5 minutes, otherwise it should get deleted at time set in 'expireAt'. But why does the item in dynamoDB not get deleted as per the time-to-live set in item 'expireAt' ?

In Java Spring-Boot, I have set EPOCH time in seconds as mentioned in the documentation login.setExpireAt((System.currentTimeMillis() / 1000) + 300);. It does not delete item in table after 5 minutes. What alternate approach can I adopt to work around this ?


Solution

  • But why does the item in dynamoDB not get deleted as per the time-to-live set in item 'expireAt'

    DynamoDB TTL works on a best effort basis as it provides free deletes for your table. If you item has surpassed its expiry time then it is eligible for deletion eventually, but that could be mins/hours/days in the future. If you need more strict deletions you have some choices:

    1. Create your own TTL eviction with EventBridge Scheduler
    2. Use a filter expression on reads, which filters out items that have surpassed the expiry time and should be evicted
    3. Create your own TTL sweeeper that periodically reads and deletes items from DynamoDB.