In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you Redis publish subscription demonstration, transaction demonstration, persistence method, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Article catalogue
Introduction to Redis publish and subscribe
Second, Redis publish and subscribe demonstration
III. Transactions in Redis
IV. Transfer function-Redis transaction demonstration
Fifth, transfer function upgrade version-watch
VI. Error handling of transactions
Business logic error
Grammatical error
7. Redis persistence
RDB persistence
AOF persistence
Introduction to Redis publish and subscribe
Redis publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends the message and the subscriber (sub) receives the message. Redis clients can subscribe to any number of channels.
Application scenarios:
① builds real-time messaging systems, such as ordinary real-time chat, group chat and other functions.
② in a blog site, there are more than n fans subscribed to you, when you post a new article, you can push messages to fans.
③ Wechat official account mode.
Second, Redis publish and subscribe demonstration
Publish and subscribe syntax
Subscribe to channels:
Subscribe channel1 [channel2.] subscribe to information for a given channel or channels.
Psubscribe pattern1 [pattern2...] subscribes to one or more channels that match a given pattern.
Publish Channel:
Publish channel message sends information to the specified channel.
Unsubscribe to the channel:
Unsubscribe channel1 [channel2...] means to unsubscribe from a given channel.
Punsubscribe pattern1 [pattern2...] unsubscribes to all channels of a given mode.
III. Transactions in Redis
Redis transactions can execute more than one command at a time (serialized in sequence, will not be inserted by other commands and cannot be plugged)
Transaction application scenarios:
Commodity second kill
Transfer
Two characteristics:
Redis serializes all commands in a transaction and then executes them sequentially (any command fails, and the rest are still executed)
The execution will not be inserted by other commands, and no extra match will be allowed.
From the beginning to the execution of a transaction, it will go through the following three stages: start the transaction, order to join the queue, and execute the transaction.
Transaction-related commands:
Multi marks the beginning of a transaction block.
Exec executes commands within all transaction blocks.
Discard cancels the transaction and discards all commands within the transaction block.
Watch key monitors one (or more) key, and if the key (or these) is altered by other commands before the transaction is executed, the transaction will be interrupted.
Unwatch unmonitors all key from the watch command.
IV. Transfer function-Redis transaction demonstration
Demand: transfer function, A transfers 50 yuan to B account.
Before the transfer, A has 80 yuan and B has 10 yuan.
After the transfer, A has 30 yuan and B has 60 yuan.
This example starts a transaction with multi, then queues multiple commands into the transaction, and finally triggers the transaction by the exec command.
When you enter the Multi command, all the commands you enter will enter the command queue in turn, but will not be executed.
Until Exec is entered, Redis executes the commands in the previous command queue in turn.
Before executing exec, if you find that there is a problem with the added command, you can use the discard command to abandon the queue operation, similar to the rollback operation in MySQL.
Fifth, transfer function upgrade version-watch
Requirements: an account operates within a transaction, and another process operates on the account before committing the transaction.
The transfer mentioned above is not safe, and misreading may occur if there are other commands operating on account an or b during execution. The solution is to add a watch command to monitor the account with watch. Once other commands operate on account an or b during the execution of the transaction, the program directly reports an error and rollback.
After the watch command is executed, if the exec or discard command is executed first, there is no need to execute unwatch to unmonitor the key, because the exec or discard command automatically cancels the monitoring.
VI. Error handling of transactions
Business logic error
A business logic error occurs: only commands that report errors will not be executed, while other commands will be executed and will not be rolled back.
Grammatical error
Syntax error occurs: all queues are canceled during execution.
7. Redis persistence
Data is stored in memory: efficient, but power-off memory data will be lost.
Data is stored on hard disk: read and write speed is slower than memory, but power-off data will not be lost.
RDB persistence
RDB is the default persistence mechanism for redis. RDB is the equivalent of a snapshot, saving a state of data. (dozens of gigabytes of data can be saved as snapshots of several KB)
Snapshots are the default persistence method. This way is to write the in-memory data to the binary file as a snapshot, and the default file name is dump.rdb (which exists in the redis.conf file).
Advantages:
Snapshot saves data very fast, restores data very fast
Suitable for disaster backup
Disadvantages:
Small memory machines are not suitable for use. If the RDB mechanism meets the requirements, snapshots will be taken and memory will be occupied.
AOF persistence
Because the snapshot mode is done at regular intervals, if redis goes down unexpectedly, all modifications since the last snapshot will be lost. If the application requires that no changes can be lost, AOF (Append-Only File) persistence can be used.
The appendonly yes command enables AOF persistence.
There are three ways to do the following (default: fsync once per second)
Appendfsync always writes to disk as soon as it receives a write command, the slowest, but guarantees full persistence
Appendfsync everysec writes to disk once a second, making a good tradeoff between performance and persistence
Appendfsync no is completely dependent on OS and has the best performance, but persistence is not guaranteed
Advantages:
AOF has better persistence than snapshots because when using AOF persistence: redis appends every write command received to the file through the write function (the default is appendonly.aof). When redis restarts, it rebuilds the contents of the entire database in memory by re-executing the write command saved in the file.
Disadvantages:
The AOF approach also brings another problem. Persistent files will get bigger and bigger, taking up the hard disk. For example, if we call the incr test command 100 times, all 100 commands must be saved in the file, but 99 of them are redundant.
The above is all the content of the article "Redis publish and subscribe demonstration, transaction demonstration, persistence method". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.