Use Redis with Binaris Functions

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

If you are new to Binaris it is 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 redis 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 python2 MyRedis

Install the ‘redis’ package

$ pip install redis -t .

Configure your Redis connection parameters

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

functions:
  MyRedis:
    file: function.py
    entrypoint: handler
    runtime: python2
+   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.py file, paste the code below:

import os
import redis

port = os.environ['REDIS_PORT']
host = os.environ['REDIS_HOST']
password = os.environ['REDIS_PWD']

r = redis.Redis(host=host, port=port, db=0, password=password)

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.

def handler(body, req):
-   name = req.query.get('name') or body.get('name') or 'World'
-   return 'Hello {}!'.format(name)
+   command = body.get('command') or 'set'
+   key = body.get('key') or 'myKey'
+   value = body.get('value') or 'Binaris'

Insert to Redis

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

def handler(body, req):
    command = body.get('command') or 'set'
    key = body.get('key') or 'myKey'
    value = body.get('value') or 'Binaris'
   
+   if command == 'set':
+     r.set(key, value)
+     return 'Inserted: {}'.format(value)

Read from Redis

This section reads the data stored in key.

def handler(body, req):
    command = body.get('command') or 'set'
    key = body.get('key') or 'myKey'
    value = body.get('value') or 'Binaris'
   
    if command == 'set':
      r.set(key, value)
      return 'Inserted: {}'.format(value)
+   elif command == 'get':
+     result = r.get(key)
+     return 'Found: {}'.format(result)
+   else:
+     return ''

Your function should now look like:

import os
import redis

port = os.environ['REDIS_PORT']
host = os.environ['REDIS_HOST']
password = os.environ['REDIS_PWD']

r = redis.Redis(host=host, port=port, password=password)

def handler(body, req):
    command = body.get('command') or 'set'
    key = body.get('key') or 'myKey'
    value = body.get('value') or 'Binaris'
   
    if command == 'set':
      r.set(key, value)
      return 'Inserted: {}'.format(value)
    elif command == 'get':
      result = r.get(key)
      return 'Found: {}'.format(result)
    else:
      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:$(bn show apiKey) https://run.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: