Computed Shapes #4717
Closed
ItsJustMeChris
started this conversation in
Feature requests
Computed Shapes
#4717
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Computed Shapes
I’ve been experimenting with “Computed Shapes”: named, server-defined reactive queries whose results are delivered through the existing Shape protocol.
They’re conceptually similar to Convex reactive functions or incrementally maintained materialized views. Instead of syncing every underlying row to clients, Electric maintains a keyed derived result and streams only changes to that result.
For example, rather than syncing every app open/close event, a client could subscribe to:
The prototype supports:
COUNT/SUMaggregations.DISTINCT, groupedMIN/MAX, top-N results, inner joins, and session rollups.Performance looks promising for high-reduction queries. In an end-to-end Postgres → WAL → HTTP test, 100 source events became one derived update:
At 100k changes, incremental COUNT/SUM took about 1.48× the CPU of raw Shape conversion while retaining 0.86% of the state and producing 0.97% of the output. Top-N and joins cost more CPU—about 2.5× and 3.1× respectively—but can still dramatically reduce client-side data.
The important finding is that generic recompute-and-diff does not scale well for large, sparsely changing results. Incremental maintenance is what makes this practical.
There are real tradeoffs beyond implementation complexity: Electric takes on more CPU, memory, durable state, recovery semantics, type/collation correctness, and potential join amplification. The source events also still pass through Postgres WAL and Electric; they simply no longer need to reach every client.
The experimental implementation and detailed benchmarks are available here:
I’d be interested in feedback on whether this fits Electric’s model and which query patterns would be most valuable to support first.
All reactions