"Redis Design and Implementation" study notes (d)

Functional independence

XVIII publish and subscribe

Publish and Subscribe composed by the following few commands

  • PUBLISH, dissemination of information, such as PUBLISH <channel name> <message content>
  • SUBSCRIBE, subscribe to a channel SUBSCRIBE <channel name>
  • UNSUBSCRIBE Unsubscribe from a channel
  • PSUBSCRIBE, subscribe to a channel model, such as news. * PSUBSCRIBE <mode channel name>
  • PUNSUBSCRIBE, unsubscribe from a channel mode

data structure

  • redisServer
    • dict * pubsub_channels key is the channel name, value is a list, save the client all subscribe to this channel
    • list * pubsub_patterns list, the value is pubsub_pattern objects, saving the client and channel mode

operating

  • Subscribe. For example the command SUBSCRIBE news.it
    • See if pubsub_channels dictionary news.it this key, if so, to add a client to the back of the list, if not, create a new list, set news.it this key point to this list
  • Unsubscribe channels. For example the command UNSUBSCRIBE news.it
    • Find news.it corresponding list, traverse the list to find out the current client, and then delete
  • Channel subscription model. For example command PSUBSCRIBE news. *
    • Add a pubsub_pattern objects behind pubsub_patterns list, save the client information and news. *
  • Unsubscribe mode channel. For example command PUNSUBSCRIBE news. *
    • Pubsub_patterns traverse the list, delete the client equal to the current client mode equal news. * Objects
  • Dissemination of information, such as PUBLISH news.it news11
    • Traversing pubsub_channels [news11] list, to send messages to all clients in the chain
    • Pubsub_patterns traversal list, if the matching pattern and news11, sends a message to the corresponding client

Other commands

These commands are added after 2.8

  • PUBSUB CHANNELS [pattern] mode is optional
    • If there is no pattern to return the server all subscribed channels
    • If so, return to the pattern matching for all channels
  • PUBSUB NUMSUB [channel-1 channel-2. . . ] You can enter multiple channels
    • Returns the number of subscribers per channel, i.e. pubsub_channels [channel-1] of length list
  • PubSub NUMPAT
    • Returns the number of server mode channel is currently subscribed, that is, the length of pubsub_patterns list.

Ninth, affairs

Redis to implement the transaction by MULTI EXEC WATCH command

Examples of affairs

MULTI
get test
set test 111
EXEC

data structure

  • redisClient client object
    • multiState mstate state of affairs
  • multiState transaction state objects
    • multiCMD * commands save all the changes the transaction following command FIFO principle
    • Int count the number of enqueued command
  • multiCmd command object
    • robj ** argv command parameters
    • int argc number of arguments
    • struct redisCommand * cmd command object

Process

  • When performing MULTI command, change the client to go into business mode. REDIS_MULTI identify the client object opens the flags property
  • When performing information get and set commands, these commands will be added to the commands queue. But does not execute immediately
  • When the EXEC command. The server to the client's commands out commands, executed one by one, then returns the result of each of
  • All commands between MULTI and EXEC command will not be executed immediately.
  • After performing the EXEC command, empty client transaction state objects, including identification flags, and other objects Multistate

WATCH command

WATCH is a command optimistic locking, you can monitor any number of key database EXEC command before beginning.
Such as transaction
WATCH name
the MULTI
the SET name kevinlu
EXEC

If after WATCH, former EXEC, name the key is changed, execute EXEC, it will return empty to the client.

achieve:

  • dict * watched_keys variable redisDb objects used to hold all key client object and watch the current command. key is the key database, value is a list, values ​​are client objects
  • When performing WATCH command, Redis data will be added in watched_keys
  • When Redis perform the modification command, performs touchWatchKey operation. Concrete is in watched_keys find the list of key changes, all client object in the linked list, open REDIS_DIRTY_CAS client's identity, these clients represent key WATCH there is at least one modified the
  • When a client executes EXEC command to check REDIS_DIRTY_CAS identity has been opened, and if so, return empty. Otherwise, the transaction was executed.

The nature of the transaction (ACID)

  • A atomicity. Because the Redis transaction execution packaged together, so having atomic. To be unsuccessful, or else fail. But there will be no Redis transaction is rolled back, that is, for example, Article 2 command fails, Article 1 of the command will not be rolled back, Article 3 will continue to execute the command. The writer is a command to roll back too complex, not simple principle for the Redis.
  • C consistency. Before and after execution of transaction execution can guarantee consistency. Consistency means that the design will not damage the database (the good general, do not know the specific boundary). Redis command execution errors are mainly two:
    • The wrong team. For example, the command does not exist. This will be found before and EXEC Redis rejected. So this will not undermine consistency.
    • Execution error. When executing a command error, such as key type string, but with rpush operation. For the implementation of such a mistake, Redis does not terminate the transaction, it will not be rolled back. So these commands will not be executed, so it will not break the consistency (do not quite understand this, there is a command execution fails, how will not destroy the consistency?)
    • Database downtime. If you do not save the data, then consistency would not have considered, because after the restart is blank database. If you save data, transactions will fall, so it can ensure consistency
  • I isolation. Since Redis is single-threaded execution, so there is no transaction concurrency issues. It is possible to ensure isolation.
  • D durability. After a successful transaction execution guarantee persisted to the database. Since Redis in the implementation of the specified command, for example BGSAVE time, will be persistence, there is no guarantee durability.
    • AOF can be used to set appendfsync to ensure durability

Guess you like

Origin www.cnblogs.com/Xjng/p/12085126.html