Redis study notes (c): Publish and Subscribe, affairs

Functional independence
 
1. Publish and Subscribe
 
Redis of publish and subscribe functions by the publish, subscribe, psubscribe commands such as composition
 
Clients subscribe to one or more channels
> SUBCRIBE " news.it "  " news.et "    // subscribe to two channels
 
The client sends a message to the channel
> Publish " news.it "  " hello "       // Send the message hello to news.it channel
Subscribe to the channel the client will receive a hello message news.it
 
Clients subscribe to one or more patterns
> Psubscribe " News. * "  " Book. * "    // equivalent to subscribe to match the two models in any one of the all channels
 
Client unsubscribe channel
> unsubscribe "news.sport"

 

Client mode unsubscribe
> punsubscribe "book.*"
 
2. The principle channel subscriptions
When a client performs subscribe subscribe command, the client and the subscription channel on the establishment of a relationship. pubsub_channels dictionary This relationship is stored in the server's status.
Dictionary key for the subscribed channel; dictionary is a linked list of all the records in the client subscription channel
 
Subscription channel , different treatment depending on whether the channel is the first subscriber
    1, is the first subscriber to create a channel corresponding key (string object), and the key is set to an empty list structure, and then add the client to the list.
    2, not the first subscriber, the key already exists, then added directly to the end of the list structure corresponding to the value.
 
When unsubscribe channel , different treatment depending on whether the last subscriber
    1, is the last one subscriber, remove unsubscribe client, continue to delete the channel corresponding keys in the dictionary
    2, not the last one subscriber, the structure corresponds directly to the value chain according to the key, and then traverse the client can unsubscribe delete
 
Subscribe principle mode
pubsub_patterns list subscription relationship model stored in the server's status
Each node contains a linked list pubsub_patterns structure pubsubPattern
pubsubPattern pattern structure contains attributes and client attributes (these two properties are composed of simple strings, or both together is a string object?)
 
Subscription model
    PubsubPattern a new structure, which is set to correspond to the pattern attribute mode, client attribute set to the subscriber, and then added to the current list of pubsub_patterns end of the table.
 
Unsubscribe mode
    Find a pattern attribute to unsubscribe mode, client property pubsubPattern structure can unsubscribe persons.
 
Send message flow and principle
> Publish <Channel> <the Message>    // Channel for the channel

1, sends a message to all subscribers of the channel's subscribers
        Principle: find the corresponding key pubsub_channels dictionary, and then sequentially transmitted message according to the value in the list structure corresponding to the client
 
2, the channel sends the message matches the given pattern subscribers
        Principle: pubsub_patterns list traversal to find all the channel pattern matching (pattern attributes), and sends the message to their corresponding client (client attributes)
 
 
View pubsub subscription information of the three sub-command
> PubSub channels [pattern]      // query the server for all channels currently subscribed, [] within an optional parameter that can be used to match limit
  
> PubSub numsub [CHANnel1 CHANnel2 ...]    // Returns the number of subscribers to these channels, that is, length value list structure corresponding to the subscriber key
  
> PubSub numpat                 // returns the current mode is the server subscription number, i.e., the length of the list returned pubsub_patterns
 
3. Transaction (transaction)
 
Concept: one or more commands packed, after which time, a unified execution order.
 
Related Commands
 
Monitor transaction database key
 > Watch <key_name>     // key_name key is to listen database, the database used to determine whether the key is modified during the transaction other client 
 
turned Affairs
 > Multi 
 
submit and execute transactions
 > exec
 
Note: a plurality of transaction-time command is sent to the server, instead of sending a one, in this way is called pipeline. disposable pipeline may send multiple commands and returns the results after the implementation of the disposable, can reduce network traffic between the client and the server so as to enhance the performance of frequency, and based on pineline queue, the queue is a FIFO characteristic, so to guarantee the order of the data.
 
Implementing transactions
Generally divided into three stages
 
1. Begin a transaction
    Use multi command, the command is executed is switched from the client transaction state to a non-transactional state
    flags attribute client state will open REDIS_MULTI logo to mark the turn of the client business, in state affairs
 
2. Command the team
    Under non-transactional state, Redis commands sent by the client will be directly executed server
    The transaction state, the server will Redis command sent by the client into a transaction queue, and returns the QUEUE (except exec, discard, watch, multi four commands)
 
    Each client has its own state of affairs, the state is stored in the client's property mstate (a multiState structure, containing a FIFO queue and a counter transaction count)
 
    A transaction queue is multiCmd array, each element multiCmd stored information (function pointer, command parameters, the number of parameters) of a command enqueue
 
    count counter is used to count the transaction queue length.
 
3. transaction execution
    After the client sends the exec command
    Traversal server the client's transaction queue, single-threaded serial execution of transactions in the transaction queue, and the results of each command in turn returned to the client
 
 
Watch command
 
    watch command is an optimistic locking, before the exec command is executed, the database used to monitor whether the key change in the transaction establishment process
    watch command word exec command is executed, it checks to monitor key modification has occurred; if modifications occur, the server will reject the transaction.
 
    The underlying implementation
        Each Redis server holds a watched_keys dictionary,
        The key is the dictionary database key is monitored, the value is a linked list structure, the list records of all the monitored client key database
    
    Realization of the principle
        Once the database server executes the command to make changes (such as SET, LPUSH, DEL, ZADD, etc.), will be called after the implementation of a function watched_keys dictionary checks to see whether the key is to monitor the modifications made; they are in the the database monitors all key REDIS_DIRTY_CAS identity of the client open to represent the client transaction security breach. The exec command is executed, the client will identify REDIS_DIRTY_CAS judgment, if it is found that the logo is turned on, the transaction is considered unsafe, and refuse to execute the transaction.
 
ACID Redis transaction
    Atomicity, consistency, isolation, durability
 
    Redis Affairs has atomicity, consistency, isolation; specific case will persistent (AOF of persistence mode, and is set to Always appendfsync)
 
    Consistent concepts: data definitions and requirements in line with the database does not contain illegal or invalid data error
    
    Redis from three aspects to maintain consistency (error detection and design to maintenance)
        1. The error into the team
            If the command does not exist, incorrect formatting, and so on, the team will get an error process, while the server refused to carry the team into the error occurred during transaction
 
        2. Execution Error
            As the operation keys is correct but the command does not meet the other objects (SET operation list, etc.), server error command is identified, and the corresponding error processing, but the error command does not make any modifications to the database
 
        3. server downtime
            (1) RDB, AOF persistent maintenance
            (2) If you can not find a corresponding file, or in a non-persistent mode, then restart the database is blank, still consistent with the requirements of
 
    Redis commands execute a transaction by a single thread, a serial transaction queue, so even if multiple clients concurrent transaction, still will not affect each other
 
    To understand why the majority of cases can not be maintained (such as when the RDB persistence persistence, as appendfsync = everysec / no why not maintenance) Persistence
    
Redis Affairs and the traditional relational database transactions biggest difference :
    Redis does not support a transaction rollback (roll back), execute the command during a transaction, even if the find command execution error, the entire transaction will continue execution until the corresponding client's transaction queue all commands executed. (The question is not will not meet the atomicity yet? Redis authors believe that this situation is there is a compilation error, the actual production does not appear?)

Guess you like

Origin www.cnblogs.com/xiang9286/p/11003922.html