# Rate limit per activity

**URL:** <https://community.temporal.io/t/rate-limit-per-activity/9956>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [October 28, 2023, 1:05pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956 "2023-10-28T13:05:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![saifxhatem](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/saifxhatem/32/1476_2.png) [@saifxhatem](https://community.temporal.io/u/saifxhatem)\
**Post date:** [October 28, 2023, 1:05pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/1 "2023-10-28T13:05:11Z")

</div>

I need to implement some rate-limiting on my activities.

Ideally, I don’t want to gimp my entire workflow (using `setMaxWorkerActivitiesPerSecond()` for example)  
because I only want to limit the activities that have outgoing HTTP requests.

My ideal solution would be that defining the maximum concurrent activities _per activity class_, but I can’t seem to find anything like this. What should be done in this case?

I assume the easiest way to achieve what I need is to have a separate task queue for the activities and assign the rate limit for that task queue, but since different APIs have different rate limits that would mean I need to make new task queues per API which is not a path I will willingly go down as this will need some more development and I need a quick solution.

Edit: I’ve tried separate task queues as suggested in [Rate limit activities - #4 by maxim](https://community.temporal.io/t/rate-limit-activities/2914/4) however it didn’t work as I expected.

Currently, I have the following:

- 2 workflows that share _some_ activities.
- Each workflow has its own worker, which registers its own activities (so the shared activities are defined twice, in two different workers)

What I’ve tried is moving all the activities that I need to ratelimit into their own Worker, on a completely new task queue. This new worker does _not_ register Workflow implementations, only activity implementations. This led to the workflow being unable to start the activity as “it is not registered with any worker”.

I also tried registering the workflow type in the new workers, but it seems this overrides the activities registered with the other worker. What am I missing here?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 28, 2023, 11:31pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/3 "2023-10-28T23:31:35Z")

</div>

You need to specify the new task queue name in ActivityOptions when invoking the activity.

---

<div class="post-metadata">

**Author:** ![saifxhatem](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/saifxhatem/32/1476_2.png) [@saifxhatem](https://community.temporal.io/u/saifxhatem)\
**Post date:** [October 28, 2023, 11:49pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/4 "2023-10-28T23:49:30Z")

</div>

Ah, apologies. I’ve tried that and the errors _are_ gone, however for testing I set the max to `0.1` which according to the docs specifies that we want 1 run every 10 seconds and ran 3 workflows, and the activities on the task queue seemed to be operating at normal speed.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 29, 2023, 6:31pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/5 "2023-10-29T18:31:37Z")

</div>

For such low rates make sure to [change](https://docs.temporal.io/references/dynamic-configuration#constraints) the default number of task queue partitions to 1.

---

<div class="post-metadata">

**Author:** ![saifxhatem](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/saifxhatem/32/1476_2.png) [@saifxhatem](https://community.temporal.io/u/saifxhatem)\
**Post date:** [October 29, 2023, 6:47pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/6 "2023-10-29T18:47:16Z")

</div>

The 0.1 was just for me to check if the rate limiter actually works before I deploy it to production. My actual limits will be 4 limits (depending on the target project) that will range from 120 to 600.

My testing was done locally (through the default docker-compose [https://github.com/temporalio/docker-compose/tree/main/dynamicconfig](https://github.com/temporalio/docker-compose/tree/main/dynamicconfig)), which did not have anything related to the queue partitions.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 29, 2023, 6:49pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/7 "2023-10-29T18:49:41Z")

</div>

I would recommend testing with the real cluster and rates. It is expected to work.

---

<div class="post-metadata">

**Author:** ![saifxhatem](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/saifxhatem/32/1476_2.png) [@saifxhatem](https://community.temporal.io/u/saifxhatem)\
**Post date:** [October 29, 2023, 6:51pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/8 "2023-10-29T18:51:03Z")

</div>

Well, I suppose worst case scenario is the same behaviour anyway. After migrating to AWS from GCP it seems that our workflows are running faster, which resulted in my project getting rate limited quite often by our other internal assets. This must work as there is no other way for me to rate-limit my outgoing http requests.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 29, 2023, 6:52pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/9 "2023-10-29T18:52:41Z")

</div>

Sorry, I don’t understand your concern. Do you mean that Temporal task queue rate limiting doesn’t work?

---

<div class="post-metadata">

**Author:** ![saifxhatem](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/saifxhatem/32/1476_2.png) [@saifxhatem](https://community.temporal.io/u/saifxhatem)\
**Post date:** [October 29, 2023, 7:01pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/10 "2023-10-29T19:01:02Z")

</div>

Yes that’s exactly what I mean.

```java
WorkerFactory factory = WorkerFactory.newInstance(client);
WorkerOptions workerOptions =
    WorkerOptions
        .newBuilder()
        .setMaxWorkerActivitiesPerSecond(0.1)
        .build();
Worker worker = factory.newWorker(HttpActivityQueues.ODOO_HTTP_TASK_QUEUE, workerOptions);

```

Edit: I have also tried `setMaxTaskQueueActivitiesPerSecond()` (since we have multiple workers) and this didn’t work either.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 29, 2023, 7:03pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/11 "2023-10-29T19:03:53Z")

</div>

0.1 requires at most one partition. Please file a bug report if you reproduce the problem with higher rates.

---

<div class="post-metadata">

**Author:** ![saifxhatem](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/saifxhatem/32/1476_2.png) [@saifxhatem](https://community.temporal.io/u/saifxhatem)\
**Post date:** [October 29, 2023, 7:04pm UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/12 "2023-10-29T19:04:32Z")

</div>

Alrighty, like I said we don’t have anything to lose, I will be sure to file a report if it happens.  
Thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![sebin](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@sebin](https://community.temporal.io/u/sebin)\
**Post date:** [January 15, 2024, 6:24am UTC](https://community.temporal.io/t/rate-limit-per-activity/9956/13 "2024-01-15T06:24:54Z")

</div>

I tried to do the same by providing a task queue for workflow and another task queue for the activity i wanted to rate limit. I ensured i gave the right task queue in workflow options and activity options.

However i observed that the rate limits are not honoured.

1. Is the rate limit on the task queue or the task queue partition? Because i was able to see 3 tasks submitted even though i gave the max activities per task queue as 1 for the activity worker.
2. What is the rate limit algorithm? Didn’t seem to be sliding window but something like token bucket.
