Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Extending Redis With Lua Scripting
2.1.
Running Scripts
2.2.
Sandboxed script context
2.3.
Maximum Execution Time
3.
What is Wrong with Eval Script
4.
What are Redis Functions
4.1.
Loading an Empty Library 
4.2.
Register a Single Function
4.3.
Call the Registered Function
5.
Frequently Asked Questions
5.1.
What is Redis?
5.2.
What is Redis Scripting?
5.3.
Redis is written in what language is it supported on all platforms?
5.4.
What is the Maximum Execution Time?
5.5.
What are Redis Functions?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Redis Functions

Author Gaurav joshi
1 upvote

Introduction

In this blog, we will discuss Redis functions. But before going into that first, I would like you to know what Redis stack is and why it is popular.

Redis is defined as an open-source (BSD licensed), stored data structure in memory and used as a database, cache, message broker, and streaming the engine. Redis provides us with data structures like strings, lists, hashes, sets, bitmaps, streams, hyper logs etc. Functions like ReplicationLua ScriptingIRU evictionTransaction and on-disk persistence of different levels are built-in in Redis. Redis is written in ANSI C language, and it works on most of the POSIX systems like Linux, *BSD, and Mac OS X.

Redis Functions refers to the API used for managing code that needs to be executed on the server. Redis Functions is an extension of EVAL in previous versions of Redis.

Extending Redis With Lua Scripting

Redis helps us provide the interface for programming that lets us execute our custom scripts on the server. In Redis 6.2 and its version below, we use Lua scripting with the EVAL command to program the server. After Redis 7, we use Redis Functions to manage and run our scripts.

Programmability in Redis is the ability to execute arbitrary logic defined by the user in the server. Such pieces of logic are known as scripts. In our case, scripts enable the data processing where it lives, a.k.a, data locality.

Running Scripts

Redis provides us with two means for running the scripts.

Firstly, and ever since the Redis 2.6.0, the EVAL command enables us to run server-side scripts. It provides us with a quick and straightforward way to have Redis run into our scripts ad-hoc.

Secondly, added in v7.0 and afterwards, Redis Functions are essentially scripts that are first-class database elements. Such functions decouple scripting from application logic and enable independent development, testing, and deployment of scripts. To use functions, they need to be loaded first, and then they are available for use by all connected clients. 

Sandboxed script context

Sandbox helps us prevent accidental misuse and reduce potential threats from the server's environment. Redis places the engine that executes user scripts inside a sandbox. Scripts should operate solely on data stored in Redis and data provided as arguments for their execution.

Maximum Execution Time

There is a default timeout for scripts to run. Scripts are subjected to a maximum execution time (usually the default value set by five seconds). The default timeout is usually enormous since most scripts run in less than a millisecond. The timeout limit is placed to handle accidental infinite loops.

What is Wrong with Eval Script

Redis functions are available after Redis 7. Prior versions to that scripting in Redis are available through the EVAL command that allows execution of the Lua script by the server. Executing our application logic inside Redis efficiently and automatically is the core use case for the Eval Scripts. These scripts help us perform conditional updates across multiple keys, perhaps combining several different data types.

What are Redis Functions

Redis Functions provide us with the same core functionality as scripts but are also first-class software artefacts of the databases. Redis manages these functions as an integral part of the database and ensures they are available via data persistence and replication. Functions are part of the database and thus need to be declared before use. Applications aren't required to load them during runtime nor risk aborted transactions. An application that uses functions depends only on its APIs rather than on the embedded script logic in the database.

Redis Functions are used to determine between the programming language used for writing functions and their management by the server. Lua is the only language interpreter that Redis supports presently as an embedded execution engine. Below is the command for the individual function mentioned.

Loading an Empty Library 

redis> FUNCTION LOAD "#!lua name=mylib\n"
(error) ERR No functions registered

Register a Single Function

#!lua name=mylib
redis.register_function(
  'knockknock',
  function() return 'What is your Name?' end
)

Call the Registered Function

redis> FUNCTION LOAD "#!lua name=mylib\nredis.register_function('knockknock', function() return 'Who\\'s there?' end)"
mylib
redis> FCALL knockknock 0
"Who's there?"

Frequently Asked Questions

What is Redis?

Redis is defined as an open-source (BSD licensed), stored data structure in memory and used as a database, cache, message broker, and streaming the engine.

What is Redis Scripting?

Redis is very popular and is used by most tech giants like Twitter, Github, Snapchat, Stack Overflow etc.

Redis is written in what language is it supported on all platforms?

Redis is written in ANSI C language, and it works on most the POSIX systems like Linux, *BSD, and Mac OS X.

What is the Maximum Execution Time?

Maximum Execution Time is a default timeout for scripts to run. Scripts are subjected to a maximum execution time (usually the default value set by five seconds). The timeout limit is placed to handle accidental infinite loops.

What are Redis Functions?

Redis Functions are used to determine between the programming language used for writing functions and their management by the server. It provides us with the same core functionality as scripts but is also a first-class software artefact of the databases. Redis manages these functions as an integral part of the database and ensures they are available via data persistence and replication.

Conclusion

In this article, we have extensively discussed Big Structured Data in detail. We have also briefly explained different sources of Big Structured Data. 

We hope that this blog has helped you enhance your knowledge regarding Big Data. To learn about Big Data and Hadoop, practice some quality SQL questions and visit our blogs on Databases in Coding Ninjas Studio. If you are preparing for an interview, visit our Interview Experience Section. To become more confident in DSA, try out Interview Problems in our Code studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass