ZeBridge 发布:设备端 PostgreSQL 副本经 NATS 同步

原标题:ZeBridge – PostgreSQL replicas on devices, synchronized over NATS
58AI 研判分

ZeBridge 是一个面向 B2B 多租户运营应用的开源同步层:每台设备保留本地 PostgreSQL 副本,离线时写入排队,重连后经 NATS 同步,最终由中心 PostgreSQL 裁决冲突并返回权威结果。它明确不适用于单用户一租户的消费级应用,当前提供 Docker 快速启动演示。

报道正文

ZeBridge

Local PostgreSQL replicas for apps and services, over NATS.

ZeBridge keeps a local database on each device, queues writes while it is offline, and synchronizes through NATS when it reconnects. PostgreSQL remains authoritative: it applies the rules, resolves conflicting writes, and returns a verdict to the app.

The client library provides one interface to read the local replica, submit mutations, and query remote responders such as routing or analytics services. Optional regional NATS leaves serve snapshot caches near devices and host those services.

Strongest use cases: B2B operational apps, where each customer organisation is a tenant (a column in your schema) whose devices share a working dataset: field teams, fleets, depots, sites. They keep working on poor connections or while a team is cut off, and every write still ends with an authoritative decision from PostgreSQL. It is not built for consumer (B2C) apps with one tenant per user.

See it running: https://bridge.zebridge.eu/demo. Five trucks on a map of France's 16,173 charge points. Pick a truck and a destination, and it drives along the road on every open screen; change its destination from any screen and it turns from where it is. The chargers are a query on the browser's own replica, the route comes from a routing service over the same NATS connection, and PostgreSQL holds only the decisions. Open it in two tabs, or on a phone and a laptop: each visit gets its own identity in a shared demo tenant, from a one-hour invite. How it is built: the depot example.

Try it with Docker

You need Docker Compose. No repository clone or local build is required.

BASH
curl -O https://raw.githubusercontent.com/ndrean/ZeBridge/main/docker-compose.quickstart.yml
docker compose -f docker-compose.quickstart.yml up

This starts PostgreSQL, NATS, the bridge, its tombstone-cleanup process (the sweeper), and two browser demos using published images. This evaluation Compose file has no separate mutation admission service; the production layout described below includes one. The Compose file selects the image version; it does not build the current repository checkout.

When the init service prints the invite links, open these in separate tabs:

Each invite is single-use. The tab creates its own device key, enrolls with the bridge, and receives scoped NATS credentials. Wait for the four phase badges to turn green; the first snapshots can take up to a minute.

See tenant isolation. Change counter_tenant in an Alice tab: the other Alice tab follows, while Bob's does not. Change counter_public: all three follow. The page also demonstrates foreign keys, refused writes and cooperative editing.

Try offline writes. Keep the tabs open, then stop the services:

BASH
docker compose -f docker-compose.quickstart.yml down

Click the counters while disconnected. Each tab reads its own local replica and keeps its pending writes in an outbox. Start the services again:

BASH
docker compose -f docker-compose.quickstart.yml up

The tabs reconnect, submit their queued writes and catch up. Counters use last-writer-wins: the final value is the winning write, not the sum of offline clicks.

The initialization log also prints links to a shared-record demo on port 5174. It shows several editors changing separate fields of the same record.

To erase this evaluation stack and start again, run docker compose -f docker-compose.quickstart.yml down -v. This removes its database and NATS volumes, including the identities those invites depend on. The quick start uses local evaluation settings without NATS TLS; use the deployment guides for a server.

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)