5/11/2016 - 5:00 PM

Creating a redis Module in 15 lines of code!

A quick guide to write a very very simple "ECHO" style module to redis and load it. It's not really useful of course, but the idea is to illustrate how little boilerplate it takes.

Step 1: open your favorite editor and write/paste the following code in a file called module.c

#include "redismodule.h"
/* ECHO <string> - Echo back a string sent from the client */
int EchoCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  if (argc < 2) return RedisModule_WrongArity(ctx);
  return RedisModule_ReplyWithString(ctx, argv[1]);

/* Registering the module */
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
  if (RedisModule_Init(ctx, "example", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
  if (RedisModule_CreateCommand(ctx, "example.echo", EchoCommand, "readonly", 1,1,1) == REDISMODULE_ERR) {

Step 2: Download redismodule.h to the same directory. (

Step 3: Compile the module:

On Linux:

$ gcc -fPIC -std=gnu99 -c -o module.o module.c
$ ld -o module.o -shared -Bsymbolic -lc


$ gcc -dynamic -fno-common -std=gnu99 -c -o module.o module.c
$ ld -o module.o -bundle -undefined dynamic_lookup -lc

And you're done!

Step 4: Download the unstable version of redis and build it in the same directory, from: or

cd redis-unstable
make -j 4
cd ..

Step 5: Load the module from the directory it was build int:

$ ./redis-unstable/src/redis-server --loadmodule ./

(If you already have redis installed and running, you'll want to add --port 9999 or another free port.

Step 6: Try it out!

$ redis-cli EXAMPLE.ECHO "hello world"

The code is available at