I've already read multiple articles about redis transactions. I have some lists that contains messages. And I use redis to generate auto-increment IDs for those messages. Here's what I'm trying to do:
- Read the counter value, then INCR it.
- Put the incremented counter value in the
Id
field of the message, then serialize the message. - Push the serialized message into the list.
So the counter always holds the last message's ID in the corresponding list. I want to put a lock on the counter key so other requests cannot read and INCR the counter and push another message to the list that conflicts with the last ID.
Since I want to have a limited number of redis clients, WATCH MULTI EXCEC cannot be implemented, because it's the same client doing the transaction. And as far as I know WATCH MULTI EXCEC is for when you have multiple redis clients.
I want to know what is the correct approach for this problem. Should I use LUA scripts and let it serialize the message?