News for hackers. Stuff that matters.
Re:Do you really need Redis? How to get away with just Postg... by pilif on Saturday June 12, 2021 @04:43AM (#27482826)
For pub/sub, I would recommend against using PostgreSQL if you're doing it at any kind of scale because LISTEN ties up one connection completely and Postgres connections are very expensive compared to a redis connection.
Re:Do you really need Redis? How to get away with just Postg... by gunnarmorling on Saturday June 12, 2021 @04:52AM (#27482870)
The other issue with LISTEN/NOTIFY is that a client will miss any notifications sent while it is not running (e.g. due to crash, update, etc.). An alternative would be logical replication and change data capture (CDC), as provided for Postgres by Debezium for instance. That way, any consumers won't miss events during downtimes, as they'll continue to read from the replication slot from the last offset they had processed.

Disclaimer: I work on Debezium

Re:Do you really need Redis? How to get away with just Postg... by hardwaresofton on Saturday June 12, 2021 @05:24AM (#27483025)
Could you explain a little more about this (Debezium is awesome btw)? So after creating the proper replication slots, you set up change data capture via Debezium, then you listen to the CDC stream from the original DB you are connected to, correct?

This sounds like it might be vulnerable to dead consumers causing WAL to pile up without the right settings/periodic checks.

Re:Do you really need Redis? How to get away with just Postg... by gunnarmorling on Saturday June 12, 2021 @01:24PM (#27485951)
> then you listen to the CDC stream from the original DB you are connected to, correct?

Yes, exactly.

> This sounds like it might be vulnerable to dead consumers causing WAL to pile up without the right settings/periodic checks.

There are some subtleties around this indeed. You should monitor backlog of replication slots, so to identify inactive consumers as you say. Also, there are some corner cases you need to take care of: when listening to changes from a low-traffic database on the same PG host that also has a high-traffic database, the replication slot may not be acknowledged often enough. Debezium mitigates this by optionally writing changes to a dummy heartbeat table in the low-traffic database, so to advance the replication slot.

In Postgres 13 there's also a new option "max_slot_wal_keep_size" which limits WAL size retained by a replication slot. This prevents unbounded WAL growth, at the risk of consumers to miss events if they are down for too long.

All in all, proper monitoring and alerting is key.

Re:Do you really need Redis? How to get away with just Postg... by jbverschoor on Saturday June 12, 2021 @05:52AM (#27483160)
This is by design and very common with pub/sub.
Re:Do you really need Redis? How to get away with just Postg... by supermatt on Saturday June 12, 2021 @05:10AM (#27482960)
isnt this also the case for redis pubsub?
Re:Do you really need Redis? How to get away with just Postg... by Nick-Craver on Saturday June 12, 2021 @09:08AM (#27484221)
For pub/sub yes that's correct. For full info though: Redis later added streams (in 5.x) for the don't-wan't-to-miss case: https://redis.io/topics/streams-intro
Re:Do you really need Redis? How to get away with just Postg... by philjohn on Saturday June 12, 2021 @05:26AM (#27483040)
I'd need to check, but I think so.

Which is probably why if you don't want loss if consumers go down a proper queue system (RabbitMQ, ActiveMQ, Amazon SQS, Kafka if you don't care about ordering between partitions) is the way I'd go.

Re:Do you really need Redis? How to get away with just Postg... by MichaelMoser123 on Saturday June 12, 2021 @01:13PM (#27485868)
then what's the point of this feature as part of the db, if it doesn't do persistance?
Re:Do you really need Redis? How to get away with just Postg... by BiteCode_dev on Saturday June 12, 2021 @06:29AM (#27483387)
Then use streams.
Re:Do you really need Redis? How to get away with just Postg... by postgressomethi on Saturday June 12, 2021 @02:51PM (#27486633)
> LISTEN ties up one connection completely

I've seen this twice in this thread, but I don't know what that means. Can you explain a bit?

Re:Do you really need Redis? How to get away with just Postg... by sa46 on Saturday June 12, 2021 @04:33PM (#27487242)
When a client connects to Postgres, Postgres creates a new process just for that client. Separate processes are great for isolation but it means Postgres connections are a bit expensive (this is an active area of improvement). Postgres really starts to struggle once you a have a few thousand connections, even if those connections aren’t doing anything.

The common workaround is to use a connection pooler like PGBouncer so that clients reuse connections. This approach doesn’t work for LISTEN because typically a client will listen for its entire lifecycle so you can’t share connections in a pool.

Re:Do you really need Redis? How to get away with just Postg... by postgressomethi on Sunday June 13, 2021 @04:09AM (#27490752)
> This approach doesn’t work for LISTEN because typically a client will listen for its entire lifecycle so you can’t share connections in a pool.

But you only need one connection for LISTEN per database, total. So I'm confused why this is made out to be a big issue.

Re:Do you really need Redis? How to get away with just Postg... by taffer on Saturday June 12, 2021 @05:41PM (#27487649)
You need one connection per worker thread, and realistically you would only have one worker per cpu core. So how many LISTENing connections do you really need?
Re:Do you really need Redis? How to get away with just Postg... by sa46 on Saturday June 12, 2021 @06:49PM (#27488057)
You generally have more than one database connection per thread. As an example, consider Node.js which acts like a single threaded process. You’d probably want to be able to handle more than 1 database query concurrently since network latency tends to dominate OLTP requests.

How you setup LISTEN and NOTIFY is app dependent. In a multi-tenant database, you could have 1 NOTIFY channel per tenant.

As you scale, you probably do something that listens in a smarter way, maybe 1 Go channel per client with a single LISTEN instead of 1 database connection per client. The downside is that now the app code is responsible for tenant isolation instead of the database.

Re:Do you really need Redis? How to get away with just Postg... by taffer on Sunday June 13, 2021 @07:43AM (#27491724)
> [...] since network latency tends to dominate OLTP requests.

You are absolutely right about that. However, in this case we are talking about background workers. I would argue that background workers, such as an image resizing worker, are typically CPU-bound and do not perform high-volume OLTP queries. Therefore, a background worker does not require multiple database connections per thread as a web server would.

Re:Do you really need Redis? How to get away with just Postg... by dvfjsdhgfv on Saturday June 12, 2021 @04:46AM (#27482844)
I was looking for your comment here, thank you. I hope they solve this problem somehow in a future release.