News for hackers. Stuff that matters.
Re:System design hack: Postgres is a great pub/sub and job s... by fizwhiz on Friday November 08, 2019 @01:25PM (#21485155)
Isn't hijacking a DB as a "distributed" message queue a pretty well trodden path? Enterprises have been doing this for decades.
Re:System design hack: Postgres is a great pub/sub and job s... by colinchartier on Friday November 08, 2019 @01:34PM (#21485250)
This pattern falls down if you need to poll the database, because if you have 3 queues and 100 workers you're making 300 queries per poll interval.

The feature of postgres that makes this viable in comparison to most other databases is the "channel"

Re:System design hack: Postgres is a great pub/sub and job s... by gigatexal on Friday November 08, 2019 @01:42PM (#21485340)
The caveat to this is in the first paragraph: all of the data is stored in memory id each row is ~1kb and the table size has some sort of TTL you’ll be good.
Re:System design hack: Postgres is a great pub/sub and job s... by colinchartier on Friday November 08, 2019 @01:44PM (#21485372)
Yeah, but even without tuning, you can have hundreds of "sparse" queues with this pattern - you don't have to care about the frequency of insertions or deletions or the size of the rows.
Re:System design hack: Postgres is a great pub/sub and job s... by gigatexal on Friday November 08, 2019 @01:47PM (#21485392)
I mean I agree I think with what I think you’re saying: as much as I love Postgres and SQL I’d much rather use Redis for this.
Re:System design hack: Postgres is a great pub/sub and job s... by colinchartier on Friday November 08, 2019 @01:53PM (#21485432)
I actually used redis pub/sub before switching to triggers - it is significantly easier to just:

  INSERT INTO JOBS
versus

  INSERT INTO JOBS
  redis-cli LPUSH job-queue job-id
It's also easier to configure, redis has BRPOPLPUSH for acked-queues, but it's much easier to just query jobs with a select statement from the database.
Re:System design hack: Postgres is a great pub/sub and job s... by Rapzid on Friday November 08, 2019 @03:12PM (#21486211)
Obviously "it depends" but here are a few reasons one may want to use PostgreSQL instead:

1.) *They want to transactionally commit work along with the change that caused it

2.) They are already using Postgresql not using Redis

3.) Requiring users install yet another service(Redis) just for this one item isn't worth the costs

Re:System design hack: Postgres is a great pub/sub and job s... by gigatexal on Friday November 08, 2019 @03:36PM (#21486452)
Redis modifications are transactional: https://redis.io/topics/transactions

Though I agree on points 2 and 3, especially 3 since it adds complexity.

Re:System design hack: Postgres is a great pub/sub and job s... by erichocean on Saturday November 09, 2019 @03:36PM (#21494185)
> Redis modifications are transactional

That's only true if they do not error—there is no "rollback" feature in Redis, and if you do error, anything you've done up to that point remains.

That's not transactional, it's more "you can, sometimes, execute a bunch of separate statement atomically".

Re:System design hack: Postgres is a great pub/sub and job s... by gigatexal on Saturday November 09, 2019 @04:14PM (#21494393)
I’ll admit I don’t know the internals of Redis as well as I do relational databases. I’ve learned much from this thread.
Re:System design hack: Postgres is a great pub/sub and job s... by Rapzid on Friday November 08, 2019 @03:52PM (#21486604)
Internally but if the change causing the work is in PostgreSQL Redis would need to support 2-phase commits to commit the change and the work together atomically(and durably).
Re:System design hack: Postgres is a great pub/sub and job s... by MuffinFlavored on Friday November 08, 2019 @02:40PM (#21485918)
What if your listeners crashed/were down at the time of the `PUB` message? Does this mean the message falls into oblivion (since it will never receive a reply/ACK/get worked on)?
Re:System design hack: Postgres is a great pub/sub and job s... by privateSFacct on Friday November 08, 2019 @07:05PM (#21488358)
Is the activity wrapped in a transaction?

If so if worker crashes you get a connection timeout on postgresql and it rolls back the transaction and releases row locks - then next select SKIP picks it up.

If you want fast response with safety listen and on restart do a poll always to pick up anything that's ready that you might have missed a pub for. If you want lots of coverage do a poll every 10s - postgresql can handle it, you still get immediate response by listening to notify.

If you need fancier approaches you can do a last_updated time on the job and or watch waiting on pg_stat_activity or similar etc - but all totally overkill I think - the simple approach get's pretty far (not an expert though at all).

Re:System design hack: Postgres is a great pub/sub and job s... by colinchartier on Friday November 08, 2019 @02:42PM (#21485936)
The pattern I follow is that workers immediately poll once when they start - this means that when your workers restart after crashing, they'll pick up any missing jobs.
Re:System design hack: Postgres is a great pub/sub and job s... by pm90 on Friday November 08, 2019 @02:58PM (#21486098)
I'm not seeing how this recovers from the failure scenario where a worker grabbed an event and then immediately crashed.
Re:System design hack: Postgres is a great pub/sub and job s... by Rapzid on Friday November 08, 2019 @03:07PM (#21486175)
The comment you are replying to wasn't addressing that scenario.

Typically you would implement visibility timeouts and other such stuff. Depending on the use case you could make specific optimizations or keep it generic and have SQS like semantics or something.

Re:System design hack: Postgres is a great pub/sub and job s... by disiplus on Friday November 08, 2019 @03:29PM (#21486379)
at this point why not use something like rabbitmq and not reinvent it in postgres.
Re:System design hack: Postgres is a great pub/sub and job s... by stickfigure on Friday November 08, 2019 @03:47PM (#21486542)
Because it's trivially easy. The dequeue operation updates the last_lease time; the query makes sure you don't dequeue something that was recently leased. It's far, far easier than setting up a separate job queue, especially if you are already using an RDBMS.
Re:System design hack: Postgres is a great pub/sub and job s... by rapind on Friday November 08, 2019 @03:46PM (#21486533)
Because then you're running and maintaining another service. Might make sense, but depends.
Re:System design hack: Postgres is a great pub/sub and job s... by tjungblut on Friday November 08, 2019 @03:07PM (#21486170)
You need some timeout and/or leasing mechanism to make it work in this scenario.
Re:System design hack: Postgres is a great pub/sub and job s... by Ericson2314 on Saturday November 09, 2019 @07:17PM (#21495485)
You should never need to poll. Polling is bad.
Re:System design hack: Postgres is a great pub/sub and job s... by cuddlecake on Friday November 08, 2019 @03:23PM (#21486314)
You can have 3 queues and 3 dispatchers, arrange some backpressure, a push-based consumer-producer model and you will have 3 queues, 300 queries and n polls per interval.

There is a solution to every perceived problem.

Re:System design hack: Postgres is a great pub/sub and job s... by schwarzmx on Friday November 08, 2019 @03:37PM (#21486460)
At what point does solving these "new" problems become reinventing the wheel?
Re:System design hack: Postgres is a great pub/sub and job s... by epx on Friday November 08, 2019 @01:34PM (#21485255)
Yeah but the propaganda around the "brave new world" solutions is so strong it makes it sound novel again.