An UNLOGGED table is a cache that speaks SQL
If you want a cache and you already have Postgres, an UNLOGGED table skips the write-ahead log. Writes are close to memory speed, and the table is empty after a crash, which is exactly what a cache should be.
CREATE UNLOGGED TABLE cache (
key text PRIMARY KEY, value jsonb NOT NULL, expires_at timestamptz NOT NULL
);
CREATE INDEX ON cache (expires_at);A cleanup job deletes expired rows. Reads use the connection the request already has, so there is no second pool to run out of.
postgresqlcaching
Longer version: the post this came from.