In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the four characteristics and principles of redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. The expiration of redis A. Application scenario
Cookie expires automatically, limiting the number of visits per minute
b. Mode of realization
Setex (String key, int seconds, String value)
Expire key time # seconds
Pexpire key time # Ms
Expireat key time # seconds
Pexpireat key time # Ms
c. Realization principle
Regular cleaning
When you set the key expiration time, create a timer, cycle around, and delete the key as soon as the time is up.
Pros: expired key is deleted immediately, with the highest memory usage.
Cons: if there are 10, 000 key with an expiration time, you need 10, 000 timers, which loops constantly, which can cause a serious loss of cpu performance.
Lazy cleaning
When fetching key, first determine whether the key has expired, and if it expires, delete it.
Pros: expired key, which is deleted only when it is used, takes up the least amount of cpu.
Cons: if a key has not been used for a long time after it expires, the key will always take up memory space and will not be released.
Regular cleaning + lazy cleaning
Redis takes this approach. Through the first two strategies, we can see very clearly. Regular cleaning and lazy cleaning can not achieve the best effect.
Now there is a third type, which is to clean up every once in a while to remove the expired key. For example, clean up every minute or ten minutes.
At the same time, use lazy cleaning, each time you get the key, you can determine whether it is expired and whether it needs to be cleaned.
2. Publish and subscribe to redis
a. Application scenario
Subscribe to news channels (sports, military, etc.), follow Big V, follow friends, system news broadcast.
b. Mode of realization
PUBLISH channel message # publish messages
SUBSCRIBE channel [channel...] # subscribe to one or more channels
PSUBSCRIBE channel # subscribe to a set of matching channels
c. Realization principle
Pubsub_channels stores channel information, and pubsub_patterns stores client information. Through a similar observer pattern, new messages are sent to each subscriber.
III. Redis's affairs
Comparison between redis transaction and mysql
RedismysqlmultibeginexeccommitdiscardrollbackwatchlockA. Application scenario
Kill the number of goods in seconds, grab tickets
b. Mode of realization
Watch multi exec discard
c. Realization principle
The implementation principle of watch:
When a key is modified, the dirty identity is set for all clients that monitor the key.
The implementation principle of multi and exec:
Three states of the client
The REDIS_MULTI client enters the transaction state.
Invalid state of REDIS_DIRTY_EXEC client transaction. There is an error command in the command queue.
The REDIS_DIRTY_CAS client transaction is in an insecure state. Watch's key has been preemptively modified by other clients.
Transaction execution principle:
1. The transaction was executed successfully.
Step 1: the client sets the transaction status to REDIS_MULTI
Step 2: add multiple commands to the command queue
Step 3: execute exec and execute all the operation commands in the command queue in turn
two。 The first transaction execution failed REDIS_DIRTY_EXEC.
Step 1: the client sets the transaction status to REDIS_MULTI
Step 2: add multiple commands to the command queue, but there are commands that produce errors in the command queue
Step 3: incorrect operation command, which causes the client state to become REDIS_DIRTY_EXEC and the transaction execution fails.
3. The second type of transaction execution failed REDIS_DIRTY_CAS
Step 1: the client sets the transaction status to REDIS_MULTI
Step 2: add multiple commands to the command queue
Step 3: the key being watch is preemptively modified by other clients, causing the client state to become REDIS_DIRTY_CAS and the transaction execution failed.
4. PiplineA of redis. Application scenario
It is recommended when a client can merge multiple commands and send them together. For example, take out the user information and take out the list of users that the user follows.
However, if there are too many commands (the return time is more than 1 second), it can be split into two client to send separately.
b. Mode of realization
Pipline is not available in the command and can be set through the java client (jedis).
Pipeline pipe = jedis.pipelined ()
Pipe.set
c. Realization principle
Pipeline reduces round-trip latency by reducing the number of communications between client and redis-server.
If the round trip time is 10ms, a thousand round trips is 10 seconds. With pipline, the round trip time is 20ms.
The principle of Pipeline implementation is queuing, that is, first in, first out. The sequence of the data is guaranteed.
This is the end of the introduction of "what are the four characteristics and principles of redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.