Skip to content

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):
    1. When a blocking command (BLPOP, BLMOVE, BZPOPMIN, XREAD BLOCK, XREADGROUP BLOCK, WAIT) cannot be satisfied immediately, redis.blocking_begin records the waiter, logical database, opaque token, command arguments and reply protocol.
    2. 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 PostgreSQL NOTIFY wakeup.
    3. Inside MULTI/EXEC, waiters only see the final state after the entire EXEC batch completes; an item pushed and deleted within the same EXEC never escapes to a blocked reader.
    4. The gateway polls redis.blocking_poll to retrieve the stored reply without re-executing the pop, or calls redis.blocking_cancel on disconnect.
  • Pub/Sub and tracking routes (redis._pubsub_*, redis._tracking_*):
    1. redis.pubsub_open and redis.pubsub_change register subscriptions in PostgreSQL tables with a lease deadline.
    2. Publishers write message pages to redis._pubsub_message (and tracking invalidations to redis._tracking_pending) inside their transaction and emit NOTIFY.
    3. 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_redis is loaded via shared_preload_libraries, a background coordinator discovers databases containing the extension and runs periodic workers that delete expired rows from redis._keyspace in batches, clean up expired blocking/Pub/Sub leases, trim tracking tables and compact mutation counters. PostgreSQL VACUUM later 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 in redis._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 = I and binary true receipt).
  • 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 WATCH state, blocking waits and Pub/Sub or tracking subscriptions.
Scan to open this page
https://mimus.pgsty.com/docs/redis/architecture/lifecycle/