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 README with extra info about concurrency #520

Merged
merged 2 commits into from
Mar 24, 2025

Conversation

cedricpim
Copy link
Contributor

Add two notes on README about conurrency controls and how blocked jobs are unblocked and for how long can they be blocked.

Related to what was discussed in #502

Add two notes on README about conurrency controls and how blocked jobs
are unblocked and for how long can they be blocked.
@cedricpim cedricpim force-pushed the add-notes-on-concurrency branch from a2727ad to 25b58e1 Compare February 21, 2025 11:02
README.md Outdated
@@ -426,7 +426,7 @@ class MyJob < ApplicationJob

When a job includes these controls, we'll ensure that, at most, the number of jobs (indicated as `to`) that yield the same `key` will be performed concurrently, and this guarantee will last for `duration` for each job enqueued. Note that there's no guarantee about _the order of execution_, only about jobs being performed at the same time (overlapping).

The concurrency limits use the concept of semaphores when enqueuing, and work as follows: when a job is enqueued, we check if it specifies concurrency controls. If it does, we check the semaphore for the computed concurrency key. If the semaphore is open, we claim it and we set the job as _ready_. Ready means it can be picked up by workers for execution. When the job finishes executing (be it successfully or unsuccessfully, resulting in a failed execution), we signal the semaphore and try to unblock the next job with the same key, if any. Unblocking the next job doesn't mean running that job right away, but moving it from _blocked_ to _ready_. Since something can happen that prevents the first job from releasing the semaphore and unblocking the next job (for example, someone pulling a plug in the machine where the worker is running), we have the `duration` as a failsafe. Jobs that have been blocked for more than duration are candidates to be released, but only as many of them as the concurrency rules allow, as each one would need to go through the semaphore dance check. This means that the `duration` is not really about the job that's enqueued or being run, it's about the jobs that are blocked waiting.
The concurrency limits use the concept of semaphores when enqueuing, and work as follows: when a job is enqueued, we check if it specifies concurrency controls. If it does, we check the semaphore for the computed concurrency key. If the semaphore is open, we claim it and we set the job as _ready_. Ready means it can be picked up by workers for execution. When the job finishes executing (be it successfully or unsuccessfully, resulting in a failed execution), we signal the semaphore and try to unblock the next job with the same key, if any. Unblocking the next job doesn't mean running that job right away, but moving it from _blocked_ to _ready_. Since something can happen that prevents the first job from releasing the semaphore and unblocking the next job (for example, someone pulling a plug in the machine where the worker is running), we have the `duration` as a failsafe. Jobs that have been blocked for more than duration are candidates to be released, but only as many of them as the concurrency rules allow, as each one would need to go through the semaphore dance check. This means that the `duration` is not really about the job that's enqueued or being run, it's about the jobs that are blocked waiting. When there are multiple jobs unblocked, it is important to note that, as a job finishes and the next jobs are unblocked, the `duration` timer for the still blocked jobs is reset (this happens indirectly via the expiration time of the semaphore).
Copy link
Member

Choose a reason for hiding this comment

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

I'd rephrase this, as it's not really necessary that there are multiple jobs unblocked (because concurrency limit > 1). The behaviour is always the same.

Instead of:

When there are multiple jobs unblocked, it is important to note that, as a job finishes and the next jobs are unblocked, the duration timer for the still blocked jobs is reset (this happens indirectly via the expiration time of the semaphore).

I'd write this as:

It's important to note that after one or more candidate jobs are unblocked (either because a job finishes or because duration expires and a semaphore is released), the duration timer for the still blocked jobs is reset. This happens indirectly via the expiration time of the semaphore, which is updated.

Copy link
Contributor Author

@cedricpim cedricpim Mar 18, 2025

Choose a reason for hiding this comment

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

Much nicer 👍

@rosa
Copy link
Member

rosa commented Feb 24, 2025

Thanks a lot @cedricpim! I just left a small comment about one of the changes 🙏

@cedricpim
Copy link
Contributor Author

@rosa sorry for the delay on this. Just updated the PR.

@rosa
Copy link
Member

rosa commented Mar 24, 2025

Nice, thanks a lot!

@rosa rosa merged commit 9161da0 into rails:main Mar 24, 2025
33 of 34 checks passed
@cedricpim cedricpim deleted the add-notes-on-concurrency branch March 24, 2025 21:06
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.

2 participants