Serverless + Redis
Functions, the perfect companion to any cache.
When you have a serverless function, but need to stash the results somewhere for a quick moment, where do you turn?
Redis offers flexible levels of persistence and speedy performance. Serverless functions offer scalability without the infrastructure overhead. Together, they complement each other well.
Getting started with serverless
Platforms like Apache OpenWhisk let you write code in the language of your choice. For example, the documentation provides a “Hello World” function in JavaScript:
After installing wsk, the OpenWhisk CLI, you simply need to define your function as an action:
Invoke that action like so:
Using Redis from a serverless function
In my Alexa Project Codename example application, stashing data from a serverless function isn’t that much more involved. First I bundle up my required Node.js modules and my Redis parameters:
Then, I can use the node_redis client’s SET command, with node_redis for added asynchronous operation (as in client.setAsync(...)
):
There’s a lot more you can do, so be sure to check these projects for more ideas.
Projects
-
Add Redis To Your Serverless Application
Medium | YouTubeUse an in-memory datastore for a little light persistence.
-
Going Serverless with IBM Cloud Functions and Compose Redis
Compose Articles | GitHub | YouTubeWith OpenWhisk, database connections are simply packages of your favorite language-API combo.
-
Custom Indexers for Cloudant
GitHub | MediumUsing JavaScript and Redis for problems that don’t fit Cloudant’s indexing engines.