$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$nbTry = 0;
do {
$redis->watch('x');
$val = $redis->get('x');
$newVal = max(0, $val + 1);
$result = $redis->multi()
->set('x', $newVal)
->exec();
//echo $result !== false ? 'Success' : 'Race lost, try again';
if($result !== false) {
break; // updated success
} else {
usleep(10);
$nbTry++;
if ($nbTry >= 1500000) break; //timeout 15 sec
}
} while (true);