--- 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. 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. ## 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.