Skip to content

pg_redis_async

Transactional outbox and background delivery to explicit Redis targets

pg_redis_async · Transactional outbox and background delivery to explicit Redis targets

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102455pg_redis_asyncpg_redis_async0.0.3ETLMITRust

PostgreSQL · 15, 16, 17, 18, 19

Relationships
Requiresplpgsql

Usage

Sources:

pg_redis_async 0.0.3 is an initial Rust/pgrx extension for transactional delivery to independent Redis targets. A logged outbox records refresh requests; after commit, native workers execute a registered SQL projection against current committed state. Delivery is at least once and business commits do not wait for Redis.

Enable Explicit Targets

The running server and build must meet PostgreSQL 15.10+, 16.6+, 17.2+, 18.7+ or 19.x. Preserve existing preload entries, configure the intended business database, then restart. The launcher is database-free and starts senders only for the allowed target databases.

shared_preload_libraries = 'pg_redis_async'
pg_redis_async.databases = 'appdb'

Create the restricted sender role once per cluster and install the superuser-only, non-relocatable extension in each allowed database:

CREATE ROLE redis_async_sender NOLOGIN;
CREATE EXTENSION pg_redis_async;
SELECT redis_async.add_destination('cache', '{"host":"redis.example.internal",
  "security":{"auth":"none"},"min_pool_size":1,"max_pool_size":4,
  "options":{"max_inflight_batches":4}}');
SELECT fence_key FROM redis_async.stats('cache');

Replace the endpoint and choose authentication explicitly: security.auth is acl with username and password, password with a password, or none to skip Redis AUTH. The example uses no AUTH with verified TLS, not plaintext. TLS defaults to verify_full; private roots use ca_certificate, and mTLS also needs client_certificate and client_key. Plain TCP requires explicit tls_mode: "disabled". Each business database stores its own destination/security configuration. New databases start paused and targets disabled.

Rotate security with redis_async.set_destination_security('cache', $1::jsonb), binding the complete object. Set log_parameter_max_length and log_parameter_max_length_on_error to 0 on that connection and review proxy/audit logging. The private credential table is excluded from logical dumps; privileged readers, WAL, replicas and physical backups can still expose secrets.

Provision the returned fence key exactly once in an isolated Redis keyspace, with twenty zero digits, NX and no TTL. Protect the fence prefix from other writers and use noeviction or volatile-only eviction. Never reset an existing fence after key loss or snapshot rollback: isolate old sessions and rebuild with a new target identity.

Register a Projection and Enqueue

The application owns source tables, triggers, cache keys and routing. A projector has signature (logical_key text, context jsonb) RETURNS jsonb and emits the documented bounded operation plan. Use register_projector, configure_projector_policy and activate_projector to bind an immutable version, authorized enqueue roles, explicit target names and key prefixes. Grant the sender only CONNECT, schema USAGE, projection EXECUTE and necessary source SELECT, without superuser or RLS bypass.

Once the fence and a retry-safe product projector are configured, enable the target with redis_async.set_enabled('cache', true) and request the database sender with redis_async.request_worker_start(). The following psql example assumes the application’s table and projector already exist:

BEGIN;
UPDATE app.product SET name = 'tea' WHERE id = 42;
SELECT redis_async.enqueue('cache', 'product', '42');
SELECT redis_async.transaction_receipts(ARRAY['cache']) AS tickets
\gset
COMMIT;
SELECT (redis_async.wait_synced(:'tickets'::redis_async.receipt[], 2000)).*;

Enqueue after the related DML. Transaction and savepoint rollback remove their queued work. A wait may use another connection to the same database with the same session user or an authorized receipt reader. Wait on exact receipts for every intended target; sequence order and maximum values do not imply commit-order barriers.

Delivery and Operations

An applied receipt means Redis replied successfully and PostgreSQL committed the acknowledgment. It does not guarantee the value remains in Redis or reached its persistence/replicas. Projections must tolerate retries; reply loss triggers a fresh projection. Lua prevents interleaving but runtime errors can still leave partial writes. JSON.SET plans require that command on the destination.

Inspect stats() and database_stats(), and review dead letters before replay or retirement. By default quota exhaustion fails the business transaction with SQLSTATE 53000. Explicit fail-open configuration can return NULL or an empty receipt, or omit newly distinct dirty keys. Applied receipts default to 24 hours and retired receipts to seven days, both configurable; a ticket can become unknown after retention.

Pause a target, commit and wait for its quiescence token before maintenance. Before offline upgrade or drop, stop new enqueue, settle or retire work, commit request_worker_stop(), wait with wait_worker_stopped(token), remove the database from the allowlist and reload. Fence the old PostgreSQL primary before promotion. Logical restore starts paused with a new installation identity and no old receipts.

Coalesced Refresh and 0.0.3 Upgrade

Projectors must pin search_path without $user or pg_temp. When per-write receipts are unnecessary, redis_async.mark_dirty('cache', 'product', '42') coalesces repeated keys and the sender later reads current state. It supplies no independent receipt for each write and does not replace the enqueue workflow when exact acknowledgment is needed.

Strict fencing remains the default. Optional auto_bootstrap_cache gives weaker guarantees after cache keyspace loss and is not equivalent to strict fence recovery. Version 0.0.3 explicitly remains early beta and unsuitable for production.

ABI 3 removes the credential file and its SQL interfaces. The supplied 0.0.2-to-0.0.3 upgrade accepts pending/dead work only after pause, sealing, sender stop/removal and exclusive ownership. Replace the loaded library while PostgreSQL is stopped, restart, then run ALTER EXTENSION pg_redis_async UPDATE TO '0.0.3'. Review pinned projector search paths and destination security before resuming.

Logical restore preserves explicit no-AUTH but starts paused with a new fence. Password/ACL destinations need new credentials, and destinations using private TLS material need it reprovisioned. Inspect stats() limits auth_mode, requires_tls_material and security_configured. Restore configuration alone does not recover queued work or reconstruct the Redis cache.

Table reader