mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
feat(search): keep an OpenSearch message index in sync through a Postgres outbox (#9838)
## Summary
This PR keeps a second copy of the message index in OpenSearch. Searches
still run on Postgres. Each searchable message writes a small "index me"
row to a new Postgres table, `search_outbox`, in the same transaction
that stores it. One relay drains that queue into OpenSearch. Writes
never wait on OpenSearch, and if it is down, rows wait in the queue.
```mermaid
sequenceDiagram
participant Client
participant Relay as Relay (admission tx)
participant PG as Postgres
participant Idx as Indexer (1 leader)
participant OS as OpenSearch
Client->>Relay: kind 9 "flamingo"
Relay->>PG: INSERT events + INSERT search_outbox (same tx)
loop every 5s, backing off up to 60s on failure
Idx->>PG: claim ≤500 rows of one community (SKIP LOCKED)
Idx->>PG: re-read those events (+ channel, thread, mentions)
Idx->>OS: one bulk request: upsert docs, delete missing ones
Idx->>PG: DELETE the claimed rows, COMMIT
end
```
- **Turn it on** with `BUZZ_OPENSEARCH_URL` plus
`BUZZ_OPENSEARCH_PREVIEW=true` (optional basic auth: set both or
neither). Without the opt-in, a relay with the URL refuses to start. The
relay refuses to start if migration `0106` has not run. Without the
setting, each insert pays one flag check.
- **Queue** (`migrations/0106`, `search_outbox.rs`): one row per stored
message of kind 9, 40002, 40008, 45001 or 45003. A rolled-back write
takes its row with it. The indexer re-reads each event, so duplicate
rows are harmless.
- **Indexer** (`indexer.rs`): one leader, chosen with a Postgres session
advisory lock. A batch holds its community's write transaction until the
bulk request ends, so deletion can't fence the community mid-batch. HTTP
400/413 skips that community for the pass. Any other failure ends the
pass and backs off. Batches have no byte cap yet, so a cluster with a 10
MiB request limit can reject a full batch; #9862 caps requests at 8 MiB.
- **Index** (`opensearch.rs`): alias `buzz-messages` →
`buzz-messages-v1`, strict mapping, message text indexed but not stored.
Writes require the alias, so they never create an index. Searches filter
on community first and fail rather than return partial hits.
- **Deletion:** community deletion fences and purges the community's
queued `search_outbox` rows with its other rows. It doesn't touch
documents already in the index.
- **Infra:** an opt-in `opensearch` service in docker-compose
(`--profile search`). The CI Postgres job starts OpenSearch 2.19.3. The
−1,895 lines move the Postgres search tests into `tests/search_suite/`,
which now runs against both backends.
### Worth knowing
- **A rejected batch blocks its community.** Rows are claimed oldest
first, so the same batch comes back every pass. Other communities still
drain, but each rejection counts as a failure, so passes slow to 60 s
apart.
- **Text isn't stored in the index,** so a new mapping version needs a
full backfill from Postgres. No backfill exists yet, so messages stored
before the setting aren't indexed.
- **Not for relays with real data yet.** Deletes, edits and account
scrubs don't reach the index until #9862, and nothing purges a deleted
community's documents. Searches never return them, since every hit is
re-read from Postgres, but their words stay in the index. So the relay
requires the `BUZZ_OPENSEARCH_PREVIEW` opt-in, which #9862 removes.
- **Coverage gaps:** each relay queues only what it stores, so set the
URL on every relay.
- **Delay:** up to ~5 s of polling plus a 30 s refresh before a message
is searchable.
### Testing
CI's Postgres job now runs the shared search suite and the outbox sync
tests against a real OpenSearch. For a local run, see
`docs/opensearch-search-deployment.md`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
54
docs/opensearch-search-deployment.md
Normal file
54
docs/opensearch-search-deployment.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# OpenSearch message index deployment
|
||||
|
||||
With `BUZZ_OPENSEARCH_URL` set, the relay keeps an OpenSearch index of
|
||||
messages up to date through the `search_outbox` table. Searches still run on
|
||||
Postgres.
|
||||
|
||||
| Variable | Meaning |
|
||||
| --- | --- |
|
||||
| `BUZZ_OPENSEARCH_URL` | HTTP(S) cluster URL without credentials, query or fragment. Unset turns indexing off. |
|
||||
| `BUZZ_OPENSEARCH_USERNAME`, `BUZZ_OPENSEARCH_PASSWORD` | Optional basic auth. Set both or neither. |
|
||||
| `BUZZ_OPENSEARCH_PREVIEW` | Required with the URL until [block/buzz#9862](https://github.com/block/buzz/pull/9862), because deleted messages stay in the index. Use it only on relays without real data. |
|
||||
|
||||
## Rollout order
|
||||
|
||||
1. Deploy a release that lists `search_outbox` as a pending deletion-scoped
|
||||
table ([block/buzz#9871](https://github.com/block/buzz/pull/9871)).
|
||||
Deletion code that predates it rejects the catalog once migration `0106`
|
||||
runs, which stops community deletion.
|
||||
2. Deploy the release with migration `0106`. Nothing is queued until the
|
||||
setting is on.
|
||||
3. Set `BUZZ_OPENSEARCH_URL` and `BUZZ_OPENSEARCH_PREVIEW=true` on every
|
||||
relay. Each relay queues only the messages it stores, so one without the
|
||||
settings leaves gaps. A relay with them refuses to start before `0106` has
|
||||
run. Until [block/buzz#9862](https://github.com/block/buzz/pull/9862)
|
||||
caps bulk requests, a cluster with a 10 MiB request limit can reject a
|
||||
full batch.
|
||||
|
||||
Messages stored before step 3 are not indexed; there is no backfill yet.
|
||||
|
||||
To turn indexing off, unset the URL and restart the relays. Queued rows stay
|
||||
until it is back on.
|
||||
|
||||
## Operating
|
||||
|
||||
While OpenSearch is down the queue grows without bound. Check it with:
|
||||
|
||||
```sh
|
||||
psql -c 'SELECT count(*), min(created_at) FROM search_outbox'
|
||||
```
|
||||
|
||||
The index holds the words of every indexed message, DMs included, so anyone
|
||||
who can query the cluster can search them. Limit cluster access to the relays.
|
||||
|
||||
If you stream Postgres changes through logical replication, leave
|
||||
`search_outbox` out of the publication. Its rows are short-lived queue
|
||||
entries.
|
||||
|
||||
## Local run
|
||||
|
||||
```sh
|
||||
docker compose --profile search up -d opensearch
|
||||
BUZZ_OPENSEARCH_URL=http://localhost:9200 BUZZ_OPENSEARCH_PREVIEW=true just relay
|
||||
curl -s 'localhost:9200/buzz-messages/_search?pretty'
|
||||
```
|
||||
Reference in New Issue
Block a user