This page will walk you through building a function that uses Redis to maintain state.

If you are new to Binaris is it recommended that you look through the previous getting-started tutorials:

Note: the instructions below assume you are using MacOS, Ubuntu or Debian.

Serverless functions and state

One of the core values of Serverless functions is the fact that they are stateless. This means that a function invocation can’t depend on pieces of data collected or computed by other functions. To enable state, it is common to use reliable repositories such as Redis. In this tutorial you will walk through building a simple function that inserts and retrieves values from a Redis instance.

Note: this tutorial is not intended to teach you how to setup Redis. You can use cloud hosted Redis services or set it up yourself on a container or VM. You will need the Redis port, host URI and password to complete the tutorial.

1. Connect to Redis

You will use the ioredis package to interface with Redis. You will also need to collect a few configuration settings for your Redis instance.

Create the function

$ mkdir MyRedis
$ cd MyRedis
$ bn create node8 MyRedis

Install the ioredis npm package

$ npm install ioredis

Configure your Redis connection parameters

Declare the environment variables needed for Redis by editing the binaris.yml file as below:

functions:
  MyRedis:
    file: function.js
    entrypoint: handler
    runtime: node8
+   env:
+     REDIS_PORT: 
+     REDIS_HOST: 
+     REDIS_PWD: 

Set the values to the environment variables:

$ export REDIS_PORT=<Your_Redis_Port_Number>
$ export REDIS_HOST=<Your_Redis_Host_URL>
$ export REDIS_PWD=<Your_Redis_Password>

Add the Redis connection code

At the top of the function.js file, paste the code below:

const Redis = require('ioredis');

const {
  REDIS_PORT: port,
  REDIS_HOST: host,
  REDIS_PWD: password,
} = process.env;

const redis = new Redis({
  port,
  host,
  family: 4,
  password,
  db: 0,
});

2. Insert and fetch data

In this section you will reconstruct your function code to read and write to Redis.

Read processing instructions

This section reads the Redis key, data and method (select or insert) from the function body.

- exports.handler = async (body, context) => {
+ exports.handler = async (input) => {
+  const data = body.data || 'redis';
+  const defaults = {
+   command: 'set',
+   key: 'myKey',
+   value: 'Binaris',
+ };
+ const { command, key, value } = {...defaults, ...input};
- const name = context.request.query.name || body.name || 'World';
- return `Hello ${name}!`;

Insert to Redis

This section inserts the value field in the request body into key in the Redis database.

exports.handler = async (input) => {
  const defaults = {
    command: 'set',
    key: 'myKey',
    value: 'Binaris',
  };
+  const { command, key, value } = {...defaults, ...input};
+  switch (command) {
+    case 'set': {
+      await redis.set(key, value);
+      return `Inserted: ${value}`;
+    }

Read from Redis

This section reads the data stored in key.

exports.handler = async (input) => {
  const defaults = {
    command: 'set',
    key: 'myKey',
    value: 'Binaris',
  };
  const { command, key, value } = {...defaults, ...input};
  switch (command) {
    case 'set': {
      await redis.set(key, value);
      return `Inserted: ${value}`;
    }
+   case 'get': {
+     const result = await redis.get(key);
+     return `Found: ${result}`;
+   }
+   default: {
+     return '';
+    }
+  }
+ };

Your function should now look like:

const Redis = require('ioredis');

const {
  REDIS_PORT: port,
  REDIS_HOST: host,
  REDIS_PWD: password,
} = process.env;

const redis = new Redis({
  port,
  host,
  family: 4,
  password,
  db: 0,
});

exports.handler = async (input) => {
  const defaults = {
    command: 'set',
    key: 'myKey',
    value: 'Binaris',
  };
  const { command, key, value } = {...defaults, ...input};
  switch (command) {
    case 'set': {
      await redis.set(key, value);
      return `Inserted: ${value}`;
    }
    case 'get': {
      const result = await redis.get(key);
      return `Found: ${result}`;
    }
    default: {
      return '';
    }
  }
};

3. Deploy and Invoke the function

$ bn deploy MyRedis
Deployed function MyRedis
Invoke with one of:
  "bn invoke MyRedis"
  "curl -H X-Binaris-Api-Key:<Your_API_Key> https://run-sandbox.binaris.com/v2/run/<Your_Account_Number>/MyRedis"

Insert data into Redis

bn invoke MyRedis --data '{"command":"set","key":"MyKey","value":"MyData"}'
"Inserted: MyData"

Read data from Redis

bn invoke MyRedis --data '{"command":"get","key":"MyKey"}
"Found: MyData"

You now have a function deployed that uses Redis to store and read data!

4. Cleanup

$ bn remove MyRedis
Removed function MyRedis

What’s next

To learn more about Binaris functions, review the below documents: