Building Redis-Modules with JavaScript using Cthulhu

Cthulhu is an interesting project. It allows you to code custom redis-modules in JavaScript. This is very interesting because JS is very productive and easy to write code.

You might be wondering how they made this efficient? Well, the secret is a project called Duktape which is an efficient JS engine written in C. So Cthulhu + Duktape will pre-compile your javascript function and run it on C which will be efficient and will be able to talk with redis core.

Unfortunately, not all functions are available -- you can check the ones that are available here: List of available APIS.

You can so a simple comparison with a module In C I wrote some time ago in this blog post. C is way more powerful but also way more error-prone and might take more time to write if you are not used to it. So for this Blog Post, I will how to download and compile Cthulhu and they install in Redis 4 and code some custom function in JavaScript. Let's get started.




Building Redis-Module in JavaScript



So here we are downloading and building Redis 4.0.6(Redis modules was introduced on Redis 4) and then we are making Cthulhu so Lib. As you can see we just need to build Cthulhu one time and then we link Cthulhu with our custom js function.

There are 3 JS functions: my_incr, my_append and echo. The function my_incr gets a key turns the content into a number and the increment 1 to the number and set back to the key and return to the user.

Second function my_append does text appending on existing key. Getting the current value and appending the parameter. Both functions already exist so they are here just to show how easy is to work with JS and Redis.

The last function is just an echo -- Whatever string you send you will receive back. This functions can be called on the Redis-CLI but also by any client like Jedis(>=3.0.0-SNAPSHOT) or Lettuce(4.x,5.x) versions. So its possible to call this commands by any language.

Cheers,
Diego Pacheco

Popular posts from this blog

Kafka Streams with Java 15

Rust and Java Interoperability

HMAC in Java