Add Redis to Your Serverless Application

How I gave my Alexa Skill a “memory” with Redis and OpenWhisk

--

The serverless revolution is definitely upon us. Personally, I’m finding a lot to love about this flexible, scalable tech. Working with it is as easy as working in established development toolchains. In particular, it easily integrates with other components, such as data storage, that I’d want to use in any other application. Recently I added a Redis feature to one of my serverless projects running on OpenWhisk, so I thought I’d share some of that code so you can see how the two interact.

Alexa, I need a new project name!

One of the first “toy” projects I used a serverless platform for was a “skill” for my Amazon Echo Dot. The skill creates a new GitHub project name for me — basically it spits out two random words. This project was brilliant fun. (It’s here on GitHub if you want to see the code.) I often found myself wanting to ask Alexa to repeat the hilarious combination of words she just came out with!

“Functional Mist” for a project name, anyone?

So Alexa needed a better memory. I amended my project to store the random words to Redis. Redis is an excellent, open source, in-memory datastore, and since it’s not exactly a crisis if I lose this data, it’s perfect for the Alexa-memory use case.

Get Redis-ready

I deploy my serverless OpenWhisk actions to the IBM Bluemix platform, so I’ll create a Redis instance there. To find it, go to the Catalog page for services, look for Redis and choose Compose for Redis, or use this direct link.

Shopping for a Redis instance in the Bluemix catalog

On the next screen, the Standard plan is perfect for my needs, so I’ll just click Create. Once created, the dashboard for this service will display a Connection String field. I’ll use this field in the next step, where I’ll add my connection information as a parameter to OpenWhisk.

New to OpenWhisk? Head over to http://openwhisk.incubator.apache.org/ and click the Get Started button. This guide will walk you through everything you need to configure the tools, including the wsk tool used here. You’ll also find resources for general serverless info, and tutorials for various use-cases.

The connection string I copied above needs to go into a file called params.json, along with any other parameters to pass to the action. Mine looks like this:

Then, I have a file called build.sh that deploys the action to OpenWhisk. It’s in a script because there are a few things to remember:

  • Since I’m including some npm modules that aren’t available by default on OpenWhisk, I create the action using a ZIP file that includes both my own JavaScript code in index.js and the contents of node_modules.
  • The script builds this for me every time.
  • When updating the action, I also supply the params.json file.

Here’s the build.sh file:

This step isn’t strictly necessary, as you could run the commands yourself, but I find it helpful to have it packaged so I do it the same way (correctly!) every time.

Using Redis as Alexa’s memory

Working with serverless JS is slightly different to using NodeJS in a more traditional runtime setting. As a result, I use the Bluebird library alongside Redis from npm to access Redis from OpenWhisk. There are some examples in the Redis module docs too, which I found helpful!

Here’s the code that helps Alexa remember what she just said:

Using the standard Redis SET command becomes client.setAsync() when working with Bluebird. Into this function, I pass the key codenames and the random value generated on line 7. Now that Alexa can remember the value, I can ask her to repeat herself! Here’s the code that does that:

Again, the asynchronous version turns the Redis GET command into client.getAsync(). It lets me fetch the key I’m interested in and then perform the next step once the data arrives.

Alexa, I need a conclusion!

Being able to easily integrate open source solutions into existing applications makes serverless platforms like OpenWhisk very approachable and quick for making genuinely useful tools. These qualities mean that, as developers, we know we’ll be able to push the software to go far beyond the many “Hello World” examples that exist.

I’ll leave the final words to Alexa, showing off her new memory skills:

Alexa, …

If you enjoyed this article, please ♡ it to recommend it to other Medium readers.

--

--

Lorna Mitchell
Center for Open Source Data and AI Technologies

Polyglot programmer, technology addict, open source fanatic and incurable blogger (see http://lornajane.net)