Redis internals affairs

1: Perform MULTI command marks the beginning of a transaction

 

2: After entering the command queued client transaction state when the server receives a command from the client, the command will not be executed immediately, but all of these commands into a transaction queue, and then return to QUEUED, indicating that the command has been queued .

Redis each client has its own state of affairs, the transaction includes a transaction queue status, the counter has a command of the team.

typedef struct multiState{

// transaction queue, FIFO order (array)

multiCmd *commands;

// enqueued command count

int count;

} multiState;

Transaction queue is an array, each array item contains three properties are:

typdef struct multiCmd{

// command parameters

robj ** argv;

// number of parameters

int argc;

// pointer command

struct redisCommand *cmd;

} multiCmd;

 

3: EXEC command is executed, first in first out (FIFO) manner traversal client transaction execution command queue. After the execution, EXEC command queue will reply back to the client, the client command counter is cleared into the team, the release transaction queue, returns to the non-transactional state.

 

4: DISCARD command is used to cancel a transaction, it is clear the entire transaction queue of the client, then the client will adjust back to the state from non-transactional state affairs, and returns the string OK to the client, indicating that the transaction has been canceled

 

5: WATCH commands are used to monitor any number of matters before the start button: When you call EXEC command is executed transactions, if any of the monitored key has been another client changed, then the entire transaction is no longer executed, direct return failure. In the state of affairs send WATCH command will cause an error, but it will not cause the entire transaction fails, it will not modify the existing data transaction queue.

 

WATCH command to achieve

redis.h / redisDb structure type database, are kept a watched_keys dictionary whose key is the key to this database is monitored, and the value of the dictionary is a list, the list holds all monitoring this key client. WATCH command role, is the current client and to monitor key associate in the watched_keys

 

WATCH trigger

When the client sends EXEC command, triggering transaction execution, client server status will be checked:

1 :: If the client's REDIS_DIRTY_CAS options have been opened, then that is monitored at least one client key has been modified, security affairs, has been destroyed. Server will give up the implementation of this transaction directly to the client returns an empty reply, indicate that the transaction failed.

2: If REDIS_DIRTY_CAS option is not open, then that monitors all keys are secure, server formal enforcement branch

 

redis affairs caveats:

1: enqueue command syntax error, the exec command has not been executed at this time

While redis met before the exec command commands in the transaction will not be executed, however, it would be appropriate to check each command, when found to have some obvious syntax errors, such as incorrect number of parameters, it will be in when the team returns an error message, and when you see the exec command to call discard command rollback

2: When the exec finished, error occurred while executing other commands

When redis in the implementation of the command, if something goes wrong, then redis not terminate the execution of other commands. That is, as long as the correct command, or after, will be successfully executed both before and command errors

3: The interaction between transactions

If two transactions simultaneously modify a record, and redis transactions in default did not conduct a process, first of all the result of an exec transaction will be overwritten. Here we can use the watch command, which is used to monitor certain specific key, if the key has been modified by other transactions, then it will not succeed this time to amend the transaction, and then return to prompt failure

 

 

Published 50 original articles · won praise 2 · Views 2284

Guess you like

Origin blog.csdn.net/eafun_888/article/details/104738549