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

Elixir and Phoenix
Realtime is written in Elixir, which compiles to Erlang, and uses many tools the Phoenix Framework 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, which use Phoenix.PubSub with the default Phoenix.PubSub.PG2 adapter.
The PG2 adapter uses Erlang process groups 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 by default to protect your data.
Streaming the Write-Ahead Log
Realtime streams database changes in the following order:
- Realtime acquires a Postgres logical replication slot when it connects to your database.
- It polls the replication slot and appends channel subscription IDs to each WAL record.
- 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: send messages between clients.
- Presence: share user state across clients.
- Postgres Changes: listen to database changes.
- Realtime Authorization: control who can send and receive messages.