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 store that is well-suited for many Dash
apps. In particular, you can use Redis to:
While Redis is an in-memory database, as Dash Enterprise regularly
backs up its data to the server, it’s safe for use in production. Dash Enterprise can dynamically spin up and manage secure instances of Redis for your app.
This page describes how to manage Redis databases using the App Manager, but you can also use the Dash Enterprise CLI.
Redis instances are app-specific. Each app can have one Redis instance. To use Redis with an app, add it after you’ve created the app:
Once you add Redis to an app, you then access Redis in your app code with redis_instance = redis.StrictRedis.from_url(os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379'))
. You’ll also find this redis_instance
code snippet in the Dash Enterprise UI if you go to your app’s Redis database.
Importing the necessary libraries and accessing the Redis instance from the REDIS_URL
environment variable:
import os
import redis
redis_instance = redis.StrictRedis.from_url(
os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379')
)
In the App Catalog, the app Use Redis to Cache Datasets is a good example of using Redis in your code.
In that app code, a redis_instance
is created here:
redis_instance = redis.StrictRedis.from_url(
os.environ.get("REDIS_URL", "redis://127.0.0.1:6379")
)
Then the app saves data to that Redis instance with the .hset()
method:
redis_instance.hset("data", "iris", iris.to_json())
redis_instance.hset("data", "medals", medals.to_json())
And retrieves the data later with the .hget()
method:
df = pd.DataFrame(json.loads(redis_instance.hget("data", dataset_name)))
Dash Enterprise Workspaces share the same Redis instance as your deployed
app. This allows you to inspect your Redis instance’s data from that app’s workspace. However, it is important that you structure your code to not override Redis data in your deployed app.
Each Redis instance has 16 available databases. The different databases can be specified by appending the database number( /0
, /1
, /2
, …, /15
) to the end of the connection URL, e.g. redis://127.0.0.1:6379/12
.
dash-snapshots
implicitly uses an alternative Redis database on the
same Redis instance so that the app and the workspace write data to different places.
Many of the apps in the App Catalog demonstrate how to use an alternative Redis database from within the workspace environment.
Here is a function you can use to connect to a different Redis database on the same Redis instance in a workspace vs in an app. It takes the REDIS_URL
environment variable and increments the database URL by one:
import redis
import os
from urllib.parse import urlparse
def get_redis_url():
"""
Returns REDIS_URL if within the app environment.
If within the Workspace environment, returns REDIS_URL with the database incremented by one.
This allows you to use one Redis instances but connect to different databases to not override data.
"""
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")
return REDIS_URL
redis_instance = redis.StrictRedis.from_url(get_redis_url())
Redis in Workspaces
After adding Redis to an app, you’ll need to restart the workspaces to have access to that database.
To test your app locally, you’ll need to install and start Redis.
See the Redis documentation
for more information on installing and running Redis.
To delete an app’s Redis service:
Find and select the app in the App Manager.
Select the Services tab.
Select the Redis service. (The name ends in -redis).
Select Remove. You are prompted to enter the service name to confirm.
Dash Enterprise 5 uses Redis version 7 from Docker Hub and its default settings. When you add a Redis instance to your app, it’s created with 16 databases. Right now, it’s not possible to change these settings.
Each Redis instance has a maximum storage capacity of 25 GiB. Additionally, the number of services (Postgres or Redis) that you can have across Dash Enterprise is limited by the number of pods and volumes allowed per node by your Kubernetes cloud provider and the number of nodes in your cluster.
If you are an administrator with access to the Kubernetes cluster that Dash Enterprise is installed on, you can view logs for an app’s Redis service by connecting to the cluster.
Managed database services are located in the dash-services
namespace. To view logs for an app’s Redis service, make sure your kubectl
context is set to the right cluster, and then run the following command:
kubectl logs -f -n dash-services -l app=redis-<app-name>
where <app-name>
is the name of the app whose Redis logs you want to view.
In the App Catalog, you’ll find many apps demonstrating Dash Enterprise’s built-in Redis: