Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Background
3.
Running scripts
4.
Sandboxed script context
5.
Maximum execution time
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Programmability in Redis

Author Manan Singhal
0 upvote

Introduction

Redis has a programming interface that allows you to run custom scripts directly on the server. You can use Redis Functions to manage and run your scripts with Redis 7 and later. In Redis 6.2 and earlier, you utilise Lua programming and the EVAL command to program the server.

Background

Redis is a "domain-specific language for abstract data types". Redis has its language, which is made up of commands. The majority of the commands focus on altering core data kinds in various ways. In many circumstances, these commands provide all of the capability needed by a developer to manage Redis application data.

The term programmability in Redis refers to the server's ability to run any user-defined logic. Scripts are what we call logic chunks like this. Scripts, in our situation, allow us to process data where it is stored, also known as data localization. Furthermore, properly implementing programmatic workflows in the Redis server can reduce network traffic and improve overall performance. Developers can use this functionality to create sophisticated, application-specific APIs. Business logic can be encapsulated in these APIs, and a data model can be maintained across numerous keys and data structures.

An embedded, sandboxed scripting engine runs user scripts in Redis. Redis currently only supports the Lua 5.1 interpreter as a scripting engine.

Running scripts

Redis has two methods for executing scripts.

The EVAL command, available since Redis 2.6.0, allows you to run server-side scripts. Eval scripts are a simple and quick approach to having Redis run your scripts on demand. Using them, however, implies that the scripted logic is part of your program (not an extension of the Redis server). Every applicative instance that runs a script must have the script's source code available at all times for loading. Because scripts are only cached by the server and are volatile, this is the case. This strategy may become more difficult to build and maintain as your application expands.

Second, Redis Functions, introduced in version 7.0, are effective scripts that are first-class database items. As a result, functions isolate scripting from application logic, allowing scripts to be developed, tested, and deployed independently. To use functions, they must first be loaded, after which they will be available to all connected clients. In this approach, deploying a function to the database becomes an administrative operation (like installing a Redis module), separating the script from the application.

When a script or function is run, Redis ensures that it is executed atomically. Similar to the semantics of transactions, the script's execution blocks all server activities for the duration of its execution. According to these semantics, all of the script's consequences are either yet to occur or have already occurred. An executing script's blocking semantics apply to all connected clients.

It's worth noting that the potential drawback of this blocking method is that it's not a good idea to run slow scripts. Because scripting has a low overhead, it is simple to write rapid scripts. If you want to employ a sluggish script in your app, keep in mind that all other clients will be blocked and won't be able to run any commands while it's running.

Sandboxed script context

Redis creates a sandbox for the engine that runs user programs. The sandbox is designed to avoid unintentional usage and reduce potential dangers from the server's surroundings.

Scripts should never attempt to access the Redis server's underlying host systems, such as the file system or network, or make any other system calls not supported by the API.

Scripts should only work with data saved in Redis and data passed to them as arguments.

Maximum execution time

Scripts have a time limit on how long they can run (set by default to five seconds). The default timeout is large because a script normally executes in less than a millisecond. The restriction was put in place to deal with unintentional endless loops during development.

Either using redis.conf or the CONFIG SET command, you can change the maximum time a script can be performed with millisecond precision. The busy-reply-threshold configuration setting affects the maximum execution time.

When a script passes the timeout threshold, Redis does not automatically end it. This would breach Redis's contract with the scripting engine, which assures that scripts remain atomic. Interrupting the execution of a script may result in changes to the dataset that is only partially written.

As a result, if a script runs longer than the configured timeout, the following occurs:

  • Redis logs that a script has been running for an excessive amount of time.
  • It resumes receiving commands from other clients, but all clients providing standard commands will receive a BUSY error. SCRIPT KILL, FUNCTION KILL, and SHUTDOWN NOSAVE are the only commands that work in this situation.
  • The SCRIPT KILL and FUNCTION KILL commands can be used to terminate a script that only executes read-only commands. Because no data has yet been written to the dataset by the script, these commands do not violate the scripting semantics.
  • The only command allowed if the script has already done a single write operation is SHUTDOWN NOSAVE, which ends the server without storing the current data set to disc (basically, the server is aborted).

Frequently Asked Questions

  1. What is Redis?
    Redis is a key-value data storage and cache that is open-source. It's also known as a data structure server because the keys include hashes, sets, lists, sorted sets, and strings.
     
  2. What is the meaning of Redis?
    Redis stands for REmote DIctionary Server.
     
  3. What language is Redis written in?
    Redis is a cache solution and session management system developed in ANSI C. It generates one-of-a-kind keys for storing data.
     
  4. What is the usage of Redis?
    Redis is a key-value store database that may be used as a NoSQL database or a memory cache store to boost performance when delivering data from system memory.
     
  5. What are the ways to interact with Redis?
    After the server has been installed, you can either use the Redis Client given by the Redis installation or open a command prompt and type the following command: redis-cli

Conclusion

In this article, we have learned the programmability in Redis. We hope that this blog will help you understand the concept of the Redis programmability, and if you would like to learn more about it, check out our other blogs on redismongo-dBdatabase, and operational database.

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass