Celery parallel distributed task with multiprocessing
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Celery is a powerful, open-source distributed task queue that is widely used for handling asynchronous and scheduled tasks in production systems. Its ability to distribute work across multiple systems, parallelize tasks, and improve application performance makes it an excellent choice for tasks that can be offloaded to background processing. In this article, we will delve into how Celery can be used in a parallel and distributed manner utilizing multiprocessing, which is one of the cornerstones of its architecture.
Key Concepts
Task Queue
At its core, Celery provides a mechanism to queue tasks, allowing them to be processed in the background. The task queue architecture involves:
- Producers: These are responsible for creating tasks and sending them to a broker.
- Brokers: The middleware that handles the actual queuing of tasks. Common brokers include RabbitMQ, Redis, and Amazon SQS.
- Workers: These execute the tasks. Workers are distributed systems that pick up tasks from the broker and execute them.
Concurrency
Concurrency in Celery is achieved using multiprocessing, eventlet, or gevent. Here we focus on multiprocessing for parallel task execution. Multiprocessing leverages the CPU cores by spawning multiple processes, providing true parallelism.
Celery with Multiprocessing
Configuration
To enable multiprocessing in Celery, you simply need to configure the worker's concurrency setting. This can be done in the worker command line or the Celery configuration file.
Command Line
- Hardware: Number of CPU cores and system architecture.
- Nature of the Task: CPU-bound vs. I/O-bound tasks.
- Broker Performance: Latency and throughput capabilities of the broker impact overall performance.
- Scalability: Easily incorporate more workers to handle increased tasks.
- Fault Tolerance: If a worker fails, tasks can be reassigned.
- Concurrency Management: Efficient CPU utilization by leveraging multiple cores.
- A user uploads a file.
- The file undergoes multiple processing steps (e.g., virus scan, metadata extraction).
- Each of these steps can be a Celery task processed in parallel using multiprocessing.
Related reading
- Celery rpc vs amqp result backend
- Celery task schedule (Celery, Django and RabbitMQ)
- Celery tasks disappear
- Celery tasks received but not executing
- celery tasks, workers and queues organization
- Celery with Amazon SQS
- Change execution thread of the current method
- Change number of threads for Tensorflow inference with C API

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.