Session lifecycle
Connections, authentication, pooled sessions, blocking commands and subscriptions over time.
While keys and collections persist in PostgreSQL tables across restarts, blocking waits, Pub/Sub subscriptions, client-side caching routes and pooled connections carry session state that requires explicit lifecycle management.
Blocking commands and Pub/Sub
- Blocking registrations (
redis._blocked,redis._block_keys,redis._ready):- When a blocking command (
BLPOP,BLMOVE,BZPOPMIN,XREAD BLOCK,XREADGROUP BLOCK,WAIT) cannot be satisfied immediately,redis.blocking_beginrecords the waiter, logical database, opaque token, command arguments and reply protocol. - When a producer writes to a watched key (or when the timeout expires), a single PostgreSQL transaction consumes the element and writes the wire-formatted reply into
redis._ready, then sends a PostgreSQLNOTIFYwakeup. - Inside
MULTI/EXEC, waiters only see the final state after the entireEXECbatch completes; an item pushed and deleted within the sameEXECnever escapes to a blocked reader. - The gateway polls
redis.blocking_pollto retrieve the stored reply without re-executing the pop, or callsredis.blocking_cancelon disconnect.
- When a blocking command (
- Pub/Sub and tracking routes (
redis._pubsub_*,redis._tracking_*):redis.pubsub_openandredis.pubsub_changeregister subscriptions in PostgreSQL tables with a lease deadline.- Publishers write message pages to
redis._pubsub_message(and tracking invalidations toredis._tracking_pending) inside their transaction and emitNOTIFY. - The gateway drains committed batches via
redis.pubsub_poll. Lease expiry garbage-collects abandoned sessions if a gateway crashes, and bounded queue quotas close lagging subscribers when unread bytes exceed limits.
Key expiry and background maintenance
- Lazy expiry: Any foreground command that accesses an expired key removes its physical row inside the current transaction (or treats it as missing on a read-only path).
- Active background expiry: When
pg_redisis loaded viashared_preload_libraries, a background coordinator discovers databases containing the extension and runs periodic workers that delete expired rows fromredis._keyspacein batches, clean up expired blocking/Pub/Sub leases, trim tracking tables and compact mutation counters. PostgreSQLVACUUMlater reclaims dead tuples. - Maintenance pause (
DEBUG PAUSE-CRON 0|1): Administrators can pause periodic background maintenance in the current database by writing a postmaster-epoch-scoped control row inredis._debug_control. Workers check this row at the start of each maintenance cycle and skip background cleanup while paused, while foreground commands and lazy expiry continue normally. A PostgreSQL restart starts a new postmaster epoch and automatically resumes default background maintenance.
Pooled connection reuse and shutdown
- Each client connection has a single output writer goroutine in the gateway.
- A PostgreSQL backend connection returns to the gateway’s per-identity pool only after its business transaction commits and
redis._pool_reset()confirms an idle, clean session (ReadyForQuery = Iand binarytruereceipt). - Connections that encounter an unconfirmed commit, a reset error, a canceled context or an open transaction are closed and discarded from the pool.
- Shutting down or restarting the gateway or PostgreSQL terminates active client connections. Restart recovery restores committed keys and TTL deadlines in PostgreSQL, while clients must reconnect and re-establish
WATCHstate, blocking waits and Pub/Sub or tracking subscriptions.
