Redis, inside your Postgres
pg_keyspace runs a Redis and Valkey compatible server inside Postgres. Your Redis client connects unchanged, and SQL reads the same keys.
# Any Redis client works. Here, redis-cli on the RESP port.
redis-cli -p 6379 SET session:42 '{"user":"ada"}' EX 3600
redis-cli -p 6379 HSET user:42 name Ada plan pro
redis-cli -p 6379 ZADD leaderboard 120 ada 95 grace
redis-cli -p 6379 ZRANGE leaderboard 0 -1 WITHSCORES
# The same keyspace, read from SQL in the same database.
psql "$DATABASE_URL" -c "
SELECT convert_from(supacache.get('session:42'), 'UTF8');"
# Write from SQL with a 60 second TTL, read it back over RESP.
psql "$DATABASE_URL" -c "SELECT supacache.set('flag:beta', 'on', 60);"
redis-cli -p 6379 GET flag:beta # "on"
# Tell RESP subscribers something from SQL, say from a trigger.
psql "$DATABASE_URL" -c "
SELECT supacache.publish('orders', 'shipped:42');"
# Hit rate, entries and memory, as a normal Postgres view.
psql "$DATABASE_URL" -c "SELECT * FROM supacache.pg_stat_keyspace;" Why pg_keyspace
Most teams that need a cache run Redis or Valkey beside Postgres. That is a second stateful service to deploy, secure, monitor and back up. It has its own users, its own backups and its own idea of what is current.
pg_keyspace moves the cache into Postgres. A background worker speaks the Redis protocol (RESP2 and RESP3) over a shared-memory keyspace. redis-cli, ioredis, redis-py and valkey-go connect without changes. The same bytes are readable from SQL, and a read inside a backend takes about 9 ns.
It keeps what you already trust. Redis AUTH maps to a Postgres role, keys can be scoped per tenant, and the wire can use TLS. Durability is a setting per key prefix, from memory only to fsync before the reply. Persisted keys live in ordinary tables, so your Postgres backups and point-in-time recovery cover the cache too.
pg_keyspace or a separate Redis or Valkey cluster
| a separate Redis or Valkey cluster | pg_keyspace | |
|---|---|---|
| What you run | A separate service or cluster to deploy, patch, secure and monitor. | A background worker inside the Postgres you already run. |
| Clients | Any Redis client. | Stock Redis clients, unmodified, over RESP2 or RESP3, including client-side caching. |
| Cache hit (GET, closed loop) | About 35 to 39 µs. | About 34 to 39 µs. |
| Pipelined throughput, one worker | About 537k to 628k ops/s. Higher on GET. | About 500k to 556k ops/s. More workers scale it out. |
| SQL access to the same data | None. It is a separate datastore. | Yes. supacache.get reads the same shared memory, about 9 ns in a backend. |
| Auth and tenancy | Redis ACLs, in a separate user store. | AUTH maps to a Postgres role. Keys and channels are scoped per tenant. TLS can reuse the Postgres certificate. |
| Durability | RDB snapshots and AOF, set for the whole instance. | Four tiers, from memory only to a synchronous standby, chosen per key prefix. |
| Backups | A separate artefact on a separate schedule with a separate restore. | Persisted keys are Postgres tables, so base backups and point-in-time recovery include them. |
| Monitoring | A Redis exporter to deploy and wire up. | pg_stat_keyspace views, granted to pg_monitor. Existing Postgres collectors read them. |
| Command and type surface | Wider. Adds streams, scripting, HyperLogLog and geo. | Strings, hashes, lists, sets, sorted sets, bitmaps, Bloom and Cuckoo filters, pub/sub and transactions. |
| Maturity | Mature and battle-tested, with a large ecosystem. | New, with a focused feature set. |
What it does not do
- No streams, Lua scripting (EVAL) or blocking commands such as BLPOP yet. They are planned with no date, and a client that sends them gets ERR unknown command. Check for both before treating it as a drop-in.
- FUNCTION and FCALL are declined. HyperLogLog is not planned, and geo is declined in favour of PostGIS.
- Valkey has a higher raw write ceiling with persistence off. If you need maximum single-node write throughput or mature cluster tooling today, stay on Valkey.
- It is opt-in. The library has to be preloaded, so turning it on is a restart. Its shared memory is reserved at startup whether or not the cache is full.
- The default tier is ephemeral, so keys live in memory and are lost on restart. The durable tiers wait for a Postgres commit: a median of 1.04 ms per write against 39 µs for ephemeral in the published benchmark.
- The replicated tier cannot be mixed with other tiers on one instance. It applies to the whole keyspace or not at all.
- A streaming standby serves no RESP until it is promoted. On a replica the keyspace is reachable only through SQL.
- With more than one worker and a persisted tier, clients need their cluster constructor, unless owner recovery addressing is on. Changing the worker count is a restart, and there is no online resharding.
- The PostgREST row cache is off by default and eventually coherent. After an UPDATE, another session can read the previous row for 200 ms plus decode time by default, and for longer when many databases share the invalidation pool.
- Pub/sub crosses instances only for channels you opt in, and delivery is at most once. Bloom and Cuckoo filters are reachable over RESP only, with no SQL functions yet.
Two ways to run it
pg_keyspace ships in the supatype/postgres image. Use the image as your Postgres, with or without the rest of Supatype.
# pg_keyspace ships in the image but is not loaded by default.
# SUPATYPE_KEYSPACE_ENABLED=1 adds it to shared_preload_libraries
# and creates the extension in POSTGRES_DB on first start.
docker run -d \
--name supatype-postgres \
-e POSTGRES_PASSWORD=your-password \
-e SUPATYPE_KEYSPACE_ENABLED=1 \
-e SUPATYPE_KEYSPACE_DURABILITY=ephemeral \
-e SUPATYPE_KEYSPACE_DURABILITY_OVERRIDES='acme:=durable' \
-p 5432:5432 \
-p 127.0.0.1:6379:6379 \
supatype/postgres:17-latest
redis-cli -p 6379 PING # PONG
# RESP asks for no password until you set one, so the port above
# listens on this machine only. Before you open it up:
# SELECT supacache.set_credential('app', 'secret', 'app_role', 'app'); Get the image already running, with migrations, access rules, an API, a typed client and Studio generated from your TypeScript types. Cloud starts with 2 projects, 500 MB database and 1 GB storage, free and with no card.