watch
linux command is used to run a command at regular intervals.The command below is the simplest form of watch:
watch YOUR_COMMAND
For instance:
watch df -h
The command above runs
df -h
(check disk space) every 2 seconds by default.In order to change the interval:
watch -n 5 df -h
-n
or --interval
specify update interval in second. The command will not allow quicker than 0.1 second interval.In case you want to see the differences in your output command use
-d
or --differences
. It wil highlightwhen part of your command output changes. For example in our command if disk space usage changes we will see
the new result highlighted.
SIDE NOTE:
-h
in df
command will show a human readable format of disk space in mega byte.#linux #sysadmin #watch
Transactions in Redis
MULTI
, EXEC
, DISCARD
and WATCH
are the foundation of transactions in Redis
. They allow the execution of a group of commands in a single step, with two important guarantees:- All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation.
- Either all of the commands or none are processed, so a Redis transaction is also atomic. The EXEC command triggers the execution of all the commands in the transaction, so if a client loses the connection to the server in the context of a transaction before calling the MULTI command none of the operations are performed, instead if the EXEC command is called, all the operations are performed. When using the append-only file Redis makes sure to use a single write(2) syscall to write the transaction on disk. However if the Redis server crashes or is killed by the system administrator in some hard way it is possible that only a partial number of operations are registered. Redis will detect this condition at restart, and will exit with an error. Using the redis-check- aof tool it is possible to fix the append only file that will remove the partial transaction so that the server can start again.
Sample usage of the transaction:
> MULTI
OK
> INCR foo
QUEUED
> INCR bar
QUEUED
> EXEC
1) (integer) 1
2) (integer) 1
As it is possible to see from the session above, EXEC returns an array of replies, where every element is the reply of a single command in the transaction, in the same order the commands were issued.
In the next post we will talk about
WATCH
and DISCARD
commands too.#redis #transaction #multi #exec #discard #watch
Transactions in Redis part2
DISCARD
can be used in order to abort a transaction. In this case, no commands are executed and the state of the connection is restored to normal.We can discard a transaction like below:
> SET foo 1
OK
> MULTI
OK
> INCR foo
QUEUED
> DISCARD
OK
> GET foo
"1"
As you can see
foo
variable has not been incremented and value is set to 1 not 2.Optimistic locking using check-and-set:
WATCH
is used to provide a check-and-set (CAS) behavior to Redis
transactions.WATCHed
keys are monitored in order to detect changes against them. If at least one watched key is modified before the EXEC command, the whole transaction aborts, and EXEC returns a Null reply to notify that the transaction failed.WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC
Using the above code, if there are race conditions and another client modifies the result of val in the time between our call to
WATCH
and our call to EXEC
, the transaction will fail.#redis #transaction #multi #exec #discard #watch