Repository navigation
Description
Hello, we're using sequences to ensure only one job is running at a given time "per entity". We have a couple of workers (~20ish) that are all set to use an id field as river:"sequence" - some of which will generate follow up jobs for the same entity - think regular maintenance jobs or jobs that first create things, call an external service and then start things. This works fine on production (so far) however our test suite is becoming slower and slower.
The docs states that
If there are no actively running jobs in a sequence, the first job in that sequence may encounter a higher latency before being moved to available by the sequence maintenance process. This latency does not apply to subsequent jobs in the sequence if they are already enqueued when the previous job completes; such subsequent jobs will be scheduled immediately.
but it seems like this is always true for jobs that are created within a sequenced job for the same entity.
For example (im using the same TaskArgs here, but this also happens with different TaskArgs/Workers):
type TaskArgs struct {
EntityId int `json:"entityId" river:"sequence"`
RescheduleCounter int `json:"rescheduleCounter"`
}
func (worker *TaskWorker) Work(ctx context.Context, job *river.Job[TaskArgs]) error {
// ...
_, err := client.Insert(context.Background(), TaskArgs{...}, nil)
// ...
}When run you can see the "1 second" within the logging. For example 10 jobs will usually take around 10-12 seconds.
$ go test
2025/06/24 10:57:12 INFO Work() started entityId=13 rescheduleCounter=10
2025/06/24 10:57:12 INFO Scheduled next job nextArgs="{EntityId:13 RescheduleCounter:9}"
2025/06/24 10:57:13 INFO Work() started entityId=13 rescheduleCounter=9
2025/06/24 10:57:13 INFO Scheduled next job nextArgs="{EntityId:13 RescheduleCounter:8}"
2025/06/24 10:57:14 INFO Work() started entityId=13 rescheduleCounter=8
<snip>
2025/06/24 10:57:21 INFO We're done! entityId=13
PASS
ok rivertestexecution 10.267s
I've build a small river test execution project to recreate the issue in isolation. It has a basic worker implementation calling itself X times to showcase the issue.
Is there a way to work around this timer / issue?
Activity
bgentry commentedon Jun 25, 2025
Hi @marttinen, sorry for the delay on this. As a Pro customer you can always feel free to email us at team@riverqueue.com for direct support, but I really appreciate you putting together an extensive reproduction like this!
Your report actually led me to uncover a related bug that hadn't shipped yet (introduced by mistake only a few days ago) so I was able to get that one fixed preemptively. Thanks 😅
After fixing that issue, my hunch was that yours is actually a result of the fetch poll interval of your client which defaults to 1 second. Here's your tests running as is:
And if I change
FetchPollIntervalto 100ms, I see this:That gets us down to a considerably shorter interval, but honestly still much higher than I was expecting. To explain, here's the sequence of events as I understand it:
pending(notavailable).FetchPollIntervaltick following the 100msFetchCooldown. When it does finally fetch again, that next job is ready to go.If I change your example so the worker inserts the new jobs as part of a transaction that also completes the running job, and also lower the
FetchPollIntervalto 100ms, I see a much better result:Here's what that diff looks like:
git diff
I know it's not a totally satisfying answer, and there may be some more things we can look into to further lower latency here, but AFAICT this seems to be mainly the result of having a single client with longer default fetch intervals. I think there's more we can look into about how the various buffers and delays can add up to a fair bit of latency, and we've talked about this previously as something to look into.
Hoping to have a bit more time to keep digging more on this tomorrow.
0x6ee3 commentedon Jun 25, 2025
Hey @bgentry, thank you already for the elaborate feedback.
So far I've only tweaked the FetchPollInterval and I can reproduce the performance increase in the test project (very similar to your results). I will look into using transactions next.