Repository navigation
Description
$ celery -A app report
software -> celery:4.0.0 (latentcall) kombu:4.0.0 py:3.5.2
billiard:3.5.0.2 redis:2.10.5
platform -> system:Linux arch:64bit, ELF imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:redis results:redis://localhost:6379/11
broker_url: 'redis://localhost:6379/10'
result_backend: 'redis://localhost:6379/11'
Happens on master (b8fa241) and 4.0.0.
Steps to reproduce
app.py (those chained groups could as well be chords):
import gc
from timeit import timeit
from celery import Celery, chain, group
from redis import StrictRedis
BROKER = 'redis://localhost:6379/10'
BACKEND = 'redis://localhost:6379/11'
app = Celery('abc', broker=BROKER, backend=BACKEND)
broker_redis = StrictRedis.from_url(BROKER)
result_redis = StrictRedis.from_url(BACKEND)
broker_redis.flushdb()
result_redis.flushdb()
@app.task
def a_task(*a, **kw):
print('invoking', a, kw)
def make_group(base_name):
"""Return a group of 2 similarly named tasks."""
return group([a_task.si('%s-A' % base_name), a_task.si('%s-B' % base_name)])
def build_canvas(depth):
callback = a_task.si(depth)
if depth == 0:
return callback
else:
return chain(
make_group(depth),
build_canvas(depth=depth - 1)
)
def build_canvas_unrolled():
"""
In case you suspect the recursive function above is botched.
Uncomment the lines below and invoke the task to observe the time increasing.
"""
from itertools import count
c = count()
return chain(
#make_group(next(c)),
#chain(
# make_group(next(c)),
# chain(
make_group(next(c)),
chain(
make_group(next(c)),
chain(
make_group(next(c)),
chain(
make_group(next(c)),
chain(
make_group(next(c)),
chain(
make_group(next(c)),
chain(
make_group(next(c)),
chain(
make_group(next(c)),
a_task.si('fin')
)
)
)
)
)
)
)
# )
#)
)
def probe(depth):
canvas = build_canvas(depth)
print('=' * 10)
print('Canvas depth:', depth)
print('Canvas:', canvas)
print('Took:', timeit('canvas.delay()',
globals={'canvas': canvas},
number=1))
print('Broker redis size:', broker_redis.info()['used_memory_human'],
'Peak:', broker_redis.info()['used_memory_human'])
print('Result redis size:', result_redis.info()['used_memory_human'],
'Peak:', result_redis.info()['used_memory_human'])
# Who knows...
del canvas
gc.collect()
broker_redis.flushdb()
result_redis.flushdb()
if __name__ == '__main__':
print('Warm up in case of lazy imports')
probe(3)
print('Warm-up finished')
print('============================= Actual measurements start here.')
for depth in range(1, 11):
probe(depth)Run: $ python app.py
Expected behavior
Chords are scheduled in reasonable time - probably linear.
Actual behavior
Chords schedule in exponential time:
Measurements
Lines "bend" when depth=8 because I made the measurements with depth<8 repeat trice, but failed to clean the redis between them. It's not super important, just don't assume that artifact is relevant. The code above is fixed.
Chart of Redis memory (both DBs are actually the same Redis): http://www.wolframalpha.com/input/?i=722880+738380+792820+1050000+2120000+6370000+21650000+108670000+144670000+552680000
$ python app.py # Run without active workers.
Warm up in case of lazy imports
==========
Canvas depth: 3
Canvas: %celery.chord([abc.a_task('3-A'), abc.a_task('3-B')], header=[abc.a_task('2-A'), abc.a_task('2-B')], body=%abc.a_task([a_task('1-A'), a_task('1-B')], 0), kwargs={})
Took: 0.05945688800420612
Broker redis size: 823.21K Peak: 823.21K
Result redis size: 715.26K Peak: 715.26K
Warm-up finished
============================= Actual measurements start here.
==========
Canvas depth: 1
Canvas: %abc.a_task([a_task('1-A'), a_task('1-B')], 0)
Took: 0.009336177026852965
Broker redis size: 722.88K Peak: 722.88K
Result redis size: 722.88K Peak: 722.88K
==========
Canvas depth: 2
Canvas: %celery.chord([abc.a_task('2-A'), abc.a_task('2-B')], header=[abc.a_task('1-A'), abc.a_task('1-B')], body=abc.a_task(0), kwargs={})
Took: 0.012298419023863971
Broker redis size: 738.38K Peak: 738.38K
Result redis size: 738.38K Peak: 738.38K
==========
Canvas depth: 3
Canvas: %celery.chord([abc.a_task('3-A'), abc.a_task('3-B')], header=[abc.a_task('2-A'), abc.a_task('2-B')], body=%abc.a_task([a_task('1-A'), a_task('1-B')], 0), kwargs={})
Took: 0.013047932996414602
Broker redis size: 756.84K Peak: 756.84K
Result redis size: 792.82K Peak: 792.82K
==========
Canvas depth: 4
Canvas: %celery.chord([abc.a_task('4-A'), abc.a_task('4-B')], header=[abc.a_task('3-A'), abc.a_task('3-B')], body=%chord([abc.a_task('2-A'), abc.a_task('2-B')], header=[abc.a_task('1-A'), abc.a_task('1-B')], body=abc.a_task(0), kwargs={}), kwargs={})
Took: 0.021236155065707862
Broker redis size: 1.05M Peak: 1.05M
Result redis size: 1.05M Peak: 1.05M
==========
Canvas depth: 5
Canvas: %celery.chord([abc.a_task('5-A'), abc.a_task('5-B')], header=[abc.a_task('4-A'), abc.a_task('4-B')], body=%chord([abc.a_task('3-A'), abc.a_task('3-B')], header=[abc.a_task('2-A'), abc.a_task('2-B')], body=%abc.a_task([a_task('1-A'), a_task('1-B')], 0), kwargs={}), kwargs={})
Took: 0.055250592064112425
Broker redis size: 2.08M Peak: 2.08M
Result redis size: 2.12M Peak: 2.12M
==========
Canvas depth: 6
Canvas: %celery.chord([abc.a_task('6-A'), abc.a_task('6-B')], header=[abc.a_task('5-A'), abc.a_task('5-B')], body=%chord([abc.a_task('4-A'), abc.a_task('4-B')], header=[abc.a_task('3-A'), abc.a_task('3-B')], body=%chord([abc.a_task('2-A'), abc.a_task('2-B')], header=[abc.a_task('1-A'), abc.a_task('1-B')], body=abc.a_task(0), kwargs={}), kwargs={}), kwargs={})
Took: 0.19522034004330635
Broker redis size: 6.33M Peak: 6.33M
Result redis size: 6.37M Peak: 6.37M
==========
Canvas depth: 7
Canvas: %celery.chord([abc.a_task('7-A'), abc.a_task('7-B')], header=[abc.a_task('6-A'), abc.a_task('6-B')], body=%chord([abc.a_task('5-A'), abc.a_task('5-B')], header=[abc.a_task('4-A'), abc.a_task('4-B')], body=%chord([abc.a_task('3-A'), abc.a_task('3-B')], header=[abc.a_task('2-A'), abc.a_task('2-B')], body=%abc.a_task([a_task('1-A'), a_task('1-B')], 0), kwargs={}), kwargs={}), kwargs={})
Took: 0.7236925609176978
Broker redis size: 21.61M Peak: 21.61M
Result redis size: 21.65M Peak: 21.65M
==========
Canvas depth: 8
Canvas: %celery.chord([abc.a_task('8-A'), abc.a_task('8-B')], header=[abc.a_task('7-A'), abc.a_task('7-B')], body=%chord([abc.a_task('6-A'), abc.a_task('6-B')], header=[abc.a_task('5-A'), abc.a_task('5-B')], body=%chord([abc.a_task('4-A'), abc.a_task('4-B')], header=[abc.a_task('3-A'), abc.a_task('3-B')], body=%chord([abc.a_task('2-A'), abc.a_task('2-B')], header=[abc.a_task('1-A'), abc.a_task('1-B')], body=abc.a_task(0), kwargs={}), kwargs={}), kwargs={}), kwargs={})
Took: 2.9150424430845305
Broker redis size: 108.63M Peak: 108.63M
Result redis size: 108.67M Peak: 108.67M
==========
Canvas depth: 9
Canvas: %celery.chord([abc.a_task('9-A'), abc.a_task('9-B')], header=[abc.a_task('8-A'), abc.a_task('8-B')], body=%chord([abc.a_task('7-A'), abc.a_task('7-B')], header=[abc.a_task('6-A'), abc.a_task('6-B')], body=%chord([abc.a_task('5-A'), abc.a_task('5-B')], header=[abc.a_task('4-A'), abc.a_task('4-B')], body=%chord([abc.a_task('3-A'), abc.a_task('3-B')], header=[abc.a_task('2-A'), abc.a_task('2-B')], body=%abc.a_task([a_task('1-A'), a_task('1-B')], 0), kwargs={}), kwargs={}), kwargs={}), kwargs={})
Took: 4.2594310390995815
Broker redis size: 144.64M Peak: 144.64M
Result redis size: 144.67M Peak: 144.67M
==========
Canvas depth: 10
Canvas: %celery.chord([abc.a_task('10-A'), abc.a_task('10-B')], header=[abc.a_task('9-A'), abc.a_task('9-B')], body=%chord([abc.a_task('8-A'), abc.a_task('8-B')], header=[abc.a_task('7-A'), abc.a_task('7-B')], body=%chord([abc.a_task('6-A'), abc.a_task('6-B')], header=[abc.a_task('5-A'), abc.a_task('5-B')], body=%chord([abc.a_task('4-A'), abc.a_task('4-B')], header=[abc.a_task('3-A'), abc.a_task('3-B')], body=%chord([abc.a_task('2-A'), abc.a_task('2-B')], header=[abc.a_task('1-A'), abc.a_task('1-B')], body=abc.a_task(0), kwargs={}), kwargs={}), kwargs={}), kwargs={}), kwargs={})
Took: 20.646082949009724
Broker redis size: 552.64M Peak: 552.64M
Result redis size: 552.68M Peak: 552.68M
The b'celery\x06\x169' list items in the broker redis seem to be heavily duplicated.
The list after executing a canvas with depth=4 (deeply deserialised by hand and pretty-printed as json): https://gist.github.com/not-raspberry/ee60565b584d0342c96345d744baaa3d
Activity
[-]Nested chords execute in exponential time[/-][+]Nested chords take exponential time (both in caller and in worker)[/+]not-raspberry commentedon Nov 9, 2016
As a side note, if you enable a worker (with
-l DEBUG), you can observe how it struggles with the first elements, until only 8 levels are remaining. Then it runs normally.auvipy commentedon Dec 19, 2017
could you verify this still happens on master?
not-raspberry commentedon Dec 20, 2017
not-raspberry commentedon Dec 20, 2017
Still occurs.
auvipy commentedon Jan 15, 2018
Lets see if it gone away after 4.2 release by this month.
auvipy commentedon May 28, 2018
could you try celery 4.2 rc4?
not-raspberry commentedon May 28, 2018
The full code sample is included in the first comment. You can try it yourself or even include it as a performance test.
24 remaining items
dan-licht commentedon Jan 7, 2025
any update on this? I'm looking into implementing nested chords, but have some hesitancy based on this issue
auvipy commentedon May 14, 2025
you can try to reproduce this with latest celery release, and see how that goes?
chrisbarber commentedon Jun 4, 2025
I observe the same issue on 5.5.3
auvipy commentedon Jun 4, 2025
thanks for confirming!
chrisbarber commentedon Jun 4, 2025
Looking more into my case, I found that switching to pickle for serialization completely solved the issue. Json recursively expands because it cannot handle the object references. So I ended up with 6MB messages whereas the pickle is tiny.
It seems there could be a handful of issues that could all be this same root cause. Namely the one we are commenting on plus
It seems there might be trouble collecting the right information in order to make sense of what is going on here, namely 1) what serializer people are using and 2) if switching serializer to pickle resolves the issue for people.
auvipy commentedon Jun 5, 2025
That's a very helpful finding! Thanks again
aurangzaib048 commentedon Apr 6, 2026
I've been investigating the root cause of this issue. Here's what I found and some questions for the @auvipy @Nusnus.
Root cause:
chord.freeze() stores self.body in each header task's options.chord field. In memory this is a shared reference (zero cost). But when header tasks are published to the broker via group._apply_tasks, each task is serialized independently (amqp.py:398). JSON serialization expands the full nested structure at every level since it has no concept of shared references.
With N header tasks per chord and D nesting depth, the outermost header body size is O(N^D). At depth 10 with 2 tasks per group, each header carries ~132MB of JSON.
This explains chrisbarber's finding that pickle resolves it, as pickle handles object references natively.
Questions:
auvipy commentedon Apr 21, 2026
we should first focus on the structural fix