Introduction to Watch in Redis

Introduction to Watch in Redis

Welcome back! In previous lessons, you've learned how to build and execute transactions in Redis using PHP with the Predis library. This lesson will introduce you to the watch functionality in Redis, enabling conditional and controlled transactions. Such functionality is vital for scenarios where you need to monitor specific keys and ensure operations only execute when certain conditions are met. The lesson will focus on understanding how to monitor keys to control transaction execution.

Using "watch" with Predis

Below is a code example of how to use the watch command with Predis:

PHP
<?php

require 'vendor/autoload.php';

use Predis\Client;

// creating a client
$client = new Client();

// creating another client simulating concurrent access to the same application
// from different callers
$otherClient = new Client();

// keys to be used for our values
$key = "valueToMonitor";
$anotherKey = "valueNotMonitored";

// setting initial values
$client->set($key, 0);
$client->set($anotherKey, 0);

// telling redis to watch a specific key
$client->watch($key);

// starting a transaction
$client->multi();

// incrementing both key values by 50
$client->incrby($key, 50); 
$client->incrby($anotherKey, 50);

// a different client modifies the value under the watched key
$otherClient->incrby($key, 23);

// incrementing both key values by 50 again
$client->incrby($key, 50);
$client->incrby($anotherKey, 50);

// calling the exec command to commit to the transaction
$client->exec();

echo "Current value of key [", $key, "]: ", $client->get($key), "\n";
echo "Current value of key [", $anotherKey, "]: ", $client->get($anotherKey), "\n";

// Output:
// Current value of key [valueToMonitor]: 23
// Current value of key [valueNotMonitored]: 0

The example code demonstrates how to use the watch command. Here's a detailed explanation:

  1. Setup: The code begins by loading the Predis library with require 'vendor/autoload.php';. Two Redis clients are then created using Predis\Client. These simulate concurrent access to Redis.

  2. Key Initialization: Two keys, $key and $anotherKey, are initialized with the value 0. These keys simulate data fields within Redis that are subject to transactions.

  3. Watch Command: The primary client initiates a watch on the key $key using $client->watch($key);. This instructs Redis to monitor this specific key for changes.

  4. Start Transaction: A transaction block is started by invoking $client->multi();. This allows all subsequent commands to be queued and executed as a single transaction.

  5. Increment Operations: Both keys are incremented by 50 using $client->incrby($key, 50); and $client->incrby($anotherKey, 50);. These commands are queued for transaction execution.

  6. Concurrent Modification: A simulated concurrent client ($otherClient) modifies the watched key, $key, by 23 using $otherClient->incrby($key, 23);.

  7. Additional Increment: The primary client again queues increment commands for both keys with an additional 50.

  8. Execute and Check: When $client->exec(); is called, the transaction attempts to execute but fails to apply changes to $key because it was altered by another client during the transaction. Note that even though the value under $anotherKey wasn't subject to the watch command, it still didn't get updated by the calls done within a transaction.

  9. Output: The final output shows that $key has a value of 23 (from the concurrent modification), whereas $anotherKey remains at 0, reflecting the rollback of changes due to the watched condition.

This code illustrates the power of the watch command to maintain data integrity by acknowledging concurrent data modifications, effectively preventing conflicts in a multi-client environment.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal