Files
supabase/apps/docs/content/guides/realtime/architecture.mdx
Miranda Limonczenko 4e15414929 docs(guides): rework five more explainer pages to the explainer template (#53757)
## Problem

Five more concept pages are close to the explainer template from #53751.
They have short or missing descriptions, generic or question headings, a
pointer-only heading, and no Related resources. Several also have
filler, marketing language, or time phrases.

Closes DOCS-1617. This is a second batch on top of #53755.

## Solution

The PR is one squashed commit. It covers a style pass, the restructure
to the template, critic-pass fixes, tables and lists, the opener change,
content moved in from other pages, and two headings renamed to `X vs Y`.

### Structure

| Page | Change |
| --- | --- |
| `ai/keyword-search` | Value statement, `Using full-text search` (a
pointer-only heading) and `See also` folded into Related resources |
| `ai/vector-indexes` | Value statement, a `## Limits` section for the
dimension limits and version check, `Resources` to Related resources |
| `storage/buckets/fundamentals` | Retitled `Storage buckets`, value
statement, an access model table, the bucket types promoted to H2,
`Example use cases` headings to lead-in sentences, Related resources |
| `storage/analytics/introduction` | Retitled `Analytics buckets`, value
statement, question and generic headings made descriptive, Related
resources |
| `realtime/architecture` | Retitled `Realtime architecture`, value
statement, `Elixir & Phoenix` to `Elixir and Phoenix`, Related resources
|

- **Linked headings**: headings that other pages link to keep their
text: `#distance-operators`, `#public-buckets`, `#private-buckets`, and
`#access-model`. Moving a bucket type from H3 to H2 keeps its slug.
- **Openers and tables**: only the value statement and a cross-reference
come before the first heading, and each concept opener moves into the
first H2, per the template change in #53751. Prose comparisons and
parallel facts become tables and lists. On Realtime architecture, the
WAL streaming steps are a numbered list.
- **Moved content**: each explainer now owns its concept, and the page
that held it links back. Nothing is newly written; every added fact
comes from an existing page:
- Keyword search: `## How keyword search works`, summarized from the
Usage section of [Full text
search](/docs/guides/database/full-text-search#usage), which links back.
- Vector indexes: `## Trade-offs` and `## When to use each index type`,
from [HNSW indexes](/docs/guides/ai/vector-indexes/hnsw-indexes) and
[IVFFlat indexes](/docs/guides/ai/vector-indexes/ivf-indexes). Both now
link back, and their copies of the index list and dimension limits are
replaced with links.
- Analytics buckets: `## Services`, moved from the Architecture overview
in [Iceberg
Catalog](/docs/guides/storage/analytics/connecting-to-analytics-bucket),
and `## Limits`, from [Create an analytics
bucket](/docs/guides/storage/analytics/creating-analytics-buckets) and
[Analytics buckets limits](/docs/guides/storage/analytics/limits).
- **Facts**: unchanged. The value statements, the access model table,
and Related resources are the only new text, and the table summarizes
facts already on the page.

### Style

- **Descriptions**: add or rewrite one per page, 150 to 160 characters.
- **Alt text**: rewrite the Realtime diagram's alt text against the
image. It was `Architecture`.
- **Terms**: `WORD_LIST.md` passes for numbers, _will_, _since_, _allows
you to_, _performant_, _here_ as link text, filler, and marketing
language such as "perfect for" and "an amazing Supabase user".
- **Critic pass**: shorter sentences, repeated points and idioms cut,
fewer repeated links, lowercase SQL, code-formatted type names, and
external links that name their destination.

### Structure numbers

These numbers are from before the opener and table changes.

| Page | Chars before → after | Headings | Generic headings |
Description chars |
| --- | --- | --- | --- | --- |
| `ai/keyword-search` | 2,611 → 2,544 | 3 → 2 | 1 → 0 | 40 → 156 |
| `ai/vector-indexes` | 2,557 → 2,890 | 3 → 4 | 1 → 0 | 28 → 153 |
| `storage/buckets/fundamentals` | 2,128 → 3,189 | 5 → 4 | 2 → 0 | 41 →
156 |
| `storage/analytics/introduction` | 1,698 → 2,332 | 2 → 3 | 0 → 0 | 0 →
158 |
| `realtime/architecture` | 4,474 → 5,185 | 6 → 7 | 0 → 0 | 45 → 153 |

### How the template did

- **New learnings, added to #53751**: an optional `## When to use
<concept>` section, because the template's own reader question asks
"when do I use it?". One H2 per Process stage, for
`realtime/architecture`. More than one Parts section, because
`ai/vector-indexes` has index types and distance operators.
- **Fit well**: `storage/buckets/fundamentals` is a parts table followed
by one H2 per part, which is the rule from the first batch.
- **Still loose**: `realtime/architecture` maps to the template only as
a run of Process stages. A future rework could group them, or split
`Broadcast from Postgres` into its own page.
`storage/analytics/introduction` keeps a descriptive comparison heading
instead of `## Trade-offs`.

Skipped on purpose: the Realtime PNG diagram isn't converted to Mermaid,
because that's new content.

### Preview

| Page | Live | Preview |
| --- | --- | --- |
| Keyword search |
[Live](https://supabase.com/docs/guides/ai/keyword-search) |
[Preview](https://docs-git-docs-explainer-rework-batch-2-supabase.vercel.app/docs/guides/ai/keyword-search)
|
| Vector indexes |
[Live](https://supabase.com/docs/guides/ai/vector-indexes) |
[Preview](https://docs-git-docs-explainer-rework-batch-2-supabase.vercel.app/docs/guides/ai/vector-indexes)
|
| Storage buckets |
[Live](https://supabase.com/docs/guides/storage/buckets/fundamentals) |
[Preview](https://docs-git-docs-explainer-rework-batch-2-supabase.vercel.app/docs/guides/storage/buckets/fundamentals)
|
| Analytics buckets |
[Live](https://supabase.com/docs/guides/storage/analytics/introduction)
|
[Preview](https://docs-git-docs-explainer-rework-batch-2-supabase.vercel.app/docs/guides/storage/analytics/introduction)
|
| Realtime architecture |
[Live](https://supabase.com/docs/guides/realtime/architecture) |
[Preview](https://docs-git-docs-explainer-rework-batch-2-supabase.vercel.app/docs/guides/realtime/architecture)
|

## Review instructions

1. Open each page's live and preview links from the Preview table side
by side.
2. Check that each page opens with a value statement and ends with
Related resources.
3. Open `/docs/guides/ai/vector-indexes#distance-operators` and
`/docs/guides/storage/buckets/fundamentals#public-buckets` on the
preview, and check that each jumps to its heading.
4. On Storage buckets, check that the access model table matches the
Private buckets and Public buckets sections.

## Checklist

Check all before review:

- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-23 16:23:21 -08:00

90 lines
5.7 KiB
Plaintext

---
id: 'architecture'
title: 'Realtime architecture'
description: 'Learn how Supabase Realtime runs as a global Elixir cluster, how Broadcast and Presence reach clients in any region, and how it streams database changes.'
sidebar_label: 'Architecture'
---
Knowing how the Realtime cluster routes messages tells you how messages and database changes reach your clients. For how to send messages between clients, see [Broadcast](/docs/guides/realtime/broadcast).
## How the Realtime cluster works
Realtime is a globally distributed Elixir cluster. Clients can connect to any node in the cluster via WebSockets and send messages to any other client connected to the cluster.
<Image
alt="Diagram of the Realtime global cluster. A user in Region A connects to a Realtime node in Region A, which connects to the tenant database. A user in Region B connects to a Realtime node in Region B. The two nodes connect to each other."
src={{
light: '/docs/img/guides/platform/realtime/architecture--light.png',
dark: '/docs/img/guides/platform/realtime/architecture--dark.png',
}}
width={1990}
height={2226}
/>
## Elixir and Phoenix
Realtime is written in [Elixir](https://elixir-lang.org/), which compiles to [Erlang](https://www.erlang.org/), and uses many tools the [Phoenix Framework](https://www.phoenixframework.org/) provides.
Client-facing WebSocket servers need to handle many concurrent connections. Phoenix can handle millions of them, because Elixir provides lightweight processes (not OS processes) to work with.
## Channels
Channels are implemented using [Phoenix Channels](https://phoenix.hexdocs.pm/channels.html), which use [Phoenix.PubSub](https://phoenix-pubsub.hexdocs.pm/Phoenix.PubSub.html) with the default `Phoenix.PubSub.PG2` adapter.
The PG2 adapter uses Erlang [process groups](https://www.erlang.org/docs/18/man/pg2.html) to implement the PubSub model where a publisher can send messages to many subscribers.
## Global cluster
Two features reach clients in any region through the cluster:
| Feature | What it does |
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Presence | An in-memory key-value store backed by a conflict-free replicated data type (CRDT). When a user is connected to the cluster, the state of that user is sent to all connected Realtime nodes. |
| Broadcast | Sends a message from any connected client to a Channel. Any other client connected to that same Channel receives that message. |
Both work globally. A client connected to a Realtime node in the United States can send a message to another client connected to a node in Singapore. Connect two clients to the same Realtime Channel and they both receive the same messages.
Broadcast is useful for getting messages to users in the same location rapidly. If a group of clients are connected to a node in Singapore, the message only needs to go to that Realtime node in Singapore and back down. If users are close to a Realtime node, they get Broadcast messages in the time it takes to ping the cluster.
With the Realtime cluster, you don't have to think about which regions your clients are connected to.
If you're using Broadcast, Presence, or streaming database changes, messages always reach your users by the shortest path possible.
## Connecting to a database
Realtime lets you listen to changes from your Postgres database. When a new client connects to Realtime and initializes the `postgres_changes` Realtime Extension, the cluster connects to your Postgres database and starts streaming changes from a replication slot.
To keep the stream close and available:
- Realtime knows the region your database is in, and connects to it from the closest region possible.
- Every Realtime region has at least two nodes, so if one node goes offline, the other node should reconnect and start streaming changes again.
## Broadcast from Postgres
Realtime Broadcast sends messages when changes happen in your database. Realtime creates a publication on the `realtime.messages` table. It then reads the Write-Ahead Log (WAL) file for this table, and sends a message whenever an insert happens. Messages are sent as JSON packages over WebSockets.
The `realtime.messages` table is partitioned by day:
- Partitions are retained for three days before being deleted.
- Realtime deletes old messages efficiently by dropping old partitions.
Broadcast uses [Realtime Authorization](/docs/guides/realtime/authorization) by default to protect your data.
## Streaming the Write-Ahead Log
Realtime streams database changes in the following order:
1. Realtime acquires a Postgres logical replication slot when it connects to your database.
2. It polls the replication slot and appends channel subscription IDs to each WAL record.
3. After receiving the results of the polling query, it delivers the records to the clients those subscription IDs represent.
Subscription IDs are Erlang processes representing underlying sockets on the cluster. These IDs are globally unique, and the Erlang virtual machine routes messages to processes automatically.
## Related resources
- [Broadcast](/docs/guides/realtime/broadcast): send messages between clients.
- [Presence](/docs/guides/realtime/presence): share user state across clients.
- [Postgres Changes](/docs/guides/realtime/postgres-changes): listen to database changes.
- [Realtime Authorization](/docs/guides/realtime/authorization): control who can send and receive messages.