Working on large datasets -- comparison with shiny


First of all, like everyone on this forum I generally love Dash. I was excited to see the beta a year ago and I’m glad it’s coming of age. Secondly, this is yet another post on a topic that’s been discussed pretty extensively.

There is a particular type of workflow that we use all the time, which is pretty easy to do in Shiny and (as far as I can tell) virtually impossible difficult to do in Dash/Flask, because of the inability to store server-side variables. Basically our apps let the user load some large array of data (for this purpose it doesn’t matter if it comes from an uploaded file or from a database qurey), do some operations on it, display some results, and perhaps let the user download some results. In shiny this is pretty straighforward, as session variables can be shared across all the callbacks.

There are many threads about how to store pandas dataframes in memory, and answers like this Sharing a dataframe between plots work quite nicely for small dataframes. However for large dataframes (millions of elements), it seems like I’m going to be spending the entire time just reading and writing to the json.

The second standard solution seems to be to not store the data in memory, but rather to fetch the data from the database every time it’s needed (and put it in a database if it was uploaded by the user), and combine this with caching. This would be great if I didn’t need to manipulate the data at all. But if I want to modify a row or column (but I need the entire dataset for computation), then I’m going to be doing a lot of fetching. Moreover, I think with most caching methods, I still need to serialize/deserialize.

Working in a standalone script or in a notebook, I’ve found that pandas/numpy have some major advantages over any R data structure in terms of performance. I was really hoping to take advantage of this + dash to build some of our next-gen apps. But at the moment it seems difficult to take advantage of it. Does anyone have some ideas for alternatives I haven’t considered?


Some background: In order for this to work across multiple python processes, we need to store the data somewhere that is accessible to each of the processes. There are 3 places to store this data:
1 - On the disk (e.g. on a file or on a new database)
2 - In a shared memory space like with Redis
3 - In the user’s browser session

For 1 and 2:

  • Easiest to implement with tools like Flask-Caching, see
  • Data has to be serialized out of python data structures into simpler datastructures like strings and numbers (or just JSON) for storage
  • Data that is cached in this way will be available for every future session.
    • If you open up the app in a new browser window (or if a different viewer opens up the app), the app’s callbacks may retrieve the data from the cache instead of computing it fresh.
  • Since data is stored for all sessions, you could run into memory issues (if using e.g. Redis and not the file system) if you’re storing the output for every single set of parameters of your cache.
    • As such, you need to balanace what you cache and what you compute. If querying the raw data is slow (e.g. from SQL), then you could cache the results of the query once and then perform fast computations (e.g. pandas aggregations) on top of that.

For 3:

  • Implemented by saving the data as part of Dash’s front-end store through methods explained in Sharing a dataframe between plots
  • Data has to be converted to a string like JSON for storage and transport
  • Data that is cached in this way will only be available in the user’s current session.
    • If you open up a new browser, the app’s callbacks will always compute the data. The data is only cached and transported between callbacks within the session.
    • As such, online 1 and 2, this method doesn’t increase the memory footprint of the app.
    • There could be a cost in network transport. If your sharing 10MB of data between callbacks, then that data will be transported over the network between each callback.
    • If the network cost is too high, then compute the aggregations upfront and transport those. Your app likely won’t be displaying 10MB of data, it will just be displaying a subset or an aggregation of it.

Reading and writing JSON isn’t that expensive but sending it over the network could be. To get around this, the last point in the outline above is an option:

If the network cost is too high, then compute the aggregations upfront and transport those. Your app likely won’t be displaying 10MB of data, it will just be displaying a subset or an aggregation of it.

For example:

@app.callback(Output('intermediate-value', 'children'), [Input('dropdown', 'value')])
def clean_data(value):
     # an expensive query step
     cleaned_df = your_expensive_clean_or_compute_step(value)
     # a few filter steps that compute the data
     # as it's needed in the future callbacks
     df_1 = cleaned_df[cleaned_df == 'apples']
     df_2 = cleaned_df[cleaned_df == 'oranges']
     df_3 = cleaned_df[cleaned_df == 'figs']
     return {
         df_1: df_1.to_json(orient='split'),
         df_2: df_2.to_json(orient='split'),
         df_3: df_3.to_json(orient='split'),

    Output('graph', 'figure'),
    [Input('intermediate-value', 'children'])
def update_graph_1(jsonified_cleaned_data):
    dff = pd.read_json(jsonified_cleaned_data['df_1'])
    figure = create_figure_1(dff) 
    return figure

    Output('graph', 'figure'),
    [Input('intermediate-value', 'children'])
def update_graph_2(jsonified_cleaned_data):
    dff = pd.read_json(jsonified_cleaned_data['df_2'])
    figure = create_figure_2(dff) 
    return figure

    Output('graph', 'figure'),
    [Input('intermediate-value', 'children'])
def update_graph_3(jsonified_cleaned_data):
    dff = pd.read_json(jsonified_cleaned_data['df_3'])
    figure = create_figure_3(dff) 
    return figure

Your mileage will vary depending on your aggregations and your UI. You could end up reducing a 5M row dataframe into 3 bar graphs each with 100 points each, in which case the transport costs will be really low.


@chriddyp thanks a lot for the detailed reply. I understand that the type of approach I have with shiny is incompatible with the paralellization in dash.

In my case it’s not network issues I’m running into (I’m just running locally for testing), but really the cost of reading the json, at least when it’s read all in one big chunk. What you’ve outlined in the last section – splitting up the data into multiple smaller pieces – is a great idea and is the direction I started going in. That approach could take major advantage of the paralellization, but what does become difficult is to interact with the smaller pieces. To put another way, the last example would be pretty tricky to write if the user needed to specify [apples,oranges,figs] and the list could be long. We discussed this briefly here also: Capture click on subplot. I guess if you fix the number of elements it becomes pretty doable (as per More sliders! (help with button callback example)).

Best Way to Handle User-Uploaded Dataset

I thought about this a little bit more today and came up with another alternative solution. This solution:

  • Uses Redis via Flask-Cache for storing “global variables”. This data is accessed through a function who’s output is cached and keyed by its input arguments.
  • Uses the hidden div solution to send a signal to the other callbacks when the expensive computation is complete
  • Note that instead of Redis, you could also save this to the file system. See for more details.

This “signaling” is cool because it allows the expensive computation to only take up one process. Without this type of signaling, each callback could end up computing the expensive computation in parallel, locking 4 processes instead of 1.

This approach also has the advantage that future sessions use the pre-computed value. This will work well for apps that have a small number of inputs.

Here’s what this example looks like. Some things to note:

  • I’ve simulated an expensive process by using a time.sleep(5).
  • When the app loads, it takes 5 seconds to render all 4 graphs
  • The initial computation only blocks 1 process
  • Once the computation is complete, the signal is sent and 4 callbacks are executed in parallel to render the graphs. Each of these callbacks retrieves the data from the “global store”: the redis cache.
  • I’ve set processes=6 in app.run_server so that multiple callbacks can be executed in parallel. In production, this is done with something like $ gunicorn --workers 6 --threads 2 app:server
  • Selecting a value in the dropdown will take less than 5 seconds if it has already been selected in the past. This is because the value is being pulled from the cache.
  • Similarly, reloading the page or opening the app in a new window is also fast because the initial state and the initial expensive computation has already been computed.

Here’s a GIF of this app (too big to show inline):

import copy
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
import datetime
from flask_caching import Cache
import numpy as np
import os
import pandas as pd
import time

app = dash.Dash(__name__)
    # try 'filesystem' if you don't want to setup redis
    'CACHE_TYPE': 'redis',
    'CACHE_REDIS_URL': os.environ.get('REDIS_URL', 'localhost:6379')
cache = Cache()
cache.init_app(app.server, config=CACHE_CONFIG)

N = 100

df = pd.DataFrame({
    'category': (
        (['apples'] * 5 * N) +
        (['oranges'] * 10 * N) +
        (['figs'] * 20 * N) +
        (['pineapples'] * 15 * N)
df['x'] = np.random.randn(len(df['category']))
df['y'] = np.random.randn(len(df['category']))

app.layout = html.Div([
        options=[{'label': i, 'value': i} for i in df['category'].unique()],
        html.Div(dcc.Graph(id='graph-1'), className="six columns"),
        html.Div(dcc.Graph(id='graph-2'), className="six columns"),
    ], className="row"),
        html.Div(dcc.Graph(id='graph-3'), className="six columns"),
        html.Div(dcc.Graph(id='graph-4'), className="six columns"),
    ], className="row"),

    # hidden signal value
    html.Div(id='signal', style={'display': 'none'})

# perform expensive computations in this "global store"
# these computations are cached in a globally available
# redis memory store which is available across processes
# and for all time.
def global_store(value):
    # simulate expensive query
    print('Computing value with {}'.format(value))
    return df[df['category'] == value]

def generate_figure(value, figure):
    fig = copy.deepcopy(figure)
    filtered_dataframe = global_store(value)
    fig['data'][0]['x'] = filtered_dataframe['x']
    fig['data'][0]['y'] = filtered_dataframe['y']
    fig['layout'] = {'margin': {'l': 20, 'r': 10, 'b': 20, 't': 10}}
    return fig

@app.callback(Output('signal', 'children'), [Input('dropdown', 'value')])
def compute_value(value):
    # compute value and send a signal when done
    return value

@app.callback(Output('graph-1', 'figure'), [Input('signal', 'children')])
def update_graph_1(value):
    # generate_figure gets data from `global_store`.
    # the data in `global_store` has already been computed
    # by the `compute_value` callback and the result is stored
    # in the global redis cached
    return generate_figure(value, {
        'data': [{
            'type': 'scatter',
            'mode': 'markers',
            'marker': {
                'opacity': 0.5,
                'size': 14,
                'line': {'border': 'thin darkgrey solid'}

@app.callback(Output('graph-2', 'figure'), [Input('signal', 'children')])
def update_graph_2(value):
    return generate_figure(value, {
        'data': [{
            'type': 'scatter',
            'mode': 'lines',
            'line': {'shape': 'spline', 'width': 0.5},

@app.callback(Output('graph-3', 'figure'), [Input('signal', 'children')])
def update_graph_3(value):
    return generate_figure(value, {
        'data': [{
            'type': 'histogram2d',

@app.callback(Output('graph-4', 'figure'), [Input('signal', 'children')])
def update_graph_4(value):
    return generate_figure(value, {
        'data': [{
            'type': 'histogram2dcontour',

# Dash CSS
    "external_url": ""})
# Loading screen CSS
    "external_url": ""})

if __name__ == '__main__':
    app.run_server(debug=True, processes=6)


@chriddyp thanks, looks interesting – just wanted to say it might be a couple of days before I have a chance to play with this fully.

Am I right to assume that there is a hidden serialization taking place when Redis stores the DataFrame result? If not, how does it work?


Yeah, from their source code, it looks like they pickle it: It may be faster for you to perform the string serialization yourself (e.g. to JSON or CSV). Some benchmarks have shown that JSON serialization is faster than Pickling ( but my guess is that it depends on the type and size of the object.

This is surprising to me. I wonder if some methods of pandas dataframe serialization are faster than others. Alternatively, converging a CSV and back might make this faster too.



I’ve pulled some of this discussion into a new chapter of the Dash user guide: (source here: Thanks for kicking off this discussion! :slight_smile:


New guide looks great.

Wrt to the best way to serialize the pandas dataframe, I’ve found that a binary encoding really gives an order of magnitude performance boost if dealing with large tables of numeric values.

I have been using something like this:

import io
import base64
import pandas as pd

def enc(df):
    return base64.b64encode(

def dec(s):
    return pd.read_msgpack(io.BytesIO(base64.b64decode(s)))


That’s good to know! I wonder if it would be possible to use Feather with redis. Feather provides binary DataFrame storage (using Apache Arrow) that’s compatible with both Python and R. For saving DataFrames to disk, I’ve switched to using it, as it gives a noticeable speedup over saving as CSV.