Data Storage API returns 400 when using ttl and setIf

Hi,

I am setting some data via Data Storage API inside onTicketEvent handler method. I am using ttl param to ensure their removal as soon as the job is done and setIf to ensure a one-time operation in case of duplicate events.

However, it’s returning 400 Error message, even when the key-value association should not exist (expired ttl). The $db.set command is as follows:

$db.set(`requester_${requesterId}`, { "ticketId": originalTicket['id'] }, {ttl: 5, setIf: "not_exist"})

It mostly goes into error block and returns the following error:

Ticket ID: XYZ - {"status":400,"message":"The setIf conditional request failed","errorSource":"APP"}

The assumption would be that the key already exists. However, to confirm, I have set a temporary recurring job checking the existence of one hardcoded Requester ID. That record always returns:

{"status":404,"message":"Record not found"}

Let me know if more context is needed.

Edit: This works locally and creates entry in localstore file and returns 400 only when data actually exists.

2 Likes

Hi @utkarsh,

Welcome to the community! :tada:

The ttl param will make sure the key-value pair is eventually removed after the mentioned ttl time on our end. But, it may not happen immediately which causes the setIf conditional operation to fail.

The key used with ttl can be replaced with SET or UPDATE operations. However, it cannot be used with setIf conditional operation. This is the behavior of it due to the implementation.

As a workaround, you can GET the key and upon receiving the error, {"status":404, "message": "Record not found"}, the new value can be SET. It will increase one more DB operation than your implementation.

Share with us if you have used any other workaround.

1 Like