Redis affairs - read articles must understand

1: Redis Introduction

  • Redis transaction can execute multiple commands once , and with the following three important guarantee:
    • Batch operation in a transmission before the EXEC command is placed in queue buffer .
    • After receiving EXEC command to enter the transaction execution, transaction arbitrary command execution fails, the rest of the command is still being executed .
    • During the transaction, the command requests submitted by other clients will not be inserted into the transaction execution command sequence .

redis may be a queue, the disposable, sequential, exclusive execute a series of commands

  • From start to execute a transaction will go through the following three stages:
    • Begin transaction: to begin a thing MULTI
    • Command into the team: the team into multiple commands into transactions, received these orders will not be executed immediately, but placed in a queue waiting to be executed inside a transaction
    • The enforcement branch: EXEC command triggered by the transaction

2: Usage Redis affairs

2.1 Redis transaction commands introduced

Using the MULTI command input a Redis transaction . This command always returns the OK . In this case, the user can issue multiple commands. Redis These commands will be queued, rather than executing them. All commands executed when calling EXEC . Instead, the call will flush the transaction queue to give up and quit the transaction.

No. Command and description
1 DISCARD: cancel the transaction, giving up all the commands within a transaction block
2 EXEC: execute it within a transaction block command
3 MULTI: mark the beginning of a transaction block
4 UNWATCH: Cancel WATCH command to monitor all the key
5 WATCH key [key]: monitoring one or more key, if this key change is executed before the transaction, then the transaction will be interrupted

2.2 normal execution

We did not talk much old iron, in Ituri
normal execution demo:

Here Insert Picture Description

2.3 renounce Affairs

Abandon the transaction demonstrates:
Here Insert Picture Description

2.4 all guilt

All even sit demo
Here Insert Picture Description

2.5 injustice head creditors

Head injustice creditors demo
Here Insert Picture Description

2.6 all guilt and injustice head difference creditors

If the direct program appears like Java 10/0 does not give you the opportunity to direct an error - all guilt ( if the command is all queued error occurs even sit )
Java IO will throw an exception if it is thrown - Head injustice creditors

2.7 watch monitor

  • Pessimistic locking / optimistic locking / CAS (Check And Set) Introduction
  • Pessimistic locking: As the name suggests, is very pessimistic, pick up data every time the thought that others will modify, so every time she took the data will be locked, so people want to take this data will block until it got the lock. Prior to traditional relational database inside to use a lot of this locking mechanism, such as row locks, table locks, etc., read lock, write lock, are doing the first lock operation
  • Optimistic locking: As the name suggests, is very optimistic, pick up data every time when they are that others will not be modified, but it is not locked at the time of the update will determine what others during this time did not go to update the data, you can use the version No. mechanisms. Optimistic locking is suitable for the types of applications to read, which can improve throughput

Optimistic locking strategy: Submit version must be greater than the current record version in order to perform the update

watch monitor demo
initialization credit card the amount available and Under and no stopper tampering, first monitor and then turn multl, to ensure that these amounts vary within the same transaction, there Gasser tampered
1: Initial credit cards available amounts and Under and no stopper tamper
Here Insert Picture Description
2: to monitor and then open multl, to ensure that these amounts vary within the same transaction
Here Insert Picture Description
3: at the same time began to engage in sabotage, we conduct monitoring of balance, a stopper tampering

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
4: View of the value of balance now, and restored to its original state
Here Insert Picture Description

  • summary
  • Watch command, similar optimistic locking, the transaction commits, if the value of the Key has been changed other end customers, such a list has been another client pysh / pop too, and the whole transaction queue will not be executed
  • By monitoring multiple Keys WATCH command before executing a transaction, if there is any value in Key has changed after WATCH, transaction .EXEC command execution will be discarded, and returns Nullmulti-bulk (nil) response to inform the caller transaction failed
  • Once added before the implementation of exec / unwatch monitoring the lock will be canceled

3: Redis transaction characteristics

  • Isolation separate actions: All commands are serialized transaction performed sequentially. During execution of a transaction, the command will not be sent to other client requests interrupted.
  • No concept of isolation levels: will not be executed before the actual command queue did not submit, because before the transaction commits any instruction will not be actually executed, the query does not exist in the "things to be seen in a transaction update in foreign affairs inquiry can not see "the people are extremely headache
  • Does not guarantee atomicity: Redis same transaction if there is a command fails, the subsequent commands will still be executed, there is no rollback
Published 173 original articles · won praise 122 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44891295/article/details/104098768