r/PostgreSQL • • 1d ago

How-To How to choose a Postgres read cache: Redis, Memcached, build your own, or proxy

https://www.pgcache.com/blog/choosing-a-postgres-read-cache/?utm_source=reddit&utm_content=postgresql

hey everyone - a couple months ago, I posted an overview of caching in Postgres. Seemed like it was helpful to a lot of people, and I've noticed there's not a lot of clear content on this topic out there. So I wrote this is the follow-up, for anyone who has decided to cache reads and is trying to figure out how to do it.

In addition to the main options, the article also covers some of the classic zingers that leave you serving stale data: invalidation, stampedes, tenants and RLS, read-your-own-writes, schema changes.

(disclaimer: I'm one of the founders of PgCache, which is one of the proxy options)

Did I miss anything?

19 Upvotes

8 comments sorted by

5

u/Discordbothost 1d ago

One option worth adding: LISTEN/NOTIFY-driven invalidation - have writers NOTIFY a channel on commit so the cache gets push-based invalidation without polling, at the cost of a persistent connection per cache node.

3

u/compy3 1d ago

Thanks! I covered some of this in a section called "Triggers and LISTEN/NOTIFY." I agree it's really appealing since it's all built into Postgres and only fires on commit.

A couple potential issues that I flag in the article: (1) nothing is replayed if the listener disconnects, (2) LISTEN doesn't work through PgBouncer's transaction pooling, and (3) under heavy concurrent writes the commit lock can cause problems (Recall.ai wrote up outages related to this).

2

u/DuckDatum 1d ago edited 1d ago

Alternatively, pg_logical_emit_message() combined with logical replication slots?

I think something like this can have at least once delivery semantics.

Configure with

CREATE PUBLICATION event_stream;
SELECT pg_create_logical_replication_slot('event_consumer', 'pgoutput');

Another option if you don’t mind getting your hands dirty…

SELECT cron.schedule(
'retry_notifications',
'5 seconds',
$$
SELECT pg_notify('message_ready', '')
WHERE EXISTS (
SELECT 1 FROM message_queue
);
$$
);

5

u/ants_a 1d ago

A misconception that I see is that databases are slow and caches are fast. Adding a caching service is just adding a new database to your infrastructure - a cache is just a key-value database. It can benefit from being optimized for the specific use case, but people tend underestimate what kind of performance you can get out of postgres with a little bit of mechanical sympathy. Just for some ballpark numbers, executing a prepared statement for a primary key lookup takes less than 10µs, of which the actual index lookup is about 1-2µs. You can do north of 1M qps on a single desktop class CPU. If you design your schema with your read patterns in mind then you might not need a dedicated cache at all.

1

u/compy3 18h ago

You're correct! In most cases a read cache isn't necessary at all. But when it is ... you gotta know your options 😁 And it usually just comes down to tradeoffs. Some folks will need a cache for speed, some will need it to take load off the primary ... some might want it to serve as a CDN for data and cache rows not keys.

Key value stores are just one approach. The postgres proxy we're building - PgCache - is wire compatible and caches data rows not keys. Different tradeoffs.

1

u/waldiesel 3h ago

In real life, we do not get loopback performance. You need to carefully tune pgdog/bouncer. The db can be under heterogeneous load and basic requests need not bother the db. It is a way to allow for scaling to the load and provide consistent latencies or moving the cache closer to point of use. For a small system it is unneeded complexity to add caching.

0

u/AutoModerator 1d ago

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.