Skip to content

Merging using shuffle = 'disk' is 25x slower than when using shuffle='tasks' #5554

Open

Description

@bsesar

What happened: When using shuffle = 'disk' merging took 50 minutes compared to 2 minutes when using shuffle = 'tasks'. Also, Dask dashboard was showing very low CPU utilization when using shuffle = 'disk'.

What you expected to happen: I expected merging with shuffle = 'disk' to be faster than merging with shuffle = 'tasks' since the load on the scheduler is supposed to be lower in that case.

Minimal Complete Verifiable Example:

from dask.distributed import Client
import dask.dataframe as dd
import pandas as pd
import numpy as np
import os
from time import time
import pandas.util.testing
import dask.config

# folder in which data will be saved
data_folder = 'path_to_folder_for_saving_data'

# enable shuffle compression
# NOTE: you need to install python-snappy package in order to use Snappy compression!!
dask.config.set({
  "dataframe.shuffle-compression": 'Snappy',
  'temporary_directory': data_folder
  })

# start a local Dask cluster
# (note: I used a single machine with 14 cores and started the cluster with n_workers=14 and threads_per_worker=1)
client = Client(local_directory=data_folder )

# create the folder for fake data for the right side of the merge
os.mkdir('%s/right_ddf' % data_folder)

# number of partitions for the right side of the merge
N_partitions_right = 40

# number of rows per partition
Nrows_right = 1038790

# create data for the right side of the merge
for partition_id in range(N_partitions_right):
  df = pd.DataFrame({'ID':pandas.util.testing.rands_array(16, Nrows_right, dtype='O'),
                     'Date':pd.Timestamp('2019-12-01')})
  df.to_parquet('%s/right_ddf/%d.parquet' % (data_folder, partition_id))

# create the folder for fake data for the left side of the merge
os.mkdir('%s/left_ddf' % data_folder)

# number of partitions for the left side of the merge
N_partitions_left = 1325

# number of rows per partition
Nrows_left = 99063

# to create the left side of the merge, sample Nrows_left from a randomly
# selected partion on the right side
partitions_to_sample = np.random.randint(0, high=(N_partitions_right-1), size=N_partitions_left)
for partition_id, partition_to_load in enumerate(partitions_to_sample):
  df = pd.read_parquet('%s/right_ddf/%d.parquet' % (data_folder, partition_to_load))
  df = df.sample(Nrows_left, replace=True)
  df.to_parquet('%s/left_ddf/%d.parquet' % (data_folder, partition_id))

# benchmark merging using tasks and disk shuffle
for shuffle_type in ['tasks', 'disk']:  
  
  # load data
  left_ddf = dd.read_parquet('%s/left_ddf' % data_folder)
  right_ddf = dd.read_parquet('%s/right_ddf' % data_folder)

  # merge
  left_ddf = left_ddf.merge(right_ddf, on='ID', how='inner', shuffle=shuffle_type)
  
  t0 = time()
  left_ddf = left_ddf.persist().head()
  elapsed_time = (time() - t0)/60.
  print('Merging using %s takes %.1f minutes' % (shuffle_type, elapsed_time))

EDIT: If the above MVE is too much for your machine, use Nrows_right = 1038, Nrows_left = 99, and N_partitions_left = 600. Merging with shuffle='tasks' then takes 0.4 min, and merging with shuffle='disk' takes 2.4 minutes, or a factor of 6 difference (with n_workers=14 and threads_per_worker=1).

Environment:

  • Dask version: 2021.11.2
  • Python version: 3.8.12
  • Operating System: Windows Server 2016 64 bits
  • Install method (conda, pip, source): conda

Activity

  1. fjetter commented on Dec 2, 2021

    @fjetter
    Member

    Thanks for this example. I'm currently trying to reproduce but a few notes up front

    1. I realized your example is extremely asymmetric. Right dataframe has few and large files while the left dataframe has many partitions with few rows. This kind of asymmetry typically causes less than optimal performance since dask cannot fix this distribution itself. I would strongly recommend for you to repartition the left DF before shuffling, e.g. left_ddf = left_ddf.repartition(npartitions=40) (40 is kind of random, you might even be better of with a smaller value since htere are fewer rows left)
    2. The minimal example you provided is already relatively large. I'm executing it on a small-ish notebook but even the task based shuffle is just swapping and I am disk bound. Do you see the same problem if you scale the problem down?
  2. bsesar commented on Dec 3, 2021

    @bsesar
    Author

    Thank you for the comments, @fjetter. My answers are below:

    1. The example is extremely asymmetric because my real data are asymmetric :-) My real left dataframe has 1300 partitions of 100 MB in size and the right dataframe has 40 partitions of 100 MB in size. Yes, I could repartition data, but then I am running the risk of running out of memory (per worker). Besides, even though the dataframes are asymmetric, merging with shuffle='tasks' does not seem to have a problem with them (i.e., merging takes about 2 minutes in that case). That being said, reducing the number of partitions in the left dataframe by half does reduce the merging time in half. So it seems that shuffle='disk' is quite sensitive to the number of dataframes, while shuffle='tasks' is not as sensitive (or is just too fast to not make any notable difference).
    2. Yes, I see the same problem if I set Nrows_right = 1038, Nrows_left = 99, and N_partitions_left = 600 in the MVE code. Merging with shuffle='tasks' takes 0.4 min, and merging with shuffle='disk' takes 2.4 minutes, or a factor of 6 difference.

    In the end, my point is that on the same machine with the same data and disk, merging with shuffle='tasks' is much faster than merging with shuffle='disk'.

  3. fjetter commented on Mar 17, 2025

    @fjetter
    Member

    This is already an old issue but the issue still persists. The below configuration is running the shuffle operations on a VM that should be able to fit the dataset twice

    import coiled
    
    @coiled.function()
    def run_benchmark():
        import dask
        from dask.datasets import timeseries
        methods = [
            "tasks",
            "disk",
        ]
        result = {}
        import time
        for meth in methods:
            print(f"Running {meth}")
            # About 5-6GiB in memory
            with dask.config.set({"dataframe.shuffle.method": meth}):
                start = time.perf_counter()
                (
                    timeseries(end='2004-01-01')
                    .shuffle("x")
                    .map_partitions(lambda x: len(x))
                    .compute(scheduler="threads")
                )
                result[meth] = time.perf_counter() - start
        return result
        
    run_benchmark()

    this is a 4 vCPU VM and we can see that the initial burst uses all CPUs which is the tasks based shuffle. The endless tail is the disk

    Image

    Allowing this to finish gives something like

    {'tasks': 94.88317428799999, 'disk': 2241.7395113889997}

    i.e. I can reproduce a ~24x slowdown of disk compared to tasks with a very simple example

  4. fjetter commented on Mar 17, 2025

    @fjetter
    Member

    From the metrics it also looks like it is only able to use a single CPU when doing the disk based shuffle.

    some brief profiling points to https://github.com/fjetter/dask/blob/33412e9f1dfe7d0b3408d495fc0a00dbce60bdcf/dask/dataframe/dask_expr/_shuffle.py#L506 which eats >60% of this runtime and primarily points to pyarrow.compute, probably due to the pyarrow string dtype. I don't know what the tasks based approach is doing that makes this so much faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions