Files
buzz/docs/opensearch-search-deployment.md
Salman Mohammed 6b0dfc1957 feat(search): sign OpenSearch requests with AWS SigV4 (#9947)
## Summary

The relay's OpenSearch client could only use HTTP basic auth. Amazon
OpenSearch Serverless accepts only requests signed with AWS SigV4, and
managed Amazon OpenSearch Service domains can require them, so the relay
could not index into either. This PR adds optional SigV4 signing. It is
off by default; basic auth and unauthenticated clusters work as before.

| Variable | Meaning |
| --- | --- |
| `BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE` | Turns signing on and names the
signing service: `aoss` for a Serverless collection, `es` for a managed
domain. Unset means no signing. |
| `BUZZ_OPENSEARCH_AWS_REGION` | Region to sign for. Defaults to
`AWS_REGION`, then `AWS_DEFAULT_REGION`. |

The relay refuses to start when signing is set without a region,
together with basic auth, without `BUZZ_OPENSEARCH_URL`, or with a
service other than `es` or `aoss`.

- **Credentials** come from the AWS default chain: environment
variables, the shared profile, a web identity token, then the container
or instance role. No keys are added to the relay's own settings.
- **Refresh** (`aws_credentials.rs`): the `opensearch` crate asks its
credentials provider on every request, and the AWS default chain does
not cache. Used directly, every search and bulk write would call the
token service or the instance metadata endpoint. The relay keeps one set
of credentials and reloads it five minutes before it expires (every 15
minutes for credentials without an expiry). Each load builds the AWS
provider anew, because a kept one never rereads the shared profile; a
key rotated there is picked up at the next load. Concurrent requests
share one load, whether it succeeds or fails: the next load starts at
least 30 s after the last one ended, and requests in between use the
valid set or fail at once. A first load is bounded at 30 s. A reload
while the current set is still valid is bounded at 5 s, and if it fails
the current set stays in use.
- **Logging:** a failed credential load logs only the kind of error,
because the causes can carry what a `credential_process` helper printed.
The AWS SDK logs the full cause itself under `aws_config` at warn level,
which the relay's default log filter leaves out; the deployment doc says
how to turn it on and what it can contain. Credentials are never logged,
and the config's `Debug` output shows only the service and region.
- **Dependencies:** enables the `opensearch` crate's `aws-auth` feature
and adds `aws-config` for the credential chain.

### Worth knowing

- The region is not read from the shared AWS profile file, only from the
variables above.
- Right after a Serverless collection became active, the first signed
request got HTTP 401 once; the same run passed 45 s later. The indexer
already retries such failures.
- A Serverless collection does not support `_refresh`, which only the
tests' `SearchIndex::refresh` uses.

### Related issue

N/A. Closest existing work: #9838 (the OpenSearch index this client
writes to) and #9862. No duplicate found.

### Testing

- Unit tests: startup validation of the new settings; credentials are
loaded once, survive a failed reload, a failed first load is not
repeated per request, requests waiting on a stalled load share its one
failure, and a key rotated in a shared profile file is picked up.
- Local, unauthenticated OpenSearch 2.19.3: the
`postgres_opensearch_search` suite, plus index create, bulk write,
search, bulk delete and index delete through `SearchIndex`.
- Verified against an Amazon OpenSearch Serverless collection through
`SearchIndex` with `aoss` signing and credentials from a shared profile:
alias check, index create, bulk write of documents with
`community:event_id` ids, search (3 hits), bulk delete of one document,
search (2 hits), index delete. On the same collection an unsigned client
got HTTP 403 for search and bulk write, and a client signing for `es`
got HTTP 403.
- The relay binary refuses to start with signing and no region, and with
signing plus basic auth.
- Not run: a managed domain (`es` signing), and the web identity,
container and instance role credential sources.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 22:12:07 +00:00

6.9 KiB

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, and backfills the messages stored before it was set. 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_AWS_SIGV4_SERVICE Optional AWS SigV4 request signing, for Amazon OpenSearch Service: es for a managed domain, aoss for a Serverless collection. Unset turns signing off. Not combined with basic auth.
BUZZ_OPENSEARCH_AWS_REGION Region to sign for. Defaults to AWS_REGION, then AWS_DEFAULT_REGION. Signing needs one of them.
BUZZ_SEARCH_INDEXER_WORKERS Indexer workers on the relay that holds the lease: 1 to 32, default 1. Each holds one connection from BUZZ_DB_POOL_SIZE for the length of a bulk request, so leave the pool room for them.
BUZZ_OPENSEARCH_PREVIEW Required with the URL for now, because deleting a community leaves its messages in the index. Use it only on relays without real data.

AWS request signing

With signing on, the relay takes credentials from the standard AWS chain: environment variables, the shared profile, a web identity token, then the container or instance role. It reloads them before they expire, and static keys every 15 minutes, so role credentials and a key changed in the shared profile are picked up without a restart. The identity needs HTTP access to the domain or, for Serverless, a data access policy on the collection and its buzz-* indexes plus aoss:APIAccessAll.

When a load fails, the relay logs only the kind of error. For the cause, add aws_config=warn to RUST_LOG. That output can include what a credential_process helper printed, so make sure the helper prints no secrets. A helper should also bound its own run time: the relay gives up on a load that takes too long but does not stop a helper that hangs.

Rollout order

  1. Deploy a release that lists search_outbox and search_backfill as pending deletion-scoped tables (block/buzz#9871). Deletion code that predates it rejects the catalog once migration 0106 runs, which stops community deletion.
  2. Deploy the release with migrations 0106 to 0108. Nothing is queued until the setting is on.
  3. Set BUZZ_OPENSEARCH_URL and BUZZ_OPENSEARCH_PREVIEW=true on every relay in one deploy. A relay queues only its own writes, so one without the settings leaves the index without the messages it stores and with the ones it deletes. A relay with them refuses to start before the migrations have run. The backfill starts with the first relay to hold the lease.

If a relay without the settings ran after the backfill started, wait until every relay has them, then delete the index while searches run on Postgres. The relay clears the backfill progress, creates the index again, waits 30 seconds before its first write, and backfills every community. It does the same whenever the index goes missing. Running DELETE FROM search_backfill without deleting the index is not enough: the backfill reads only live messages, so it would leave the documents of messages deleted in the meantime. Deleting one community's row backfills that community again.

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:

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 and search_backfill out of the publication. Their rows are short-lived queue entries and progress markers.

After a point-in-time restore, or a cut-over to a cluster built by logical replication (which copies no sequence values), the version sequence can be far below the versions the index holds. The index then refuses changes to the messages it holds: buzz_search_index_version_conflicts_total rises and their rows stay in search_outbox. Rebuild the index (above), or move the sequence past any version handed out before. The indexer takes far fewer than a million values a second, so a value tied to the clock works after every restore or cut-over:

psql -c "SELECT setval('search_index_version', GREATEST((SELECT last_value FROM search_index_version) + 1000000000000, 10000000000000000 + (EXTRACT(EPOCH FROM now())::bigint * 1000000)))"

What to watch

Metric Act when
buzz_search_index_rejected_total It rises steadily: the cluster refuses those documents for good, and their rows stay queued.
buzz_search_index_version_conflicts_total It is not zero: the version sequence handed out values again (below).
buzz_search_backfill_complete 1 means every stored message of the community is queued, not yet indexed. Move searches to OpenSearch once it is 1 for every community and search_outbox has emptied.

If buzz_search_index_rejected_total rises, investigate the rejection and fix the cause. The relay retries rejected rows automatically. A sustained spike can block newer messages in the affected community and pause backfill across communities. After the fix, verify that the queue drains.

Known gaps

  • Deleted communities. Deleting a community does not remove its documents from the index. No search can return them, but their words stay in the index.
  • Postgres crash or failover. The version sequence can hand out values again after a Postgres crash or the promotion of an asynchronous replica. Rows then stay queued until the sequence passes the versions the index holds. If a bulk request sent before the crash also lands after a newer write of the same message, the index keeps that message's older text until it changes again.
  • Writes applied late. OpenSearch forgets a delete's version after 60 seconds (gc_deletes), so an older write that lands later, such as a previous lease holder's bulk request, brings the deleted document back.
  • Removed text on disk. A removed document can no longer be found, but its text stays in the index's files until OpenSearch merges them. The relay does not force that merge.

In the two cases before this one, a search can match text a message no longer has. Every hit is re-read from Postgres before it is returned, so that text is never shown.

Local run

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'