How electric-sql/electric works
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: server.ts Agents Server · stack_supervisor.ex Stack Supervisor · index.ts Agents Runtime SDK · replication_client.ex Replication Client
Components 28
Postgres Sync Service
- OTP Application
packages/sync-service/lib/electric/application.ex - Stack Supervisor
packages/sync-service/lib/electric/stack_supervisor.ex - Connection Manager
packages/sync-service/lib/electric/connection/manager.ex - Replication Client
packages/sync-service/lib/electric/postgres/replication_client.ex - Replication Decoder
packages/sync-service/lib/electric/postgres/logical_replication/decoder.ex - Transaction Builder
packages/sync-service/lib/electric/replication/transaction_builder.ex - Shape Event Router
packages/sync-service/lib/electric/shapes/event_router.ex - Shape Materializer
packages/sync-service/lib/electric/shapes/consumer/materializer.ex - Shape Cache · Decouples replication ingestion from HTTP delivery.
packages/sync-service/lib/electric/shape_cache.ex - File Storage
packages/sync-service/lib/electric/shape_cache/pure_file_storage.ex - Shape HTTP API
website/electric-api.yaml - HTTP Router
packages/sync-service/lib/electric/plug/router.ex - Shape Serving Plug
packages/sync-service/lib/electric/plug/serve_shape_plug.ex
Durable Streams
- Durable Streams Service · Independent append/read stream service.
packages/durable-streams-rust/src/main.rs - Stream HTTP/SSE Handlers
packages/durable-streams-rust/src/handlers.rs - Stream Store
packages/durable-streams-rust/src/store.rs - WAL Recovery
packages/durable-streams-rust/src/wal/mod.rs
Agents Platform
- Agents Server · Tenancy, permissions, entity projection, routing, and scheduling.
packages/agents-server/src/server.ts - Entity Manager
packages/agents-server/src/entity-manager.ts - Agents Runtime SDK
packages/agents-runtime/src/index.ts - Model Runner
packages/agents-runtime/src/model-runner.ts - Sandbox Execution
packages/agents-runtime/src/sandbox/docker.ts
Client & Delivery Surfaces
- Shape Clients · Track snapshots, parse streams, and reconnect.
packages/typescript-client/src/client.ts - React Hooks
packages/react-hooks/src/react-hooks.tsx - Agents UI
packages/agents-server-ui/src/main.tsx - MCP Bridge
packages/agents-mcp/src/index.ts
Other components
- PostgreSQL · Logical replication source.
- LLM Providers
Connections 27
- 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