Azure Redis

How to use Redis in an Azure Function

Quick explanation of how to use Redis and, specifically, the StackExchange.Redis package in an Azure Function.

af and redis


I’m going to assume you have a Redis cache. Make sure to have the connection string handy. We will need it shortly. It should look something like this,password==,ssl=True,abortConnect=False

Add a project.json

  • Select your function
  • Click View Files
  • Click Add
  • Type project.json

This is what you need to enter in your project.json. Note that the version may be different in the future (it was current when I wrote this).

Once done, make sure to click Save



Add the Connection String

You could put your connection string in code but you can store it centrally, as you normally do with an Azure Web App.

  • In the list of functions, click the topmost element
  • Click Platform features
  • Click Application settings
  • A blade will pop up on the right. Scroll down and, under Connection strings, enter
    • A name for your connection string i.e. mycats
    • Enter the connection string
    • Where it says SQL Database, select Custom
    • Press Save


Putting it Together

In your .csx file, you need to:

  • Add a using StackExchange.Redis statement
  • Access the connection string
  • Create a connection multiplexer using your connection string
  • The rest is up to you!

Here’s the start of some of some code. Doesn’t do anything; it’s just some sample code I grabbed but checkout Lines 2, 9 and 10, which I added.

If you have any questions on how to do something with StackExchange.Redis, checkout the docs.


  1. Hello Ben,
    I’m using Asp.Net websites with Redis cache. One of the main problem is the cost of instance ConnectionMultiplexer.
    With multiple request the connection pool was full in few minutes blocking all websites for timeout.
    To fix that you have to create a singleton in this way:

    private static readonly Lazy LazyConnection
    = new Lazy(() =>
    var connection = ConnectionMultiplexer.Connect(GetRedisConnectionString());
    // connection.PreserveAsyncOrder = false;
    return connection;

    How do you manage this iussue as azure function are serverless and destroy instance after request even if are singleton?


    1. Good question. What I have learned since creating this article is that that a connection like this should be initiated and stored as a static, to ensure it only gets created once and then reused. So I create a static class, put initialization info in the (static) constructor and then access the mux via a property or perhaps via static helper methods. Can you let me know how that goes?


      1. My last test creating static property for a service bus connection on azure function failed. At each request a new connection was created(i’m using a consumption plan. I guess that with fixed plan we won’t have the problem).
        That’s why you have to use bindings for service bus(unfortunately not avalable for redis).

        In January i’ll do more deeper test with redis and function with heavy load test and i let you know.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: