Home > Blockchain >  How can I keep only the last n items within DynamoDB?
How can I keep only the last n items within DynamoDB?

Time:05-09

I have a streaming app that is putting data actively into DynamoDB.

I want to store the last 100 added items and delete the older ones; it seems that the TTL feature will not work in this case.

Any suggestions?

CodePudding user response:

There is no feature within Amazon DynamoDB that enforces only keeping the last n items.

Limit 100 items as the maximum within your application by perhaps storing and keeping a running counter.

CodePudding user response:

I'd do this via a lambda function with a trigger on the DynamoDB in question.

The lambda would then delete the older entries each time a change is made to the table. You'd need some sort of highwater mark for the table items and some way to keep track of it. I'd have this in a secondary DynamoDB table. Each new item put to the DynamoDB item table would get that HWM add it as a field to the item and update it. Basically implementing an autoincrement field, as they don't exist in DynamoDB. Then the lambda function could delete any items with an autoincrement id that is HWM - 100 or less.

There may be better ways but this would achieve the goal.

  • Related