Redis

Redis Module Load and Unload

Redis Modules Explained

Redis, by default, offers several data types and enormous commands to operate on these data structures. With the growth of the complexity of modern applications, the demand for the new Redis commands and structures are increasing.

Redis Modules are meant to fill this gap between the existing Redis functionalities and the increasing demand for new functional requirements. They can enhance the existing Redis functionalities with new commands and data structures that provide advanced functionalities without changing the Redis core code.

Most of the Redis modules are written in C language by exporting the C API as a single header file called “redismodule.h”. In addition, languages like C++ that support C bindings can be used to implement the Redis modules.

The given high-level illustration depicts how the modules integrate with Redis Core using Modules API. Furthermore, Modules API is where most of the effort was made. It abstracts and isolates the Redis internals from the modules and acts as the server’s C-binding contract. In addition, a module is registered in the Redis Core with a specific module API version.

As an example, you can create your own HELLOWORLD module with a PRINT command on top of the ECHO command that comes from Redis Core. It looks like the following:

As you can see in the illustration, you can always communicate or call the Redis Core commands and data structures with the help of Modules API and enhance the existing functionality with your own command or structure as in the HELLOWORLD module.

Most of the work is with module implementation with C language by consuming the Modules API when needed. Then, it is all about loading the compiled module to the Redis server at runtime or using the Redis.conf configuration file directive.

In this guide, we will not focus on module development with C or C++ but discuss more on loading and unloading of available Redis modules. Some of the widely popular Redis modules are listed in the following:

  • RediSearch – Full-text searching
  • RedsiJSON – The JSON data type for Redis
  • RedisGraph – A graph database for Redis
  • Neural Redis – A neural network

Redis Module Loading and Unloading

Today, a number of pre-developed modules can be found in the Redis modules library that are developed for different purposes and requirements. To consume one or multiple modules in your application, it needs to be loaded to the Redis server first.

Redis MODULE LOAD Command

The MODULE LOAD command is a built-in Redis command which is used to load a given module from a dynamic library. In addition, this command does the module initialization process as well. This command accepts a path argument that specifies the location of the module library with the module name. Most importantly, the command has constant time complexity which operates fast.

Syntax:

MODULE LOAD module_path [arg [arg ...]]

Module_path: The absolute location path to the library with the module file name.

Arg: Additional arguments can be passed to the module but it is not mandatory.

Before loading the module, it is a must to clone the module source code and build that will produce a “.so” file. This “.so” file’s location needs to be provided as the path argument.

If the module is loaded properly, the OK string reply is returned.

On the other hand, a loaded module may need to be unloaded as well. In that case, the module unloading can be done at runtime using the MODULE UNLOAD command.

Redis MODULE UNLOAD Command

The MODULE UNLOAD command unplugs an already loaded Redis module from the server. This command only accepts one mandatory argument which is the name of the module. The name of the module should be the one reported by the MODULE LIST command. So, it is important to keep in mind that this name may be different from the one displayed in the module library file name.

Syntax:

MODULE UNLOAD module_name

Module_name: The module name reported by the MODULE LIST command as shown in the following:

One thing to note is that the custom data structures which is registered with a module can’t be unloaded with the MODULE UNLOAD command.

Conclusion

To conclude, Redis modules are used to extend the functionality of the Redis core commands and structures. As mentioned, a module is a single unit that is built on top of existing Redis command(s) or structure(s) using C-binding languages like C or C++. It heavily utilizes the Redis modules API to communicate with core and other modules. According to the given explanation, to use an already implemented Redis module in your application, it needs to be loaded using the MODULE LOAD command at runtime. Similarly, the unloading is done by the MODULE UNLOAD command.

About the author

Nimesha Jinarajadasa

Being a Full-stack Senior Software Engineer for more than five years, I love technology, as technology has the power to solve our many problems within just a minute. I try to learn more and create more opportunities for this new world.