Open In App

Complete Reference to Redis Scripting

Last Updated : 21 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Redis scripting is primarily implemented using the Lua programming language, enabling users to create custom server-side scripts that can be executed with a single command.

Understanding Redis Scripting

Redis scripting is a feature that enables you to run custom Lua scripts on the Redis server. These scripts can be atomic and perform multiple Redis commands in a single transaction-like operation. This is especially useful when you need to execute a series of commands that must be executed together without interference from other clients.

Redis Scripting Command

Redis scripting uses the Lua programming language, which is simple and easy to learn. Redis provides a few key functions and commands to work with scripts.

EVAL and EVALSHA

  • EVAL is used to evaluate a Lua script with arguments.
  • EVALSHA is similar to EVAL, but it uses the SHA1 hash of the script for faster execution if the script has been cached on the server.
  • KEYS is an array containing the keys passed to the script.
  • ARGV is an array containing the arguments passed to the script.
EVAL "return {KEYS[1],ARGV[1]}" 1 key1 value1

Example:

> SET mykey "GFG"
OK

> EVAL "return redis.call('GET', KEYS[1])" 1 mykey
"GFG"

SCRIPT LOAD

To load a Lua script onto the Redis server and get its SHA1 hash, you can use the SCRIPT LOAD command.

SCRIPT LOAD "return 'Hello, Geeks for Geeks!'"

EVAL

The EVAL command is used to execute a Lua script with specified keys and arguments.

EVAL "return 'Hello, ' .. KEYS[1]" 1 key1

EVALSHA

The EVALSHA command is similar to EVAL but uses the SHA1 hash of a pre-loaded script for faster execution.

EVALSHA <script_sha1> 1 key1

SCRIPT EXISTS

You can check if a script exists on the server using the SCRIPT EXISTS command.

SCRIPT EXISTS <script_sha1>

SCRIPT FLUSH

To remove all scripts from the script cache on the Redis server, use the SCRIPT FLUSH command.

SCRIPT FLUSH

SCRIPT KILL

This command is mainly useful to kill a script that is running for too much time.

SCRIPT KILL

Practical Examples of Redis Scripting

Let’s explore some practical examples of Redis scripting:

Example 1: Atomic Counter

In this example, we’ll create an atomic counter using Redis scripting. We’ll increment the counter by a specified value.

local key = KEYS[1]
local increment = tonumber(ARGV[1])

local current = tonumber(redis.call('GET', key) or '0')
current = current + increment

redis.call('SET', key, current)
return current

You can execute this script in Redis using the EVAL command, passing the key and the increment value as arguments.

Example 2: Leaderboard

Imagine Geeks for Geeks wants to create a leaderboard for coding challenges. Redis scripting can help manage the leaderboard efficiently.

Syntax:

local variable1 = ARGV[index1] -- Accessing the arguments passed to the script
local variable2 = ARGV[index2] -- Accessing additional arguments if needed

-- Perform operations using Redis commands and Lua logic
redis.call('REDIS_COMMAND', key, arg1, arg2, ...) -- Execute Redis commands within the script

-- Perform custom logic and operations
if condition then
-- Perform action
end

-- Return the result or value at the end
return result

Example:

local username = ARGV[1]
local score = tonumber(ARGV[2])
local currentScore = tonumber(redis.call('ZSCORE', 'leaderboard', username) or 0)
if score > currentScore then
redis.call('ZADD', 'leaderboard', score, username)
end
return redis.call('ZRANK', 'leaderboard', username)

This script checks if the request count exceeds the limit and returns a result indicating whether the request is allowed or not.

Uses of Redis Scripting

Redis scripting can be useful in various scenarios, including:

  • Atomic Operations: When you need to ensure that multiple operations are executed as a single atomic operation.
  • Complex Data Manipulation: When you want to perform complex data manipulations that are not possible with regular Redis commands.
  • Transaction-like Operations: When you need to perform multiple operations in a transaction-like manner, guaranteeing that either all or none of the operations will be executed.
  • Custom Aggregations and Analytics: When you want to perform custom aggregations or analytics on your data.
  • Custom Command Creating: When you need to create custom commands or functionalities that are not available in Redis natively.

Redis scripting is primarily used to execute Lua scripts within the Redis server. These scripts can help in implementing custom functionalities and complex operations on Redis data.

Conclusion

Redis scripting with Lua offers an elegant way to execute complex operations on the Redis server atomically. In this article, we covered the basics of Redis scripting, including its syntax, commands, and practical examples. With Redis scripting, you can create custom functionality tailored to your specific application needs, ensuring data integrity and performance while leveraging the power of Redis. Incorporate Redis scripting into your Redis-based applications to unlock advanced features and optimize your data operations.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads