skip to Main Content

I want to enable TTL on the DyanmoDB records. However, the TTL should kick in only when the record is updated to a particular state {status: completed}.

Is it possible to first create the record with no TTL in sight (or to set the TTL to a long time in the future) and then to later update it to N days from now when the record is updated to {status: completed}?

2

Answers


  1. Yes, it is doable. I believe status would be one of the columns of the record. The only limitation of this approach would be, you will have to keep the logic of setting up TTL value basis status value inside your service/application. DynamoDB can’t do it on its own.

    You can use the following workaround:

    1. Enable TTL on your table. You can follow AWS’s official link to enable it using the console. Let us say you use expireAfter column as the TTL attribute.
    2. Whenever you are either creating/updating any record with an un-complete status. Then you have two choices
      1. Either set the longer epoch value of expireAfter attribute and save it into DDB.
      2. Second, my favorite. Don’t set or keep the empty value of expireAfter attribute and save it into DDB. During internal records cleaning activity, DDB will ignore all of those records where TTL attribute is missing.
    3. Last but not least, whenever you decide to update the status to complete, then make sure you have set expireAfter value properly. And, save it. DDB will take care of it automatically.

    Hope this clarifies your doubts

    Few AWS official links, that have explained few more things in details:

    Login or Signup to reply.
  2. Yes, the TTL field can be updated to shorten or lengthen a record’s life.

    As the docs explain, a "background process automatically and continuously evaluates the expiry status of items in the table". One process marks expired records as deletable and a second actually deletes the records. The deletion step "typically" occurs within 48 hours of expiry, although usually much faster. "Items that are past their expiration, but have not yet been deleted can still be updated, and successful updates to change or remove the expiration attribute will be honored".

    DynamoDB ignores TTL values more than 5 years older than the current time. So setting a TTL to the far past or far future will work for your uncompleted records.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search