How to Use the Redis Transactions Commands in the Database

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Open source Redis helps developers, DBAs, and other technology specialists manage data in an all-encompassing way. Foundation commands play an important role in that effort. The main reason for this is that foundation commands in Redis were designed to be atomic. This means that Redis transactions use an all-or-nothing proposition. In short, when executed, a transaction command sequence must occur in its entirety or nothing will happen. Other transactions won’t interfere with it once it is set in motion. With this in mind, using basic Redis transaction commands might be the key to streamlining your workload. Find out more in this tutorial that shows you how to use Redis transaction commands in the database.

Prerequisites

  • If you don’t already have a copy of Redis, download the latest stable version and then enable the redis-server.

  • Check your version with the redis-server --version command.

  • Verify the command line interface interactive version with the redis-cli --version command.

Transactions in Redis database

Here are the Redis database foundation commands and their corresponding functions:

  • MULTI – This is the transaction block’s beginning marker.

  • EXEC– Signals the execution of the commands in the queue and then returns to its normal state after the command execution.

  • DISCARD – Cleans out the command queue once the execution has finished and then goes back to its normal state.

The MULTI, EXEC, and DISCARD commands occur in order. If the transaction fails, the entire process is aborted. Therefore, it acts as a single sequence when executed.

Using the transactions in Redis command-line interface

  • Use the command line interface in Redis database and create some keys like this:
1
2
3
4
127.0.0.1:6379> SET int1 15
OK
127.0.0.1:6379> SET int2 20
OK
  • Start with the foundation command MULTI which starts the sequence of Redis transactions.
1
2
127.0.0.1:6379> MULTI
OK
  • The server returned an “OK,” so you are ready to apply the next two commands. Use the int1 first, and then try the int2 like this:
1
2
3
4
127.0.0.1:6379> INCR int1
QUEUED
127.0.0.1:6379> INCRBY int2 100
QUEUED

Now the commands you entered are queued but haven’t yet started.

  • At a new Redis interactive command line, verify the key values you set earlier with the GET command.
1
2
3
4
127.0.0.1:6379> GET int1
"15"
127.0.0.1:6379> GET int2
"20"

>NOTE: Redis uses a process called isolation to make certain that a server’s executed commands follow through without interruption. That is why the first set of commands you entered did not change.

  • Next, use the EXEC command to execute the transaction as shown here. You won’t be able to pass arguments with that command because the queue is already set.
1
2
3
127.0.0.1:6379> EXEC
1) (integer) 16
2) (integer) 120

>NOTE: Check your commands for accuracy if you receive a command error using Redis transactions. If you receive an error prior to calling EXEC, verify the argument number and command names as well as your server’s memory limit. If you receive an error after calling EXEC, confirm that the key values and the operation you called them against.

Alternatively, if you want to cancel the transaction, use the command DISCARD.

  • To try it on the keys, start with the MULTI command.

  • Then use the command INCR.

  • Next, use the command DISCARD.

1
2
3
4
5
6
7
8
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> INCR int
QUEUED
127.0.0.1:6379> INCR int2
QUEUED
127.0.0.1:6379> DISCARD
OK

The code shows that the Redis transactions were successfully canceled because the response to the DISCARD command returned ‘OK’.

  • Use the command GET command to display the results.
1
2
3
4
127.0.0.1:6379> GET int1
"16"
127.0.0.1:6379> GET int2
"120"

Conclusion

Redis transactions are just one of the many features users find beneficial about the flexible, user-friendly Redis software. The big three transaction commands: MULTI, EXEC, and DISCARD are essential in starting, completing, and canceling a transactional sequence. Executing a series of commands in a sequential manner without disrupting other transactions is one of the best examples of smoothing coding in Redis. Take advantage of Redis transactions today.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.