Scenes
Redis transaction implementation process:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/105048241
After the above process learn to use Redis transaction, Redis there is a watch mechanism.
Watch the principle mechanism
Use watch to monitor one or more key, the revision of the tracking key value, if there is a value key value
Is modified before the transaction is executed exec, the entire transaction is canceled. exec prompt return message indicating that the transaction has failed.
But if you use a watch to monitor the key with expiration time, even if the key expired, the transaction can still perform normally.
In most cases, the situation is different clients will visit different keys to each other while competing for the same key are generally small, optimistic locking can
The problem with good performance resolve data conflicts.
When the cancel key monitoring
1.watch command can be called multiple times. Monitoring of key effect from execution after the watch until the call exec, regardless of whether the transaction executed successfully,
Monitoring of all keys will be canceled.
2. When the client disconnects, the client monitor key will be canceled.
3.unwatch command can manually cancel the monitoring of all keys.
watch optimistic locking mechanism and an example
Download and install Centos compile Redis in the (ultra-detailed):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334
Redis startup and shutdown (foreground and background start start):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103972348
RedisDesktopManager client visualization tool to download and install and use:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103983147
Referring to the above blog to build a good environment and start redis server, and then open the two client connections.
Here it is used to copy a session so XShell
Set key-value in the first client and watch the key and then turn on the transaction, do not commit the transaction that is exec
Key 100 is then modified to copy out in this session is the second client
Then back to the first client that is executing exec commit the transaction
We can see the first transaction of the client did not commit, because the value of the watch has been modified, so give up the transaction.