Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a rate limit on a celery queue?

Tags:

celery

I read this in the celery documentation for Task.rate_limit:

Note that this is a per worker instance rate limit, and not a global rate limit. To enforce a global rate limit (e.g., for an API with a maximum number of requests per second), you must restrict to a given queue.

How do I put a rate limit on a celery queue?

like image 750
Vikash Singh Avatar asked Jan 30 '15 07:01

Vikash Singh


People also ask

How do you implement the rate limit?

Often rate-limiting is applied at a reverse proxy, API gateway, or load balancer before the request reaches the API, so that it can be applied to all requests arriving at a cluster of servers. By handling this at a proxy server, you also avoid excess load being generated on your application servers.

What is request rate limit?

A rate limit is the number of API calls an app or user can make within a given time period. If this limit is exceeded or if CPU or total time limits are exceeded, the app or user may be throttled. API requests made by a throttled user or app will fail. All API requests are subject to rate limits.

How do celery queues work?

Celery communicates via messages, usually using a broker to mediate between clients and workers. To initiate a task, the Celery client adds a message to the queue, and the broker then delivers that message to a worker. The most commonly used brokers are Redis and RabbitMQ.

Is celery a queue?

Celery is an open source asynchronous task queue or job queue which is based on distributed message passing. While it supports scheduling, its focus is on operations in real time.


1 Answers

Turns out it cant be done at queue level for multiple workers. IT can be done at queue level for 1 worker. Or at queue level for each worker.

So if u say 10 jobs/ minute on 5 workers. Your workers will process upto 50 jobs per minute collectively.

So to have only 10 jobs running at a time you either chose one worker. Or chose 5 workers with a limit of 2/minute.

Update: How to exactly put the limit in settings/configuration:

task_annotations = {'tasks.<task_name>': {'rate_limit': '10/m'}}

or change the same for all tasks:

task_annotations = {'*': {'rate_limit': '10/m'}}

10/m means 10 tasks per minute, /s would mean per second. More details here: Task annotations setting

like image 126
Vikash Singh Avatar answered Oct 01 '22 20:10

Vikash Singh