Adding Redis to an App

This documentation is for Dash Enterprise.
Dash Enterprise is the fastest way to write & deploy Dash apps and
Jupyter notebooks.
10% of the Fortune 500 uses Dash Enterprise to productionize AI and
data science apps. Find out if your company is using Dash Enterprise.

Redis is a powerful in-memory database that is well-suited for many Dash apps. In particular, you can use Redis to:

While Redis is an in-memory database, Dash Enterprise regularly
backs up its data to the underlying server. So, it’s safe for production
usage. Dash Enterprise can dynamically spin up and manage secure
instances of Redis for your app.

Creating and Linking Databases via the Dash Enterprise App Manager

You can create one Redis instance that is used by multiple apps or you
can create a unique Redis database for each individual app.
To start, we recommend creating a unique Redis Database for each
Dash app. It will be easier for you to ensure that one app doesn’t
override the data from a separate app.

You can create and link a Redis
database to your Dash app using the Dash Enterprise App Manager.
Here, you have two options:

Creating a Database Before Initializing an App

To create a database and link it at app initialization time:

  1. Go to Databases.
  2. Select Create Database.
  3. In the modal that appears, enter a name for your database (we recommend using
    a convention like appending -redis to what will be your app name).
  4. In Database Type, select Redis.
  5. Select Create.

Create Redis Database

Your new database is added to your list of databases.

<img>

  1. Go to Apps and initialize your app (for more info, see
    Initialize Dash Apps on Dash Enterprise),
    selecting your database in Link a Database.

<img>

Creating and Linking a Database After an App Has Been Initialized

To create and link a database for an existing app:

  1. In your app’s Settings, go to Databases.
  2. Select Create and link database.

<img>

  1. Enter a name for your database (we recommend using a convention like appending -redis to your app name).
  2. In Type, select Redis.
  3. Select Create and link.

<img>

Creating and Linking Databases Programmatically

While it is now possible to create and link Redis databases via the
Dash Enterprise App Manager UI, it is still possible to create and link a Redis
database via the Dash Enterprise GraphQL API.

Referencing Redis in Your Code

You can reference your Redis database with the os.environ module:

redis_instance = redis.StrictRedis.from_url(os.environ["REDIS_URL"])

Using Redis inside Workspaces

Dash Enterprise Workspaces share the same Redis instance as the deployed
app.
This enables you to inspect data of your Redis instances from within
workspaces. However, it is important that you structure your code to
not override Redis data.

Each Redis instance has 16 available databases.

dash-snapshots implicitly uses an alternative Redis database on the
same instance.

The Sample Applications & Templates demonstrate how to use an alternative
Redis database if within the Workspace environment:

if os.environ.get("DASH_ENTERPRISE_ENV") == "WORKSPACE":
    next_database_number = str((int(os.environ.get("REDIS_URL")[-1]) + 1) % 16)
    REDIS_URL = os.environ["REDIS_URL"][:-1] + next_database_number
    parsed_url = urlparse(os.environ.get("REDIS_URL"))
    if parsed_url.path == "" or parsed_url.path == "/":
        i = 0
    else:
        try:
            i = int(parsed_url.path[1:])
        except:
            raise Exception("Redis database should be a number")
    parsed_url = parsed_url._replace(path="/{}".format((i + 1) % 16))

    updated_url = parsed_url.geturl()
    REDIS_URL = "redis://%s" % (updated_url.split("://")[1])
else:
    REDIS_URL = os.environ.get("REDIS_URL", "redis://127.0.0.1:6379")

Running Redis on Your Local Machine

To get started, see the Redis documentation
to download Redis and set up a local instance.

Note for Windows users: Installing Redis from source on Windows can be tricky. If you have
Windows Subsystem for Linux, we recommend using it and installing
Redis in that Linux environment. Otherwise, we recommend installing
these 64-bit binary releases of Redis.

By referencing Redis in our code, we’ll need to add the variable to our
local environment as well. One easy way to do this is to define the
variable on-the-fly when you run python app.py.

$ REDIS_URL=redis://&lt;your-redis-instance-ip&gt;:6379 python app.py