r/PostgreSQL • u/compy3 • 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=postgresqlhey 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?
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.
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.
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.