Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/postgres-bus-driver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'bentocache': minor
---

Add `postgresBusDriver`, a bus driver that uses Postgres `LISTEN/NOTIFY`. Import it from `bentocache/drivers/postgres`. It needs the `pg` package.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ For those looking to go further, you can use the two-levels caching system. Here

- **L1: Local Cache**: First level cache. Data is stored in memory with an LRU algorithm for quick access
- **L2: Distributed Cache**: If the data is not in the in-memory cache, it is searched in the distributed cache (Redis, for example)
- **Synchronization via Bus**: In a multi-instance context, you can synchronize different local in-memory caches of your instances via a Bus like Redis or RabbitMQ. This method maintains cache integrity across multiple instances
- **Synchronization via Bus**: In a multi-instance context, you can synchronize different local in-memory caches of your instances via a Bus like Redis or Postgres. This method maintains cache integrity across multiple instances

Here is a simplified diagram of the flow :

Expand Down
121 changes: 121 additions & 0 deletions docs/content/docs/multi_tier.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,124 @@ redisBusDriver({
```

`maxSize` is the maximum number of items that can be stored in the retry queue. If the queue is full, the oldest item will be dropped. If `undefined`, the queue will have no limit.

### Postgres bus

If you already run Postgres, you can use it as the bus instead of Redis. `postgresBusDriver` uses Postgres `LISTEN/NOTIFY`. Combined with an in-memory L1 and, optionally, a [database L2](./cache_drivers.md), it lets you run a multi-tier cache without Redis.

Install the `pg` package first:

```sh
npm i pg
```

With TypeScript, also install `@types/pg`.

```ts
import { BentoCache, bentostore } from 'bentocache'
import { memoryDriver } from 'bentocache/drivers/memory'
import { postgresBusDriver } from 'bentocache/drivers/postgres'

const bento = new BentoCache({
default: 'cache',
stores: {
cache: bentostore()
.useL1Layer(memoryDriver({ maxSize: '10mb' }))
.useBus(postgresBusDriver({ connection: process.env.DATABASE_URL! })),
},
})
```

`connection` takes a connection string or a [`pg` client config](https://node-postgres.com/apis/client). From it, the driver opens two connections per store and per process: one that listens and one that publishes. It doesn't create any table or other database object.

All your instances must connect to the same database: Postgres delivers notifications only within a database.

#### Options

| Option | Default | Description |
| ---------------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `connection` | | Connection string or `pg` client config. Required. |
| `publishTimeout` | `'2s'` | Maximum time for one publish transaction, which carries up to 1,000 notifications or 1 MB. A publish that takes longer fails and goes to the [retry queue](#retry-queue-strategy). |
| `batchDelay` | `0` | Extra time the driver waits after a message before sending it, so that more messages share one transaction. Every write (`set`, `delete`, `clear`...) waits for its publish, so a delay adds to write latency in exchange for fewer Postgres transactions. |
| `heartbeat` | `{ interval: '30s', timeout: '15s' }` | Every `interval`, the driver publishes a message to itself. If a heartbeat was committed but not received within `timeout`, the listening connection is replaced. Set to `false` to disable. See [Heartbeat](#heartbeat). |
| `onError` | | Called with connection errors, failed publishes and dropped messages, for logging. The driver reconnects on its own. Without it, errors are logged with `console.error`, each distinct message at most once a minute. |
| `retryQueue` | | Same as for `redisBusDriver`. |

#### What the connection must support

The connection must reach the primary, directly or through a proxy, and `LISTEN` must work on it:

- A direct connection to Postgres, or a TCP proxy such as HAProxy that routes to the primary, works.
- PgBouncer works in session mode. In transaction mode, `LISTEN` doesn't work.
- Other poolers work if they support `LISTEN/NOTIFY` in the mode you use. PgDog, for example, needs its pub/sub support enabled in transaction mode.

Many hosting providers show a pooled connection string in transaction mode by default. Use a direct or session-mode one instead:

- Supabase: the direct connection, or the session pooler on port 5432. Not the transaction pooler on port 6543.
- Neon: the hostname without `-pooler`.
- DigitalOcean: the database cluster itself, not a connection pool in transaction mode.
- Azure Database for PostgreSQL: port 5432. The built-in PgBouncer on port 6432 works only if its pool mode is `session`.

If `LISTEN` doesn't work on the connection, the heartbeat fails and the driver reports it every time it reconnects. See [If the heartbeat fails](#if-the-heartbeat-fails).

If a proxy closes idle connections, such as HAProxy with its `timeout client` and `timeout server` settings, keep `heartbeat.interval` below that timeout. The heartbeat is what keeps both connections active. The same applies to the database's `idle_session_timeout`, if you set one: the publishing connection keeps it, and is replaced if it is closed.

Don't use the Postgres bus where processes are frozen between requests, such as AWS Lambda. A frozen process reads no notifications, so its L1 keeps old values, and its listening session holds back the server's notification queue (see below).

#### How it behaves

- Messages are sent in short transactions, each in one round trip, with `synchronous_commit` turned off for that transaction. Every such transaction uses a transaction id, writes to the WAL, takes a lock shared by the whole Postgres cluster and wakes every listening connection, so the driver sends as few as it can:
- A message is sent as soon as no transaction is running. Messages published while one runs are sent together in the next. With a `batchDelay`, a batch also waits that long after its first message, unless it already holds 1,000 messages.
- Messages in a batch with the same type and namespace are merged into one notification, with duplicate keys removed. Other instances can therefore see fewer `bus:message:received` events than were published, and messages of different types or namespaces can arrive in a different order. The caches end up the same, because every message only deletes or expires entries.
- A very large batch is split over several transactions.
- BentoCache waits for the publish in every write, so each `set`, `delete` or `clear` takes at least one round trip to the primary, plus `batchDelay`.
- Right after start, publishes wait for the first connection, up to `publishTimeout`. Later, while the publishing connection is down, publishes fail right away and go to the retry queue. They are sent as soon as the connection is back. The retry queue resends one message at a time, each in its own transaction.
- `disconnect()` first sends the messages still waiting, within `publishTimeout`.
- Postgres limits a notification to 8000 bytes. Messages with many keys are split across several notifications. If a single key doesn't fit, or a key isn't a string, the driver sends a `clear` of the key's namespace instead and reports it to `onError`. This only keeps other instances from serving stale entries: only key names are sent, never values, so it takes a key name of about 8 KB, which is very unlikely.
- Messages sent while an instance's listening connection is down, including right after start before it is ready, are lost for that instance. Its L1 can then keep old values until they expire, so use a TTL you can accept as the worst case for stale reads from L1.
- Each store uses its own channel, named after the store. Two applications that share a database and use the same store name receive each other's messages, and delete each other's L1 entries with the same keys. Give them different store names.
- If you connect through PgDog, it forwards notifications through a buffer of `pub_sub_channel_size` entries and drops them, without an error, for a client that falls more than that many behind. The load to size for is notifications per second times the number of app instances behind one PgDog. Merging keeps that number low, but check PgDog's `SHOW LISTENERS` for a non-zero `dropped` column under your peak load.

#### Heartbeat

The listening connection only receives, so it can't always tell when it stops working:

- A pooler in transaction mode accepts `LISTEN` but never delivers anything.
- A load balancer, NAT gateway or firewall can drop an idle connection without telling either end.
- If the database host goes away or the network is cut, nothing tells the client until it sends something.

In these cases the connection stays open while notifications are lost, and the instance's L1 keeps old values until they expire. So every `interval` (30 s by default), the driver sends a notification to itself. If one isn't received within `timeout` (15 s by default), the driver replaces the listening connection and reports an error. A silent failure is detected within `interval` + `timeout` + `timeout` / 3, about 50 s with the defaults.

The heartbeat also keeps both connections active, so a proxy that closes idle connections after longer than `interval` leaves them open.

Each heartbeat is a small publish transaction. While messages are being published, it goes in one of their transactions. With no traffic, each store in each process sends one every `interval`, which is 2,880 transactions a day at the default. Every one of them wakes every listening session in the database, so with 100 processes the database handles about 3 heartbeat transactions and 330 wakeups per second. With many processes, a longer `interval` lowers this.

##### If the heartbeat fails

The error `Postgres bus listener did not receive a heartbeat` means that a heartbeat was committed, but the listening connection didn't receive it in time. The driver replaces the connection on its own. Once, for example during a failover, it needs no action. If it repeats, check for:

- **A pooler in transaction mode.** `LISTEN` appears to work, but nothing is delivered. Use a direct or session-mode connection, see [What the connection must support](#what-the-connection-must-support).
- **Connections that reach different servers.** Notifications are delivered only within one server. Both connections must reach the same primary, which a proxy that spreads new connections over several nodes doesn't guarantee.
- **Something closing idle connections.** A proxy, load balancer or firewall with an idle timeout shorter than `heartbeat.interval`. Lower the interval below that timeout, or raise the timeout.
- **An unstable network.** The driver recovers each time, but messages are lost for that instance while it reconnects.
- **An overloaded process.** If the process can't read from its connection for longer than `timeout`, the heartbeat arrives too late. Time the process was completely frozen, such as a long garbage collection, is not counted. Raise `timeout` if your process is often busy for that long.

##### Turning the heartbeat off

`heartbeat: false` is safe only if both of these hold:

- The app connects to Postgres directly, with no pooler, proxy, load balancer or NAT between them, for example on the same host.
- You accept that, if a connection silently stops working, the instance's L1 may serve old values until they expire, with no error. A short L1 TTL limits how long.

Without the heartbeat, the driver still reconnects whenever a connection reports an error or is closed. That covers server restarts, terminated sessions and most failovers. A connection that is silently dropped is found only by TCP keepalive, which the driver turns on, but which with operating system defaults can take hours. The `pg` option `keepAliveInitialDelayMillis` shortens it.

#### Effect on the rest of your database

The bus shares the database with your application. It changes nothing outside its own sessions, but its transactions cost the whole server something:

- **Connections:** each store with a Postgres bus uses two connections per process. Count them when you size `max_connections` or your pooler.
- **Transaction ids:** every publish transaction uses a transaction id and writes a small WAL record that is replicated. At 1,000 transactions per second that is 86.4 million ids a day, so anti-wraparound vacuums of your tables come sooner (they start at `autovacuum_freeze_max_age`, 200 million by default). A `batchDelay` lowers the count. With no traffic, the [heartbeat](#heartbeat) still uses one every `interval` per store and process, so the database is never fully idle.
- **Notification lock:** Postgres runs commits that send notifications one at a time, using a lock shared by all databases on the server. Your other queries and transactions don't take it, but any other application on the same server that uses `NOTIFY` competes with the bus for it. The driver turns off `synchronous_commit` for its transactions to hold the lock briefly.
- **Notification queue:** notifications wait in a queue shared by all databases on the server (8 GB by default) until every listening session has read them. A listening session that stops reading, such as a frozen process or a session left inside a long transaction, holds the queue back. If it fills up, every `NOTIFY` on the server fails at commit. The driver's listener never opens a transaction, and the heartbeat replaces a listener that stops receiving while its process still runs.
- **Other listeners:** each commit wakes the sessions that listen in the same database, including your application's own listeners on other channels.
- **Session settings:** the driver changes only its own sessions. It sets `synchronous_commit = off` inside each publish transaction, and turns off `idle_session_timeout` (Postgres 14 and later) on its listening connection, since that connection is idle by design. It does so even when no timeout is set, so a timeout enabled later by a config reload doesn't close the listener. It uses a plain `SET`, which poolers such as PgDog keep to that client. It doesn't change `pg` defaults, type parsers or the config object you pass.
2 changes: 1 addition & 1 deletion packages/bentocache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For those looking to go further, you can use the two-levels caching system. Here

- **L1: Local Cache**: First level cache. Data is stored in memory with an LRU algorithm for quick access
- **L2: Distributed Cache**: If the data is not in the in-memory cache, it is searched in the distributed cache (Redis, for example)
- **Synchronization via Bus**: In a multi-instance context, you can synchronize different local in-memory caches of your instances via a Bus like Redis or RabbitMQ. This method maintains cache integrity across multiple instances
- **Synchronization via Bus**: In a multi-instance context, you can synchronize different local in-memory caches of your instances via a Bus like Redis or Postgres. This method maintains cache integrity across multiple instances

Here is a simplified diagram of the flow :

Expand Down
12 changes: 11 additions & 1 deletion packages/bentocache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"caching",
"in-memory",
"multi-tier",
"postgres",
"redis",
"tiered"
],
Expand Down Expand Up @@ -39,6 +40,7 @@
"exports": {
".": "./build/index.js",
"./drivers/redis": "./build/src/drivers/redis.js",
"./drivers/postgres": "./build/src/drivers/postgres.js",
"./drivers/memory": "./build/src/drivers/memory.js",
"./drivers/file": "./build/src/drivers/file/file.js",
"./drivers/dynamodb": "./build/src/drivers/dynamodb.js",
Expand Down Expand Up @@ -99,10 +101,12 @@
},
"peerDependencies": {
"@aws-sdk/client-dynamodb": "^3.438.0",
"@types/pg": "^8.0.0",
"ioredis": "^5.3.2 || ^6.0.0",
"knex": "^3.0.1",
"kysely": "^0.27.3",
"orchid-orm": "^1.24.0"
"orchid-orm": "^1.24.0",
"pg": "^8.0.3"
},
"peerDependenciesMeta": {
"@aws-sdk/client-dynamodb": {
Expand All @@ -119,6 +123,12 @@
},
"orchid-orm": {
"optional": true
},
"pg": {
"optional": true
},
"@types/pg": {
"optional": true
}
},
"c8": {
Expand Down
58 changes: 58 additions & 0 deletions packages/bentocache/src/bus/transports/postgres/channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createHash } from 'node:crypto'

/**
* Postgres truncates identifiers to 63 bytes (NAMEDATALEN - 1) and
* `pg_notify()` rejects longer channel names.
*/
export const MAX_CHANNEL_BYTES = 63

const HASH_HEX_LENGTH = 16
const MAX_PREFIX_LENGTH = MAX_CHANNEL_BYTES - HASH_HEX_LENGTH - 1

/**
* Printable ASCII. Anything else may not exist in the database encoding:
* a LATIN1 database rejects a channel name with Cyrillic or CJK
* characters, for example.
*/
const PRINTABLE_ASCII = /^[\x20-\x7e]*$/

/**
* Map a bus channel name to a Postgres channel name, which is always
* printable ASCII of at most 63 bytes whatever the database encoding is.
*
* Non-empty printable ASCII names up to 63 bytes are used as is. Any other name is
* turned into a prefix, where each character outside printable ASCII is
* replaced by `_`, cut to 46 characters and suffixed with `~` and the
* first 16 hex characters of the sha256 of the full name in UTF-8. The
* hash keeps names that share a prefix distinct.
*
* Every instance must compute the same name for the same channel, so this
* mapping is part of the wire format and must not change.
*/
export function toPostgresChannel(name: string): string {
/**
* Postgres rejects an empty channel name, so it is hashed like the others
*/
if (name.length > 0 && name.length <= MAX_CHANNEL_BYTES && PRINTABLE_ASCII.test(name)) {
return name
}

const hash = createHash('sha256').update(name, 'utf8').digest('hex').slice(0, HASH_HEX_LENGTH)

let prefix = ''
for (const char of name) {
if (prefix.length === MAX_PREFIX_LENGTH) break
prefix += PRINTABLE_ASCII.test(char) ? char : '_'
}

return `${prefix}~${hash}`
}

/**
* Quote a channel name for `LISTEN` / `UNLISTEN`, which don't accept
* bind parameters. Quoting also keeps the name case-sensitive, which
* matches how `pg_notify()` treats its channel argument.
*/
export function quoteChannel(channel: string): string {
return `"${channel.replace(/"/g, '""')}"`
}
Loading
Loading