In the last issue, Xiao Zhihe and a Yin were conducting an interview and Q & A. However, they were tired and wanted to have a rest before they were asked. However, they thought it was over?
Of course not. You have to continue.
NOTE: For the first type, the application needs to process resource synchronization by itself. The available methods are quite common. You can use synchronized or lock. For the second type, you need to use the setnx command of redis, but pay attention to some issues.
Features of transactions in redis:
All commands in the transaction will be executed in a serialized order. During the transaction execution, redis will not provide any service for requests from other clients, this ensures that all commands in a transaction are executed by atoms.
Compared with a transaction in a relational database, if a command fails to be executed in a redis transaction, the subsequent command will still be executed.
We can use the multi command to start a transaction. People with experience in relational database development can understand it as a "begin transaction" statement. The commands executed after this statement are considered as operations in the transaction. Finally, we can commit/roll back all operations in the transaction by executing the exec/Discard command. The two redis commands can be considered to be equivalent to the commit/rollback statements in relational databases.
Before the transaction is started, if the communication between the client and the server fails and the network is disconnected, all the statements to be executed will not be executed by the server. However, if the network interruption occurs after the client executes the exec command, all the commands in the transaction will be executed by the server.
When the append-only mode is used, redis writes all write operations in the transaction to the disk by calling the system function write. However, if a system crash occurs during the write process, such as a power failure, only some data may be written to the disk, but other data may be lost.
The redis server performs a series of necessary consistency checks when it is restarted. Once a similar problem is found, it immediately exits and provides an error message.
In this case, we must make full use of the redis-check-Aof tool provided in the redis toolkit. This tool can help us locate data inconsistency errors, and roll back some data that has been written. After the repair, we can restart the redis server again.
The above code can ensure that the execution result is correct only when a single connection is made, because if multiple clients execute this code at the same time, in this case, race condition is a common error scenario in multi-threaded programs ).
For example, both client a and client B read the original value of mykey at the same time. If the value is 10, then both clients add this value to the redis server and set it back to the redis server, in this way, the mykey result is 11 instead of 12. To solve similar problems, we need to use the watch command for help. See the following code:
WATCH mykeyval = GET mykeyval = val + 1MULTISET mykey $valEXEC
Unlike the previous Code, the new Code monitors the key through the watch command before obtaining the value of mykey, and then wraps the SET command in the transaction, this effectively ensures that the value of the mykey obtained by the current connection is modified by the client of another connection before executing exec. The exec command of the current connection fails to be executed. In this way, the caller can determine whether the Val has been reset successfully after determining the return value.
Working Principle
Redis forks.
The child process starts to write data to the temporary RDB file.
When the sub-process completes writing the RDB file, replace the old file with the new file.
This method enables redis to use the copy-on-write technology.
The VM-max-threads parameter can be used to set the number of threads accessing the swap file. It is recommended that the number of threads not exceed the number of machine cores. If it is set to 0, all operations on swap files are serial. it may cause a long delay, but it guarantees data integrity.
When I tested it myself, I found that the virtual memory performance was good. If the data volume is large, you can consider distributed databases or other databases.
1. operations that affect the survival time
You can use the del command to delete the entire key to remove the survival time, or use the set and GetSet commands to overwrite the original data. That is to say, after the value corresponding to the modified key and the same key and value are used to overwrite the data, the survival time of the current data is different.
For example, executing the incr command on a key, executing the lpush command on a list, or executing the hset command on a hash table does not change the key's survival time. On the other hand, if you use Rename to rename a key, the renamed key will have the same survival time as before.
Another possibility of the RENAME Command is to change the name of a key with a life time to another another_key with a life time. In this case, the old another_key (and its life time) will be deleted, and the old key will be renamed as another_key. Therefore, the survival time of the new another_key is the same as that of the original key. You can use the persist command to remove the survival time of the key without deleting the key, so that the key becomes a persistent key again.
2. How to update the survival time
You can execute the expire command on a key with a life time. The new life time will replace the old one. The precision of the expiration time has been controlled within 1 ms. The complexity of the primary key expiration time is O (1). the expire and TTL commands can be used together to check the current survival time of the key. If the setting is successful, 1 is returned. If the key does not exist or the survival time cannot be set for the key, 0 is returned.
Maximum Cache Configuration:
In redis, you can set the maximum memory usage, server. the default value of maxmemory is 0, and the maximum cache is not specified. If new data is added and the maximum memory is exceeded, redis will crash, so you must set it. When the redis memory data set size increases to a certain size, the data elimination policy will be implemented.
Redis provides six data elimination policies:
Volatile-LRU: selects the least recently used data from the data set with an expiration time (server. DB [I]. expires)
Volatile-TTL: select the data to be expired from the data set with an expiration time (server. DB [I]. expires)
Volatile-random: removes any data from a dataset (server. DB [I]. expires) with an expiration time set.
Allkeys-LRU: selects the least recently used data from the dataset (server. DB [I]. dict)
Allkeys-random: Any data elimination from the dataset (server. DB [I]. dict)
No-enviction: Data eviction prohibited
Note that volatile and allkeys specify whether to discard data from a specified data set with an expiration time or from all data sets, the following LRU, TTL, and random are three different elimination strategies, plus a no-enviction policy that will never be recycled.
Rules:
If the data shows a power law distribution, that is, the Access Frequency of some data is high, and the Access Frequency of some data is low, the allkeys-LRU
If the data is evenly distributed, that is, all data access frequencies are the same, allkeys-random is used.
Three data elimination policies:
TTL and random are easier to understand and implement. The primary reason is that LRU uses the elimination strategy at least recently. In Design, keys are sorted by the expiration time, and then the key that is first invalid is used for elimination.
1. session cache)
The most common scenario for using redis is session cache ). The advantage of using redis cache sessions over other storage systems (such as memcached) Is that redis provides persistence. When maintaining a cache that does not strictly require consistency, most people will be unhappy if all the information about the user's shopping cart is lost. Will they still be like this?
Fortunately, with the improvement of redis over the years, it is easy to find out how to properly use redis to cache session documents. Magento, a well-known commercial platform, also provides redis plug-ins.
2. full-page cache (FPC)
In addition to basic session tokens, redis also provides a simple FPC platform. Return to the consistency problem. Even if the redis instance is restarted, the page loading speed will not be decreased due to disk persistence. This is a great improvement, similar to the local FPC of PHP.
Taking magento as an example, magento provides a plug-in to use redis as the full-page cache backend.
In addition, for Wordpress users, Pantheon has a very good plug-in WP-redis, which can help you load the pages you have browsed as quickly as possible.
3. Queue
Reids provides list and set operations, which enables redis to be used as a good message queue platform. The Operations used by redis as a queue are similar to the push/pop operations on the list by local programming languages (such as Python.
If you quickly search for "redis queues" in Google, you will be able to find a large number of open-source projects that are designed to use redis to create excellent backend tools, to meet various queue requirements. For example, celery uses redis as the broker in the background. You can check it here.
4. rankings/counters
Redis is able to increment or decrease numbers in memory. Set and sorted set make it easy to execute these operations. redis only provides these two data structures.
Therefore, we need to get the top 10 users from the sorting set-we call it "user_scores". We just need to execute the command as follows:
Of course, it is assumed that you are performing incremental sorting based on your user scores. If you want to return the user and user scores, You need to execute the following command:
ZRANGE user_scores 0 10 WITHSCORES
Agora games is a good example. It is implemented in Ruby, and its ranking is to store data using redis. You can see it here.
5. Publish/Subscribe
The last (but certainly not least) is the redis release/subscription function. There are many scenarios for publishing/subscription. I have seen people use it in social network connections. It can also be used as a publish/subscribe-Based Script trigger, or even use redis's publish/subscribe function to build a chat system! (No, this is true. You can verify it ).
Among all the features provided by redis, I feel that this is the least popular, although it provides users with this function.
Cartoon | redis FAQ (2)