This article is a mirror article of machine translation, please click here to jump to the original article.

View: 29611|Reply: 2

[Redis] Redis uses Lua scripts for detailed explanations

[Copy link]
Posted on 4/30/2021 3:42:57 PM | | | |
Regarding redis, everyone often uses it, and the most used scenario is data caching.

review

Redis Persistence Difference Between RDB and AOF
https://www.itsvse.com/thread-9555-1-1.html

The Docker installation runs the Redis cache
https://www.itsvse.com/thread-8995-1-1.html

Examples explain what Redis cache penetration, cache avalanche, and cache breakdown are
https://www.itsvse.com/thread-8968-1-1.html

redis wildcards to delete keys in bulk
https://www.itsvse.com/thread-7957-1-1.html

CentOS 7 installation Redis 5.0.3 tutorial
https://www.itsvse.com/thread-7201-1-1.html

Install the redis extension under php 5.5.7 under CentOS
https://www.itsvse.com/thread-7200-1-1.html

How many keys can be stored in a redis instance, and what are the maximum keys and values?
https://www.itsvse.com/thread-6848-1-1.html

The issue of redis Chinese cannot be displayed properly
https://www.itsvse.com/thread-5032-1-1.html

Redis enables remote access
https://www.itsvse.com/thread-5011-1-1.html

Windows fails to start the Redis service, error 1067: Process terminates unexpectedly.
https://www.itsvse.com/thread-5010-1-1.html

CentOS installed Redis 4.0.8
https://www.itsvse.com/thread-4614-1-1.html

Redis sets up remote connection and access passwords
https://www.itsvse.com/thread-4101-1-1.html

redis empties the data cache
https://www.itsvse.com/thread-4027-1-1.html

redis persistence configuration and off persistence
https://www.itsvse.com/thread-4012-1-1.html

The difference between save and bgsave in redis
https://www.itsvse.com/thread-4010-1-1.html

Redis two persistence methods and principles
https://www.itsvse.com/thread-4009-1-1.html

Redis has three ways to start
https://www.itsvse.com/thread-4008-1-1.html

Redis method of hiding command-line windows
https://www.itsvse.com/thread-2988-1-1.html

Redis Hash Hash Hash Value Problem
https://www.itsvse.com/thread-2587-1-1.html

5 Things You Must Know Before Using Redis
https://www.itsvse.com/thread-2580-1-1.html

Redis modifies the default port number and sets the access password
https://www.itsvse.com/thread-2577-1-1.html

Redis windows 64-bit download, official download address
https://www.itsvse.com/thread-2576-1-1.html
Starting with redis 2.6.0, redis has a built-in Lua interpreter and provides an eval command to parse the Lua script evaluation.

Syntax: eval script numkeys keys args

Parameters:

eval — Redis provides commands to parse the lua script

script — lua script

numkeys — Specifies the number of keys in the keyname parameter set

keys — The keyname parameter set, represented by the global variable KEYS array, starting with a subscript of 1

args — A set of key-value parameters, represented by an array of global variables ARGV, starting with a subscript of 1


Advantages of using Lua in Redis

Reduce network overhead. Multiple requests can be sent at once in the form of scripts to reduce network latency
Atomic manipulation. redis will execute the entire script as a whole, with no other commands inserted in between. Therefore, there is no need to worry about race conditions and transactions during the scripting process.
Reinstated. The footsteps sent by the client are persistently stored in redis so that other clients can reuse the script without having to use code to complete the same logic.

Script atomicity

Lua scripts cannot have time-consuming operations or dead loops, otherwise redis will not accept other commands and execute to stop the script running

Redis uses a single Lua interpreter to run all scripts, and ensures that the scripts are executed atomically.This means that when a script is running, no other scripts or redis commands will be executed! Therefore, if the current script is running slowly, the server may not be able to execute the command because it is busy, such as:

Each script has a maximum execution time limit, the default value is 5s. The maximum execution time is controlled by the lua-time-limit option of the configuration file redis.conf, or directly by using the config get and config set commands. When a script execution reaches its maximum execution time, Redis does not actively terminate it, it performs the following steps:

(1) Redis records that a script is running out of time

(2) Redis starts to re-accept requests from other clients, but only accepts the execution of script kill commands and shutdown nosave.

(3) If the script only performs read operations, use the script kill command to stop the script immediately; If the script performs a write operation, only the shutdown save/nosave command is allowed to stop the server to prevent the current data from being written to disk. (At this point, the server is down and the data will not be saved)


example

Execute the script, the parameters are 2 key and value, and the command is as follows:


Deadloop scripts, executing the following script will cause redis to be unable to process other commands and get stuck:

Try using a script to add data of type string with the following command:

Execute some more complex scripts, if the value of the key is equal to the value we passed in, then delete the cache, otherwise do anything, the command is as follows:

The results of the implementation are as follows:



(End)




Previous:.NET/C# Lock Principle Monitor provides an in-depth explanation
Next:IIS DELETE PUT request 405 solution
 Landlord| Posted on 5/10/2021 9:18:34 AM |

The hyperlink login is visible.
The Redis Script Load command is used to add script script to the script cache, but it is not executed immediately.

The EVAL command also adds the script to the script cache, but it evaluates the input script immediately.

If the given script is already in the cache, then no action is taken.

After the script is added to the cache, the script can be called using the script's SHA1 checksum via the EVALSHA command. (This also means.)The same script executes the same sha1 value on any different redis server

Scripts can remain in the cache for an infinite amount of time until the SCRIPT FLUSH is executed.



 Landlord| Posted on 5/12/2021 11:38:55 AM |
Determine if the lua script is in the cache


After Redis restarts, the Lua script is automatically cleared, will not be permanent

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com