I have a use case where I have to update sets in Redis every one hour.
Suppose the sets were created with following commands
127.0.0.1:6379> sadd k1 m11 m12 m13 m14
(integer) 4
127.0.0.1:6379> sadd k2 m21 m22 m23 m24
(integer) 4
127.0.0.1:6379> sadd k3 m31 m32 m33
(integer) 3
127.0.0.1:6379> sadd k4 m41
(integer) 1
After 1 hour, I want to update the data to
k1 = {m11, m13, m15}
k2 = {m21, m25}
k5 = {m51, m52}
I found that this can be done using Redis transactions
multi
del k1
sadd k1 m11 m13 m15
exec
multi
del k2
sadd k2 m21 m25
exec
multi
del k5
sadd k5 m51 m52
exec
But now k3 and k4 are still present in Redis. If I don't remove them, they pile up over time. I am solving this by adding 2 hour expiry to every key.
But found one problem with the above approach.Transactions in Redis don't have roll back if a command fails after exec.
So, if del k1 passed and sadd k1 m11 m13 m15 failed, k1 will be removed from Redis entirely. I would prefer to have old data instead of having nothing.
Is there a way to avoid deleting a key when sadd fails?