Advanced Application Redis: Redis + Lua script to achieve combined operation

I. Introduction

Redis is a high-performance key-value database, to a large extent overcome the lack of such memcached key / value stored in the part of the scene, it is a good complement to relational databases. Thanks to ultra-high performance and rich data structures, Redis has become the first choice for key-value storage system architecture design.

Although Redis official website provides more than 200 commands, but still can not do the programming time to avoid a small step in order to achieve business logic and multiple calls to the Redis.

To compare and set the scene for example. If Redis native commands need to be obtained from the key Redis then extracted values ​​therein for comparison: if equal treatment is not done; If not equal, then the absence of key or key set to a target value. Only a single point of operation need to compare and set two with Redis communication.

Further, by using this dispersion operation is not characteristic of Redis atoms, occupy multiple network IO.

Today we'll explore how to gracefully deal with the scenario described above.

Two, Redis and Lua

Before introducing Lua, we need to have a preliminary understanding of the language. Lua is a small scripting language that can run on almost all operating systems and platforms. We generally do not use Lua exceptionally complicated matters, so just to understand the basic syntax of some of lua.

After the advent of Redis, its developers are aware of the problem mentioned at the beginning, so Redis from version 2.6 supports Lua script. The new version of Redis also supports Lua Script debug, little interested partners can find the corresponding presentations and QuickStart Quguan Documentation web.

Once you have Lua script, will be able to achieve significantly improved in the following aspects when using Redis program:

  • Reduce network overhead: the network had requested operation N times, a request can be completed. Logic N times the original request is completed on Redis server, network round-trip delay is reduced;
  • Atomic operations: Redis entire script will be executed as a whole, can not be inserted into the middle of other commands. This is an important feature, it must bring a small notebook remember well. As for why is an atomic operation, we later analysis;
  • Multiplexing: The script is permanently stored in the client sent in Redis. So that other clients can reuse this script, without the need to use the code to accomplish the same logic.

So now a popular saying: If you want to learn Redis, will certainly Lua Script.

Third, to achieve compare and set by Lua script

Next, we will implement a simple compare and set, and by this example to feel Redis Lua script to use to bring a new experience.

First look at how to make Redis Lua script execution.

3.1 Redis of EVAL

Redis 127.0.0.1:6379> EVAL script  numkeys key [key ...] arg [arg ...]

 

  • script: parameter is a piece of Lua 5.1 script. Script does not (and should not) be defined as a Lua function.
  • numkeys: keys for designating the number of parameters.
  • key [key ...]: From the third parameter EVAL start counting, said in the script used in the Redis key (key). In Lua, these keys can be a parameter in the form of base address of access (KEYS [1], KEYS [2], and so on) through the global variable KEYS array with.
  • arg [arg ...]: additional parameters, in Lua accessed through the global variable ARGV array, and a form accessible KEYS variable similarly (ARGV [1], ARGV [2], and so on).

Here borrow example official website.

Above script directly returned to the Senate.

  • eval is Redis key;
  • Contents of the first quotes is Lua script;
  • 2 is the number of parameters;
  • key1 and key2 are KEYS [1], KEYS [2] of the parameter;
  • first and second are ARGV [1], ARGV [2] of the reference.

It can simply KEYS [1], KEYS [2], ARGV [1], ARGV [2] is understood as a placeholder.

3.2 execute the script file and cache script

If you can only write the script executes the command line, you encounter complex script would not be crazy?

Here we look at how to make Redis Lua script file to perform, but also to test the characteristics of alternate lua script (after we no longer need periodic batch delete some of the key conform to specific rules).

Redis 127.0.0.1:6379> SCRIPT LOAD  script
Redis 127.0.0.1:6379> EVALSHA sha1  numkeys key [key ...] arg [arg ...]

 

Redis provides a SCRIPTLOAD commands, following the command script is the Lua script. Add the script command script to script cache, but it does not execute the script immediately. After the execution command, the Redis SHA1 returns a string EVALSHA second command can be executed.

Note that the script can keep unlimited time in the cache until executing the SCRIPT FLUSH. We look at the effect.

Redis also supports direct execution of Lua script file. First, write and store a Lua script.

Then call Redis-cli -eval command

Redis-cli -eval command syntax is basically the same as the original eval syntax.

3.3 Lua script to achieve compare and set

compareand set implementation logic is this: in the first acquired Redis specified key value, and then compared with a given value: if they are equal, then the key as the target value and returns an identifier; if not equal, not any operation and returns an identifier.

if Redis.call('get', KEYS[1]) == ARGV[1]  then
     Redis.call('set', KEYS[1], ARGV[2]);
     return 1
else
     return 0 end

 

Let's test this script.

First to specify the key compareAndSet Redis: key value written to a value

Execute lua scripts in Redis

We can see the first execution returns 1, indicating that modification was successful; 0 if executed again using the original parameters, instructions did not make any changes. Let us check what compareAndSet: key the key

You can see compareAndSet: key The key has been modified to the new_value.

IV Summary

We implemented a simple compareAndSet operated by lua script.

Let us through this example to verify the characteristics mentioned at the beginning.

  • Reduce network overhead: without a script, we achieve a compareAndSet requires at least two interactions with Redis, and now only need to perform one operation can be completed;
  • Atomic operations: thanks Redis design, the entire script Redis will perform as a whole, will not be inserted into the middle of other commands. Therefore, in the process of writing the script without worrying about race conditions occur, without the use of a transaction, interested can Baidu or wait for later follow-up article updates;
  • Multiplexing: a series of operations can be packaged into a Lua script, stored on a file or Redis, you can directly call the next time.

Read here, I hope you have some understanding of Redis + Lua, and can use a simple script to complete some complex operations. Follow-up will continue to update some Lua script + based distributed data structure java program implementation, such as the delay queue, reentrant locks, etc., may be of interest to a small partner sustained attention.

Author: Li Chong

Original starting UAVStack intelligent operation and maintenance

Source: CreditEase Institute of Technology

Guess you like

Origin www.cnblogs.com/yixinjishu/p/11314028.html