zlacker

[return to "Choose Postgres queue technology"]
1. aduffy+82[view] [source] 2023-09-24 20:41:49
>>bo0tzz+(OP)
For several projects I’ve opted for the even dumber approach, that works out of the box with every ORM/Query DSL framework in every language: using a normal table with SELECT FOR UPDATE SKIP LOCKED

https://www.pgcasts.com/episodes/the-skip-locked-feature-in-...

It’s not “web scale” but it easily extends to several thousand background jobs in my experience

◧◩
2. surpri+rg[view] [source] 2023-09-24 22:39:31
>>aduffy+82
I recently published a manifesto and code snippets for exactly this in Postgres!

  delete from task
  where task_id in
  ( select task_id
    from task
    order by random() -- use tablesample for better performance
    for update
    skip locked
    limit 1
  )
  returning task_id, task_type, params::jsonb as params
[1] https://taylor.town/pg-task
◧◩◪
3. thom+yy[view] [source] 2023-09-25 02:11:13
>>surpri+rg
Presumably it's okay that this loses work if your task runner has an error?
◧◩◪◨
4. surpri+kz[view] [source] 2023-09-25 02:23:54
>>thom+yy
If you read my guide, you’ll see that I embed it in a transaction that doesn’t COMMIT until the companion code is complete :)

For example, I run the above query to grab a queued email, send it using mailgun, then COMMIT. Nothing is changed in the DB unless the email is sent.

◧◩◪◨⬒
5. maxbon+IO[view] [source] 2023-09-25 05:39:21
>>surpri+kz
Holding a transaction open for the duration of a request to an external service makes me nervous. I've seen similar code lock up the database and bring down production. Are you using timeouts and circuit breakers to control the length of the transactions?
[go to top]