Skip to content

One semantic core

Why RESP and SQL share one C implementation of Redis semantics.

Decision

Implement Redis 7.2.4 command semantics once in the pg_redis PostgreSQL 18 C extension, and use the pgredis Go gateway strictly for wire-protocol framing, TLS, authentication, session queues and connection pooling. Both RESP clients and SQL callers (redis.command, redis.command_bin) invoke the same C command core, with PostgreSQL tables and WAL as the only persistent store.

Rationale

Implementing Redis data semantics in the Go gateway would require duplicating PostgreSQL’s transaction isolation, savepoint rollback, MVCC visibility, expiry cleanup and role-permission enforcement across two processes. Putting the semantic core inside the PostgreSQL extension gives SQL table writes and Redis key mutations a single atomic commit boundary.

The Go gateway still performs essential transport work:

  • Negotiating TLS and parsing RESP2 / RESP3 frames under strict memory budgets.
  • Holding MULTI command queues before EXEC submits them in one transaction.
  • Driving long-lived blocking waits, Pub/Sub subscriptions, client-side caching pushes and MONITOR streams across pooled backend connections.
  • Verifying commit confirmation (ReadyForQuery) and running redis._pool_reset() before returning connections to the pool.

Trade-offs and invariants

  • Requires superuser and preload: Because pg_redis is a C extension that registers shared memory and a background expiry worker via shared_preload_libraries, it cannot be deployed as a gateway-only proxy in front of unprivileged managed PostgreSQL services.
  • Database-level write locking: Protecting cross-key and cross-database Redis invariants inside SQL transactions uses a per-database exclusive lock for writes other than plain GET/SET, trading single-database write concurrency for strict transactional consistency in 0.1.0.
  • Non-negotiable invariants: Every performance optimization must preserve byte-exact binary keys and values, lock ordering, PostgreSQL role permission checks, synchronous_commit = on by default, and the rule that an operation with an unknown commit outcome is never replayed.
Scan to open this page
https://mimus.pgsty.com/docs/redis/design/semantic-core/