Redis Transaction

Redis support transactions, but the transaction in redis is different compare to PostgreSQL/MYSQL transaction.

A Redis transaction only guarantee that a batch of commands executes together without other client to cutting in, it doe snot support rollback.

Redis transactions are implemented with four commands: MULTI, EXEC, WATCH, and DISCARD:

  1. MULTI starts a transaction. Commands sent afterward are not executed immediately; instead, they are placed in a queue.
  2. You can add any number of commands to the queue. Redis replies QUEUED to indicate each was queued successfully.
  3. EXEC runs all queued commands in one go, and no commands from other clients can interrupt them in the meantime.
  4. If you change your mind partway through, DISCARD throws away the entire queue.
  5. WATCH can monitor certain keys before MULTI. If any of those keys are modified by someone else before EXEC, the whole transaction is aborted.

a PostgreSQL/MySQL transaction that fails halfway through can roll back, but a Redis transaction can’t. If one command in a Redis transaction fails, the remaining commands still run, and the ones that already ran are not undone.