# electric-sql/electric architecture

> Architecture diagram of the GitHub repository electric-sql/electric, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/electric-sql/electric
- Repository: https://github.com/electric-sql/electric

## Overview

#### Repository scope and entry points
Electric is a monorepo for three related data primitives: Postgres read-path sync, durable streams, and durable AI-agent sessions. The primary sync service is started as an Elixir/OTP application; its public Shape HTTP API is specified at `website/electric-api.yaml`. Key entry points are `packages/sync-service/lib/electric/application.ex`, `packages/sync-service/lib/electric/plug/router.ex`, `packages/durable-streams-rust/src/main.rs`, and `packages/agents-server/src/entrypoint.ts`.

#### Postgres Sync Service
The core product is an Elixir service that connects to Postgres logical replication, derives filtered partial-replication “Shapes,” and serves initial snapshots plus ongoing changes over HTTP/SSE. OTP supervision and stack lifecycle are rooted in `packages/sync-service/lib/electric/application.ex` and `packages/sync-service/lib/electric/stack_supervisor.ex`; request handling is separated into router and shape-serving plugs at `packages/sync-service/lib/electric/plug/router.ex` and `packages/sync-service/lib/electric/plug/serve_shape_plug.ex`.

#### Replication-to-Shape pipeline
Postgres connection management and replication decoding form the ingestion boundary: `packages/sync-service/lib/electric/connection/manager.ex`, `packages/sync-service/lib/electric/postgres/replication_client.ex`, and `packages/sync-service/lib/electric/postgres/logical_replication/decoder.ex`. Transactions are converted into changes, evaluated against Shape predicates, routed to consumers, and materialized through `packages/sync-service/lib/electric/replication/transaction_builder.ex`, `packages/sync-service/lib/electric/shapes/event_router.ex`, and `packages/sync-service/lib/electric/shapes/consumer/materializer.ex`. Shape logs/cache storage decouple database replication from client delivery in `packages/sync-service/lib/electric/shape_cache.ex` and `packages/sync-service/lib/electric/shape_cache/pure_file_storage.ex`.

#### Client consumption and integrations
TypeScript and Elixir clients implement the Shape-stream protocol, snapshot tracking, parsing, and reconnection semantics: `packages/typescript-client/src/client.ts`, `packages/typescript-client/src/shape-stream-state.ts`, and `packages/elixir-client/lib/electric/client.ex`. React-facing consumption is provided by `packages/react-hooks/src/react-hooks.tsx`. Examples under `examples/` demonstrate browser, server, mobile, Phoenix, Redis, encryption, auth-proxy, and local-first write architectures rather than constituting one deployable application.

#### Durable Streams
A separate Rust service provides durable append/read streams, HTTP/SSE handling, persistence tiers, WAL recovery, blob storage, and telemetry. Its executable and request layers are `packages/durable-streams-rust/src/main.rs`, `packages/durable-streams-rust/src/api.rs`, and `packages/durable-streams-rust/src/handlers.rs`; durable storage is centered on `packages/durable-streams-rust/src/store.rs` and `packages/durable-streams-rust/src/wal/mod.rs`. This is independently containerized at `packages/durable-streams-rust/Dockerfile`.

#### Agents platform
The agents stack persists entity/session state in durable streams and runs model/tool loops against that state. `packages/agents-runtime/src/index.ts` exposes the runtime SDK; context construction, model execution, and tool providers live in `packages/agents-runtime/src/context-factory.ts`, `packages/agents-runtime/src/model-runner.ts`, and `packages/agents-runtime/src/tool-providers.ts`. The Node server provides tenancy, principals/permissions, entity projection, routing, scheduling, bridges, and sandbox choices through `packages/agents-server/src/server.ts`, `packages/agents-server/src/entity-manager.ts`, and `packages/agents-server/src/routing/oss-server-router.ts`. Built-in Horton/worker entities are assembled in `packages/agents/src/bootstrap.ts`.

#### Delivery surfaces and infrastructure
Agents have a reusable React UI (`packages/agents-server-ui/src/main.tsx`), Electron desktop shell (`packages/agents-desktop/src/main.ts`), Expo mobile client (`packages/agents-mobile/App.tsx`), and MCP bridge supporting HTTP/stdio transports (`packages/agents-mcp/src/index.ts`, `packages/agents-mcp/src/transports/http.ts`). Material external boundaries are PostgreSQL logical replication, Docker sandbox execution (`packages/agents-runtime/src/sandbox/docker.ts`), optional E2B remote sandboxes (`packages/agents-runtime/src/sandbox/remote/e2b.ts`), LLM/model providers, MCP servers, and OpenTelemetry/Sentry-capable telemetry (`packages/sync-service/lib/electric/telemetry/open_telemetry.ex`).

Main entry points: [`packages/agents-server/src/server.ts`](https://github.com/electric-sql/electric/blob/main/packages/agents-server/src/server.ts) (Agents Server), [`packages/sync-service/lib/electric/stack_supervisor.ex`](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/stack_supervisor.ex) (Stack Supervisor), [`packages/agents-runtime/src/index.ts`](https://github.com/electric-sql/electric/blob/main/packages/agents-runtime/src/index.ts) (Agents Runtime SDK), [`packages/sync-service/lib/electric/postgres/replication_client.ex`](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/postgres/replication_client.ex) (Replication Client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_sync["Postgres Sync Service"]
  node_sync_app{{"OTP Application<br/>Elixir entrypoint<br/>[application.ex]"}}
  node_stack_supervisor["Stack Supervisor<br/>OTP lifecycle"]
  node_connection_manager["Connection Manager<br/>Postgres connections<br/>[manager.ex]"]
  node_replication_client["Replication Client<br/>logical replication"]
  node_decoder["Replication Decoder<br/>change decoder<br/>[decoder.ex]"]
  node_transaction_builder["Transaction Builder<br/>change transactions"]
  node_event_router["Shape Event Router<br/>predicate routing<br/>[event_router.ex]"]
  node_materializer["Shape Materializer<br/>shape state<br/>[materializer.ex]"]
  node_shape_cache[("Shape Cache<br/>logs and snapshots<br/>[shape_cache.ex]")]
  node_file_storage["File Storage<br/>cache persistence"]
  node_shape_api["Shape HTTP API<br/>OpenAPI contract<br/>[electric-api.yaml]"]
  node_router["HTTP Router<br/>Plug router<br/>[router.ex]"]
  node_serve_shape["Shape Serving Plug<br/>HTTP/SSE delivery"]
end

subgraph group_streams["Durable Streams"]
  node_durable_streams{{"Durable Streams Service<br/>Rust executable<br/>[main.rs]"}}
  node_streams_handlers["Stream HTTP/SSE Handlers<br/>request handling<br/>[handlers.rs]"]
  node_stream_store[("Stream Store<br/>durable persistence<br/>[store.rs]")]
  node_wal["WAL Recovery<br/>write-ahead log<br/>[mod.rs]"]
end

subgraph group_agents["Agents Platform"]
  node_agents_server{{"Agents Server<br/>Node server<br/>[server.ts]"}}
  node_entity_manager["Entity Manager<br/>session projection<br/>[entity-manager.ts]"]
  node_agent_runtime["Agents Runtime SDK<br/>agent execution<br/>[index.ts]"]
  node_model_runner["Model Runner<br/>LLM and tool loop<br/>[model-runner.ts]"]
  node_sandbox["Sandbox Execution<br/>Docker or E2B<br/>[docker.ts]"]
end

subgraph group_surfaces["Client &amp; Delivery Surfaces"]
  node_sync_clients["Shape Clients<br/>TypeScript and Elixir SDKs<br/>[client.ts]"]
  node_react_hooks["React Hooks<br/>React integration<br/>[react-hooks.tsx]"]
  node_agent_ui["Agents UI<br/>React application<br/>[main.tsx]"]
  node_mcp_bridge["MCP Bridge<br/>HTTP and stdio bridge<br/>[index.ts]"]
end

node_postgres[("PostgreSQL<br/>external database")]
node_llm_providers(("LLM Providers<br/>external models"))

node_sync_app -->|"starts"| node_stack_supervisor
node_stack_supervisor -->|"supervises"| node_connection_manager
node_stack_supervisor -->|"hosts"| node_router
node_postgres -->|"logical replication"| node_replication_client
node_connection_manager -->|"manages"| node_replication_client
node_replication_client -->|"replication messages"| node_decoder
node_decoder -->|"decoded changes"| node_transaction_builder
node_transaction_builder -->|"transactions"| node_event_router
node_event_router -->|"matching shape events"| node_materializer
node_materializer -->|"materializes"| node_shape_cache
node_shape_cache -->|"persists"| node_file_storage
node_shape_api -.->|"specifies"| node_router
node_router -->|"routes shape requests"| node_serve_shape
node_serve_shape -->|"reads snapshots and changes"| node_shape_cache
node_sync_clients -->|"consumes"| node_shape_api
node_react_hooks -->|"uses"| node_sync_clients
node_durable_streams -->|"serves"| node_streams_handlers
node_streams_handlers -->|"append and read"| node_stream_store
node_stream_store -->|"recovers through"| node_wal
node_agents_server -->|"projects entities"| node_entity_manager
node_entity_manager -->|"persists session state"| node_durable_streams
node_agents_server -->|"runs agents"| node_agent_runtime
node_agent_runtime -->|"executes"| node_model_runner
node_model_runner -->|"invokes models"| node_llm_providers
node_agent_runtime -->|"runs tools in"| node_sandbox
node_agent_ui -->|"uses"| node_agents_server
node_mcp_bridge -->|"bridges MCP clients"| node_agents_server

click node_sync_app "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/application.ex"
click node_stack_supervisor "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/stack_supervisor.ex"
click node_connection_manager "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/connection/manager.ex"
click node_replication_client "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/postgres/replication_client.ex"
click node_decoder "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/postgres/logical_replication/decoder.ex"
click node_transaction_builder "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/replication/transaction_builder.ex"
click node_event_router "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shapes/event_router.ex"
click node_materializer "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shapes/consumer/materializer.ex"
click node_shape_cache "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shape_cache.ex"
click node_file_storage "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shape_cache/pure_file_storage.ex"
click node_shape_api "https://github.com/electric-sql/electric/blob/main/website/electric-api.yaml"
click node_router "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/plug/router.ex"
click node_serve_shape "https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/plug/serve_shape_plug.ex"
click node_sync_clients "https://github.com/electric-sql/electric/blob/main/packages/typescript-client/src/client.ts"
click node_react_hooks "https://github.com/electric-sql/electric/blob/main/packages/react-hooks/src/react-hooks.tsx"
click node_durable_streams "https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/main.rs"
click node_streams_handlers "https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/handlers.rs"
click node_stream_store "https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/store.rs"
click node_wal "https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/wal/mod.rs"
click node_agents_server "https://github.com/electric-sql/electric/blob/main/packages/agents-server/src/server.ts"
click node_entity_manager "https://github.com/electric-sql/electric/blob/main/packages/agents-server/src/entity-manager.ts"
click node_agent_runtime "https://github.com/electric-sql/electric/blob/main/packages/agents-runtime/src/index.ts"
click node_model_runner "https://github.com/electric-sql/electric/blob/main/packages/agents-runtime/src/model-runner.ts"
click node_sandbox "https://github.com/electric-sql/electric/blob/main/packages/agents-runtime/src/sandbox/docker.ts"
click node_agent_ui "https://github.com/electric-sql/electric/blob/main/packages/agents-server-ui/src/main.tsx"
click node_mcp_bridge "https://github.com/electric-sql/electric/blob/main/packages/agents-mcp/src/index.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sync_app,node_stack_supervisor,node_connection_manager,node_replication_client,node_decoder,node_transaction_builder,node_event_router,node_materializer,node_shape_cache,node_file_storage,node_shape_api,node_router,node_serve_shape toneBlue
class node_durable_streams,node_streams_handlers,node_stream_store,node_wal toneAmber
class node_agents_server,node_entity_manager,node_agent_runtime,node_model_runner,node_sandbox toneMint
class node_sync_clients,node_react_hooks,node_agent_ui,node_mcp_bridge toneRose
class node_postgres,node_llm_providers toneNeutral
```

## Components

### Postgres Sync Service

- [OTP Application](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/application.ex): Elixir entrypoint · `packages/sync-service/lib/electric/application.ex`
- [Stack Supervisor](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/stack_supervisor.ex): OTP lifecycle · `packages/sync-service/lib/electric/stack_supervisor.ex`
- [Connection Manager](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/connection/manager.ex): Postgres connections · `packages/sync-service/lib/electric/connection/manager.ex`
- [Replication Client](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/postgres/replication_client.ex): logical replication · `packages/sync-service/lib/electric/postgres/replication_client.ex`
- [Replication Decoder](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/postgres/logical_replication/decoder.ex): change decoder · `packages/sync-service/lib/electric/postgres/logical_replication/decoder.ex`
- [Transaction Builder](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/replication/transaction_builder.ex): change transactions · `packages/sync-service/lib/electric/replication/transaction_builder.ex`
- [Shape Event Router](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shapes/event_router.ex): predicate routing · `packages/sync-service/lib/electric/shapes/event_router.ex`
- [Shape Materializer](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shapes/consumer/materializer.ex): shape state · `packages/sync-service/lib/electric/shapes/consumer/materializer.ex`
- [Shape Cache](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shape_cache.ex): logs and snapshots · Decouples replication ingestion from HTTP delivery. · `packages/sync-service/lib/electric/shape_cache.ex`
- [File Storage](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/shape_cache/pure_file_storage.ex): cache persistence · `packages/sync-service/lib/electric/shape_cache/pure_file_storage.ex`
- [Shape HTTP API](https://github.com/electric-sql/electric/blob/main/website/electric-api.yaml): OpenAPI contract · `website/electric-api.yaml`
- [HTTP Router](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/plug/router.ex): Plug router · `packages/sync-service/lib/electric/plug/router.ex`
- [Shape Serving Plug](https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/plug/serve_shape_plug.ex): HTTP/SSE delivery · `packages/sync-service/lib/electric/plug/serve_shape_plug.ex`

### Durable Streams

- [Durable Streams Service](https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/main.rs): Rust executable · Independent append/read stream service. · `packages/durable-streams-rust/src/main.rs`
- [Stream HTTP/SSE Handlers](https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/handlers.rs): request handling · `packages/durable-streams-rust/src/handlers.rs`
- [Stream Store](https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/store.rs): durable persistence · `packages/durable-streams-rust/src/store.rs`
- [WAL Recovery](https://github.com/electric-sql/electric/blob/main/packages/durable-streams-rust/src/wal/mod.rs): write-ahead log · `packages/durable-streams-rust/src/wal/mod.rs`

### Agents Platform

- [Agents Server](https://github.com/electric-sql/electric/blob/main/packages/agents-server/src/server.ts): Node server · Tenancy, permissions, entity projection, routing, and scheduling. · `packages/agents-server/src/server.ts`
- [Entity Manager](https://github.com/electric-sql/electric/blob/main/packages/agents-server/src/entity-manager.ts): session projection · `packages/agents-server/src/entity-manager.ts`
- [Agents Runtime SDK](https://github.com/electric-sql/electric/blob/main/packages/agents-runtime/src/index.ts): agent execution · `packages/agents-runtime/src/index.ts`
- [Model Runner](https://github.com/electric-sql/electric/blob/main/packages/agents-runtime/src/model-runner.ts): LLM and tool loop · `packages/agents-runtime/src/model-runner.ts`
- [Sandbox Execution](https://github.com/electric-sql/electric/blob/main/packages/agents-runtime/src/sandbox/docker.ts): Docker or E2B · `packages/agents-runtime/src/sandbox/docker.ts`

### Client & Delivery Surfaces

- [Shape Clients](https://github.com/electric-sql/electric/blob/main/packages/typescript-client/src/client.ts): TypeScript and Elixir SDKs · Track snapshots, parse streams, and reconnect. · `packages/typescript-client/src/client.ts`
- [React Hooks](https://github.com/electric-sql/electric/blob/main/packages/react-hooks/src/react-hooks.tsx): React integration · `packages/react-hooks/src/react-hooks.tsx`
- [Agents UI](https://github.com/electric-sql/electric/blob/main/packages/agents-server-ui/src/main.tsx): React application · `packages/agents-server-ui/src/main.tsx`
- [MCP Bridge](https://github.com/electric-sql/electric/blob/main/packages/agents-mcp/src/index.ts): HTTP and stdio bridge · `packages/agents-mcp/src/index.ts`

### Other components

- **PostgreSQL**: external database · Logical replication source.
- **LLM Providers**: external models

## Connections

- OTP Application → Stack Supervisor: starts
- Stack Supervisor → Connection Manager: supervises
- Stack Supervisor → HTTP Router: hosts
- PostgreSQL → Replication Client: logical replication
- Connection Manager → Replication Client: manages
- Replication Client → Replication Decoder: replication messages
- Replication Decoder → Transaction Builder: decoded changes
- Transaction Builder → Shape Event Router: transactions
- Shape Event Router → Shape Materializer: matching shape events
- Shape Materializer → Shape Cache: materializes
- Shape Cache → File Storage: persists
- Shape HTTP API → HTTP Router: specifies
- HTTP Router → Shape Serving Plug: routes shape requests
- Shape Serving Plug → Shape Cache: reads snapshots and changes
- Shape Clients → Shape HTTP API: consumes
- React Hooks → Shape Clients: uses
- Durable Streams Service → Stream HTTP/SSE Handlers: serves
- Stream HTTP/SSE Handlers → Stream Store: append and read
- Stream Store → WAL Recovery: recovers through
- Agents Server → Entity Manager: projects entities
- Entity Manager → Durable Streams Service: persists session state
- Agents Server → Agents Runtime SDK: runs agents
- Agents Runtime SDK → Model Runner: executes
- Model Runner → LLM Providers: invokes models
- Agents Runtime SDK → Sandbox Execution: runs tools in
- Agents UI → Agents Server: uses
- MCP Bridge → Agents Server: bridges MCP clients
