Create and Link Redis Database

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
applications. In particular, you can use Redis to:

  • Save application data that persists in memory across processes, containers, and deploys.
  • Enable queued and background processes with Celery.
    Redis and Celery Demo App
  • Use the Dash Enterprise Snapshot Engine; see the documentation on your
    Dash Enterprise server for details.
  • Cache data from your callbacks across processes.
    Caching in Dash with Redis

 

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 application.


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 application doesn’t
override the data from a separate application.

 

In Dash Enterprise, it is possible to create and link a Redis
Database to your Dash App using the Dash Enterprise UI.
Here, you have two options:

 

1. Create a database before initializing an app.

2. Create and link a database after an app has been initialized.

 

Create a Database Before Initializing an App

If you haven’t initialized an app yet, select Databases situated in the
top navigation menu. Next, select Create Database, then in the
‘Create Database’ modal, add the name of your database. We recommend using
a convention like using the name of your application and adding -redis
to the end, e.g. my-dash-app-redis.

Once your Redis Database has been created, you’ll notice that it is
added to your list of databases.

Create Database

 

Next, navigate to Apps and create a new app (for more info see
<dccLink>,
in the ‘Create App’ modal you have the option of linking a database.
Here, use the dropdown to select the database that you created previously
(see image below).

Link Database

 

In the Dash Enterprise UI, select the app then navigate
to the settings page. In Databases, use the dropdown to select
create and link database then Add.

Create and Link Database in App

While it is now possible to create and link Redis Databases via the
Dash Enterprise 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
application.
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.

 

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

 

Windows Users

Installing Redis from source on windows can be tricky. If you have the
“Windows Subsystem for Linux”, we recommend using that and installing
the Redis in that linux environment. Otherwise, we recommend installing
these 64-bit binary releases of Redis.