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
-
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.
-
What is the meaning of Redis?
Redis stands for REmote DIctionary Server.
-
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.
-
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.
-
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 redis, mongo-dB, database, and operational database.
Attempt our Online Mock Test Series on Coding Ninjas Studio now!
Happy Coding!