Transactions

We explore some commands used for Redis transactions in this section. A transaction enables one client to execute a series of multiple commands without another client being able to interrupt the commands. Transactions guarantee that the commands are executed as a single isolated operation.

Swimming Pool

In our scenario, we are using a Raspberry Pi to gather temperatures from our swimming pool and uploading those temperatures to a cloud-based historian. Unfortunately, the world is not perfect, and we occasionally lose Internet connectivity. Redis to the rescue! In this case, we use a Redis list as a temporary historian to store our temperature values until our Internet connection is restored and we can empty the list and upload the values to the cloud. :cloud: Each temperature point is stored as a JSON object as follows:

{"t":"1/6/2018 10:01","v":75.59}

As shown, we employ a convention where t represents the timestamp and v represents the value.

First, let's populate a Redis poolTemps list with some swimming pool temperature values to simulate data that has been streamed in live from our pool temperature sensor:

redis> rpush poolTemps '{"t":"1/6/2018 10:01","v":75.59}'
(integer) 1
redis> rpush poolTemps '{"t":"1/6/2018 10:02","v":75.60}'
(integer) 1
redis> rpush poolTemps '{"t":"1/6/2018 10:03","v":75.63}'
(integer) 1

Let's view the current list of values queued in the list:

redis> lrange poolTemps 0 -1
1) "{\"t\":\"1/6/2018 10:01\",\"v\":75.59}"
2) "{\"t\":\"1/6/2018 10:02\",\"v\":75.60}"
3) "{\"t\":\"1/6/2018 10:03\",\"v\":75.63}"

Our challenge is that we want to retrieve and upload all the values stored in our poolTemps to our cloud-based historian. We also want to empty the list; however, we have another process running that is faithfully adding new temperature sensor values every minute. There is a remote possibility we could retrieve all the temperature values from the list and the other process could add another sensor value in the split second before we delete the list. One of our history points would be lost. :cry: Let's solve this problem with the help of transactions. We demonstrate how it works from the command line, and we will also demonstrate using transactions with Node.js later in the tutorial.

MULTI

Marks the start of a transaction block. Subsequent commands will be queued for atomic execution using EXEC.

Let's begin our transaction so no other clients can interrupt our operation we perform in the following commands.

redis> multi
OK

See the official docs for more details.

EXEC

Executes all previously queued commands in a transaction and restores the connection state to normal.

Let's retrieve all our swimming pool temperature values and empty the list—all in one transaction. Here's a complete list of the commands, including the initial multi command, in one context:

redis> multi
OK
redis> lrange poolTemps 0 -1
QUEUED
redis> del poolTemps
QUEUED
redis> exec
1) 1) "{\"t\":\"1/6/2018 10:01\",\"v\":75.59}"
   2) "{\"t\":\"1/6/2018 10:02\",\"v\":75.60}"
   3) "{\"t\":\"1/6/2018 10:03\",\"v\":75.63}"
2) (integer) 1

Note that Redis queues the commands until the exec command is invoked. The results of each command are listed individually as the non-indented values of 1) and 2).

We conducted this transaction all from the command line so we can't fully demonstrate how to retrieve the values from the list and send them to the cloud :cloud:; however, we will demonstrate how to accomplish this goal later in the using transactions with Node.js portion of the tutorial.

See the EXEC command in the official docs for more details.

results matching ""

    No results matching ""