1

I have a table inside dynamodb with a hash key of deviceKey and a range key of timestamp. deviceKeys have the form of 'randomstring-num' so for example I might have 'abc-1', where the string is an IoT device id, and the number is an event sequence. Due to our IoT setup, we can get duplicate messages that appear identical. To avoid these duplicates, I want to avoid inserting items with the same deviceKey that are within 10 seconds of each other. I've tried to do the following, but it seems to always fail.

const params = {
  TableName: 'test',
  ConditionExpression: 'attribute_not_exists(#ts) and NOT (#ts between 
  :start and :end)

  ExpressionAttributeNames: {
    '#ts': 'timestamp'
  },
  ExpressionAttributeValues: {
   ':start': ts - (10 * 1000),
   ':end': ts + (10 * 1000),
  },
  Item: {
   deviceKey: 'abc-1',
   timestamp: Date.now(),
  }
}

The above code always results in a condition failed. It seems that anytime I include the date range NOT BETWEEN part, it fails. Am I trying something that isn't possible with this hash + range key configuration?

To summarize, If

hash    range
abc-1   2019-03-08 12:35:36

exists, I don't want to allow anything within like 10 seconds forward or backwards to be inserts with that same hash key, so

hash    range
abc-1   2019-03-08 12:35:38

should fail. These messages are coming in at roughly the same time (ms apart), so I'm not really sure if doing a query before inserting is a viable option in this case.

0 Answers0