# Architecture

> Gateway, extension and storage: how one semantic core serves RESP and SQL.

---

LLMS index: [llms.txt](/llms.txt)

---

pgredis is a [service API protocol](/docs/concepts/architecture/#protocol-classes) consisting of two components running on stock PostgreSQL 18:

- **`pgredis` (Go gateway):** accepts Redis client connections over TCP/TLS, parses RESP2 and RESP3 frames, handles authentication, maintains `MULTI` queues and session state, and manages per-identity PostgreSQL connection pools. Current source uses `pgx` v5.12.0 with a local read-budget patch; the `v0.1.0-rc1` packages use v5.11.0.
- **`pg_redis` (PostgreSQL 18 C extension):** implements Redis 7.2.4 standalone command semantics in schema `redis`, enforces privileges and lock order, runs a background expiry worker via `shared_preload_libraries`, and stores all keys in ordinary WAL-logged PostgreSQL tables.

SQL callers (`redis.command` and `redis.command_bin`) invoke the same C core directly inside their own PostgreSQL transaction.

```mermaid
flowchart LR
  Client["Redis client (RESP2 / RESP3)"] --> Gateway["pgredis Go gateway"]
  Gateway --> Core["pg_redis C extension"]
  SQL["SQL transaction (redis.command)"] --> Core
  Core --> Tables["PostgreSQL 18 tables and WAL"]
```

## Component responsibilities {#responsibilities}

| Component | Responsibilities |
| --- | --- |
| `pgredis` Go gateway | RESP2/RESP3 framing, TLS termination, authentication (`native` and `redis-acl`), `MULTI` command queueing, blocking/Pub/Sub polling, output buffer budgets and pooled connection reset |
| `pg_redis` C extension | Command execution, capability checks, database and key locking, binary key/member lookup, lazy and background expiry, tracking invalidation and `MONITOR` staging |
| PostgreSQL 18 | ACID transactions, write-ahead log (WAL), crash recovery, physical/logical backup and streaming replication |

## Section contents {#contents}

- [Request path](/docs/redis/architecture/request-path/) — how a RESP frame executes in a PostgreSQL transaction, commits, and resets its pooled backend connection.
- [Storage](/docs/redis/architecture/storage/) — the `redis.*` tables behind keys, collections, Streams, Pub/Sub, blocking waits and metadata.
- [Session lifecycle](/docs/redis/architecture/lifecycle/) — how blocking operations, Pub/Sub subscriptions, expired keys and pooled connections are managed and cleaned up.

---

Section pages:

- [Request path](/docs/redis/architecture/request-path/): From a RESP frame to a committed PostgreSQL transaction and back to the client.
- [Storage](/docs/redis/architecture/storage/): The tables behind each Redis data type and how keys, expiry and streams are stored.
- [Session lifecycle](/docs/redis/architecture/lifecycle/): Connections, authentication, pooled sessions, blocking commands and subscriptions over time.
