Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of enqueueing tasks #946

Merged
merged 3 commits into from
Oct 30, 2024

Conversation

pior
Copy link
Contributor

@pior pior commented Oct 28, 2024

Context

Before enqueueing every task, the name of the queue is published to a Redis Set key (base.AllQueues).
This is only used by the inspector for inspection/monitoring purposes (not for the processing of tasks).

Two consequences:

  • Enqueuing one task requires two round-trip to Redis
  • The same Redis set key is updated constantly, causing significant Redis load

The issue #549 has more details about the impact on performance.
We've confirmed the figures in production.

Fixes #549
Rework of #550

Changes

  • Update the RDB client to only write to base.AllQueues once per worker

Notes:

  • This light implementation does not protect against race-conditions, which is not an issue.
  • Writing to base.AllQueues will still be retried until it worked.

Add an in-memory cache to keep track of all the queues. Use this cache
to avoid sending an SADD since after the first call, that extra network
call isn't necessary.

The cache will expire every 10 secs so for cases where the queue is
deleted from asynq:queues set, it can be added again next time a task is
enqueued to it.
@pior pior changed the title Improve performance of enqueueing tasks - take 2 Improve performance of enqueueing tasks Oct 28, 2024
@pior pior force-pushed the optimize-enqueue-sadd branch from e4c7ff1 to 2e99b71 Compare October 28, 2024 17:35
Copy link
Collaborator

@kamikazechaser kamikazechaser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the queue is removed via RemoveQueue, it will not be added back on the next enqueue because it will still exist in the map. We need to also remove it from the map on any SREM("asynq::queues", qname)

@pior
Copy link
Contributor Author

pior commented Oct 29, 2024

If the queue is removed via RemoveQueue, it will not be added back on the next enqueue because it will still exist in the map. We need to also remove it from the map on any SREM("asynq::queues", qname)

Right.
Removing from the queue is a nice, simple solution.

But is it enough to remove the queue from the map "locally" (in the same process).
It works if a task is enqueued to the queue in the same process, but won't work for a different process.

Are we ok with this tradeoff?

(Implementation and tests updated)

@pior pior force-pushed the optimize-enqueue-sadd branch from 8525fa3 to 24da63e Compare October 29, 2024 09:24
@kamikazechaser
Copy link
Collaborator

It works if a task is enqueued to the queue in the same process, but won't work for a different process.

That's right. I think the tradeoff is fair.

@kamikazechaser kamikazechaser merged commit 3dbda60 into hibiken:master Oct 30, 2024
8 checks passed
@pior pior deleted the optimize-enqueue-sadd branch October 30, 2024 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve performance of enqueueing tasks
3 participants