617 points · by samwillis · 3y ago · 170 comments
We're really excited to be sharing ElectricSQL with you today. It's an open source, local-first sync layer that can be used to build reactive, realtime, offline-capable apps directly on Postgres with two way active-active sync to SQLite (including with WASM in the browser).
Electric comprises a sync layer (built with Elixir) placed in front of your Postgres database and a type safe client that allows you to bidirectionally sync data from your Postgres to local SQLite databases. This sync is CRDT-based, resilient to conflicting edits from multiple nodes at the same time, and works after being offline for extended periods.
Some good links to get started:
- website: https://electric-sql.com
- docs: https://electric-sql.com/docs
- code: https://github.com/electric-sql/electric
- introducing post: https://electric-sql.com/blog/2023/09/20/introducing-electri...
You can also see some demo applications:
- Linear clone: https://linear-lite.electric-sql.com
- Realtime demo: https://electric-sql.com/docs/intro/multi-user
- Conflict-free offline: https://electric-sql.com/docs/intro/offline
The Electric team actually includes two of the inventors of CRDTs, Marc Shapiro and Nuno Preguiça, and a number of their collaborators who've pioneered a lot of tech underpinning local-first software. We are privileged to be building on their research and delighted to be surfacing so much work in a product you can now try out.
We didnt find anyone else who was tackling the tie between client-side SQLite, an open source CRDT/sync layer, and Postgres. We found a few who were attempting to manage this in a closed source way, but it didn't make sense to give up control of the server/auth, and every other fully open source solution was SQLite -> SQLite, not Postgres.
Great stuff from some killer engineers.
Also is there a way to transition an existing Postgres data base into using electicsql?
This is the exact pattern we target :)
Drop Electric onto an existing Postgres data model and use the Shape-based sync to manage dynamic partial replication of subsets of data between central Postgres and local SQLites: https://electric-sql.com/docs/usage/data-access/shapes
James
We currently use Hasura subscriptions to pull data to the frontend (effectively select * from table where account_id = X and updated > recently). We then funnel changed rows into mobx objects so we have a lovely object graph to work with.
I’m imagining doing the same with electric so I guess you’d use notify to hear that there has been a change, then figure out if it’s in a table you care about and then select the updated records from that to merge into mobx?
Basing that off what I see in here: https://github.com/electric-sql/electric/blob/main/clients/t...
We’d definitely be interested in chatting through the reactivity model you use if you’d be up for it.
* I am by no means a crdt expert, but from my tinkering I have come to the opinion that the upside of the consistency does come at the cost of ease of understanding how state ends up how it does, which can bleed into user facing issues if how a crdt converges conflicts with intuition. A lot of that seems like an education and design problem and I am curious how you are thinking of talking that? Do you plan on some support for server-first writes for situations where crdt might not fit?
* from the docs it seems like adding electric to a table does a migration to add new columns / new tables, I assume, to support the crdt, but my other knowledge of crdts is that it can be expensive in terms of size, especially for preserving history. I will have to poke at it to learn more, but I do wonder where you think you shouldn't enable electric?
* I am curious at the commercial model? Are you going full cloud service like Supabase/neon? Or just host the elixir component?
I will keep poking, but really really interesting!
We have a Discord if you fancy chatting at more length! https://discord.electric-sql.com
Re: CRDTs and intuition, yup, there's trade offs. We are working hard to deliver a solid model to code against. So we have finality of local writes and standard relational integrity guarantees. You can read a bit more about some of techniques here https://electric-sql.com/blog/2022/05/03/introducing-rich-cr...
Re: electrify yes, we create a shadow table and some operation log tables etc. So there is some storage amplification but it's of the order of 2x (we use operational based CRDTs and we garbage collect the operation log). We see the fit being with standard OLTP workloads, mainstream relational apps. We don't target high volume data ingestion, etc.
Re: commercial model, we're designed for open source self-host. We see Electric as a drop in sync layer, not as a full stack backend-as-a-service. Lots of options but concentrating on product for now.
I'm sure I don't understand CRDTs. Can they be used with database changes that are not part of the CRDT history? That is, can they be added to an existing database with other clients?
I am curious about encryption. Assuming that ElectricSQL is handling essentially all of the syncing, is it possible to build applications that use end-to-end encryption for some of their state? I am wondering specifically because CRDTs seem to simplify E2EE a lot since well, any client that can write can resolve conflicts any time. I haven't looked very deeply yet as I've been very busy, but to me that's the main thing I've been really curious about. I know you can at least do this with Y.js which helpfully provides a way to use symmetric encryption for WebRTC synchronization, but this obviously is a whole lot more than that.
If you want encrypted and shared with other devices or users you have to surface key management to users which is very difficult to do. Signal and a few others do this but they’re very bespoke to the specific business logic. To provide a general purpose abstraction layer is still an open problem, afaik.
Just like TLS, it'd probably be bad if most people implemented SRP from scratch. That said, I did write my own implementation of SRP-6a (a variant of SRP based on the RSA cryptosystem) in TypeScript and I found it fairly simple to do. There are also PAKEs that provide even better security properties than SRP-6a, but good implementations of them are still lacking for now.
What Matrix does seems pretty simple, it just has a lot of moving parts and nuance; the underlying keys are essentially sent directly between clients after a key exchange is performed and verified out of band to ensure there is no eavesdropping and that the two clients are connected to who they think they are.
As far as I know, Signal sidesteps key management almost entirely and only allows one logged in device, and everything else must proxy through it. That's pretty lame, though I understand the stakes are very high to get it 'right' and keeping the moving parts to a minimum was likely high priority.
Academically it’s in a decent state, but tech- and UX wise I’d say it’s immature. It virtually penetrates all layers of a normal stack (including layer 8). For apps it’s at least possible but the web is very tricky as a platform for anything e2ee without absurd usage requirements.
The tech choices that remain after deciding on an e2ee model are severely limited and language dependent, and spans deeply across persistence layers, software updates, authn, authz, social graphs, moderation etc etc. And even so, there are many subtleties in terms of privacy and information leakage that are often necessary compromises for features that many apps take for granted today. Without any social aspects it’s significantly easier (eg PAKE is best suited for for interactive/online two-party operations).
W3C DID pubkeys can be stored in a W3C SOLID LDPC: https://solid.github.io/did-method-solid/
Re: W3C DIDs and PKI systems; CT Certificate Transparency w/ Merkle hashes in google/trillian and edns, keybase pgp -h, blockchain-certificates/cert-verifier-js,: https://news.ycombinator.com/item?id=36146424 https://github.com/blockchain-certificates
In a traditional client / server model, the server has an opportunity to validate each request and optionally reject it. The lower level you go with the sync protocol (data changes vs high level requests) the more difficult that becomes.
Have you addressed that and, if so, how? What prevents a malicious client from send arbitrary data streams to get synced into the root database?
You can see our database rules spec here: https://electric-sql.com/docs/api/ddlx
We haven't implemented it all yet but you can see the intention / direction. It's similar to RLS but adapted for the context.
Connections are authenticated by signed JWT: https://electric-sql.com/docs/usage/auth
We also auto-generate a type-safe data access client from the electrified subset of the Postgres schema. This applies type-level write validation and will apply general write validation when we get there.
James.
I'm visiting the docs.
> ...This must have at least a user_id claim, which should be a non-empty string matching the primary key UUID of the authenticated user...
You should probably strike this from your docs. It sounds like you are still figuring out how this should work.
The "right" answer is to author an OIDC authentication module for Postgres and license it in a way that Amazon will steal it for the purposes of actual adoption; or try to add it to PGBouncer as a proxy, converting JWTs in the password field into "set local user.id = ..." and similar in a preamble for every sql statement.
Projects like postgrest have been down this journey and spell out their solutions. It's all a little vague with Supabase, there isn't anything magical about their authorization approach either, but I wouldn't keeping looking to them as the gold standard implementation for web backend stuff.
Anyway, none of this exists in SQLite, which is a huge pain in the butt. SQLite looks promising, and then it's missing all this stuff, because it doesn't make any sense for SQLite to have row level security or JWTs or whatever. That's the issue isn't it? Like why not step back and say, "is SQLite right for me, if it's not only missing all these features I need, but also they will never be added to SQLite because it doesn't make sense for SQLite to have them?"
Separately, when I visit https://electric-sql.com/docs/intro/local-first, it's ironic, the local first says its latency is 3ms, but because it had to load all this code and stuff, it took longer than 237ms of the "cloud-first" box for that 3ms number to even appear. I've been here before, I was a Meteor developer, I am cognizant of the tradeoffs and what this measurement is saying. There's no such thing as a free lunch.
Just in case it's not entirely clear: supabase is just PostgreSQL + PostgREST. We contribute to + maintain PostgREST, so if it works with PostgREST it also works with Supabase.
> there isn't anything magical about their authorization approach either
I 100% agree with this, and that's intentional. We don't want to do anything special here, we want our solutions to be as interoperable as possible with existing approaches
What's the alternative in cases where I need more advanced checking before doing an INSERT/UPDATE that is not possible in SQL?
This is usually done in the backend. The frontend is not a trusted environment.
So, I'd just send a request to the backend, perform the checks, modify the data in Postgres and then it'd sync to the clients?
The PowerSync client SDK still handles the queueing and retrying of writes so that they can be automatically retried if network connectivity is not available — whenever there is a retry, your callback function is called. (As a result of this approach, your write function should be idempotent; we commend using GUIDs or UUIDs generate client-side for primary keys)
Similar to Electric, PowerSync also uses JWT for auth against your backend.
There are some architecture diagrams explaining this on our docs root here: https://docs.powersync.co/
When it comes to concurrency problems like not spending money twice, the plan is https://electric-sql.com/blog/2022/05/03/introducing-rich-cr...
If you have managed to pull this off in a way that reliably scales and reliably keeps all the data updated and uncorrupted I am mighty impressed. My hat off for you folks.
How do you handle transactions and rollbacks?
The model is Transactional Causal+ Consistency (TCC+). This has been formally proven to be the strongest possible consistency mode for an AP database system.
We have a page on literature here https://electric-sql.com/docs/reference/literature, which includes the Cure paper http://ieeexplore.ieee.org/document/7536539/ on TCC+ and the highly available transactions paper https://dl.acm.org/doi/10.14778/2732232.2732237
Annette Bieniusa, Nuno Preguiça and Marc Shapiro from our team are authors of the Cure work (amongst many other things!).
Re: rollbacks, our model is to provide finality of local-writes: https://electric-sql.com/docs/reference/architecture#local-w... -- which means you don't have to write rollback handlers.
How does this deal with schema changes? We have a solution where old clients can store objects that have new fields unknown to the client. Then when the clients updates the data is visible.
Do you have client libraries for mobile? Swift & Kotlin?
Does it support partial sync and fetch when you need an “archived” item? In my experience you only want the “working set” of data on the client. Not everything.
Cool work! CRDTs are the solution to this.
You can learn more about our support for migrations here: https://electric-sql.com/docs/usage/data-modelling/migration...
Native clients will be coming a bit later.
You can use the shapes to control what data syncs to the device: https://electric-sql.com/docs/usage/data-access/shapes
The CouchDB/PouchDB pattern is how I originally got interested in local first, they are such a good tool, but having the full power of Postgres and then SQLite on the client, I believe, is a real game change.
Sounds like Electric could be a really good fit for your use case. If want any advice join the Discord and we are happy to help out.
Looking forward to seeing more people trying out this architecture.
Still hoping we can find some time to collaborate on reactivity, tree-sql, typed-sql or some such other effort in the near future.
Electric is designed to support partial sync, and so you don’t have to sync your whole dataset. (Note that this is feature is under development and not yet public)
There are limitations on how much data a browser will store for an individual site, so the number of rows you can sync will depend on the shape of your dataset. Finally there are also some performance considerations with WASM SQLite, this is something the SQLite team are working on in collaboration with the browser developers, particularly with the development of the new OPFS apis which we plan to support as they mature.
So, thousands of rows are definitely viable, and we have that working with our own internal development tests. Hundreds of thousands or millions may cause issues right now, but are something we do want to support.
https://www.sqlite.org/cves.html
> Almost all CVEs written against SQLite require the ability to inject and run arbitrary SQL.
> The advertised consequence of most CVEs is "denial of service", typically by causing a crash through a NULL pointer dereference or a division by zero, or similar.
> But if an attacker can already run arbitrary SQL, they do not need a bug to cause a denial of service. There are plenty of perfectly legal and valid SQL statements that will consume unlimited CPU, memory, and disk I/O in order to create a denial-of-service without requiring help from bugs.
> Hence, the mere fact that an attacker has a way to inject and run arbitrary SQL is in and of itself a denial-of-service attack. That the arbitrary SQL might also tickle a bug in SQLite and cause a crash is not a new vulnerability.
I have yet to dive deeper in the docs and examples, but does anyone know how easy it would be with Electric to provide some kind of client-side transformations (encryption/decryption) of specific fields or entire rows in your SQLite db before syncing? A major reason to go local-first, is often privacy, and the idea that the user owns their data...
Also, it's unfortunate permissions haven't been implemented yet; https://electric-sql.com/docs/usage/data-modelling/permissio... If I understand correctly, that means in a multi-user system, everyone will sync everyone else's data?
And too bad my favorite stack (Angular + Nest.js backend) isn't supported, but I guess it should still be straightforward to integrate the client-side and run the sync service in the backend. I might try it if I have a weekend...
Handling encryption/decryption of user data is one of our priorities. We're thinking along the lines of adding hooks for transforming data as you're seeing it.
We're getting there on permissions implementations. If you want to do row-based filtering based on user_id today, you can add a special column 'electric_user_id' to your table: https://electric-sql.com/docs/reference/roadmap#shapes
We plan to integrate Electric with popular web frameworks: https://electric-sql.com/docs/integrations/frontend. To integrate with Angular, the client should work out of the box. What you'd need is to provide is the code for reactivity, which should not be a lot. Reach out on Discord and we can have a chat about it.
E2E encryption would also be nice to have, it could be worth having a look at https://www.etesync.com/ for inspiration
We assessed nearly every new data solution available and ultimately chose Electric. We recognized that the combination of SQLite's convenience on the client-side and PostgreSQL's flexibility on the server-side is a potent foundation for our work.
I've spent years grappling with the challenges of the local-first (lo-fi) approach, particularly when using the PouchDB <-> CouchDB combination to support vital offline features in our Point of Sale tools. While not every feature aligns with the lo-fi concept (even though real-time synchronization status on the client can significantly reduce the need for numerous API end points), having PostgreSQL on the backend grants us access to a wealth of existing tools and support built around PostgreSQL. This assurance provides us with peace of mind, knowing that we won't encounter insurmountable obstacles, and any challenges we face will be no more daunting than those encountered in traditional server-based API programming paradigms.
I sincerely hope that ElectricSQL achieves the recognition it deserves and can implement many of the ideas outlined in their roadmap documentation. These ideas have the potential to overcome the initial hurdles that have hindered the widespread adoption of the lo-fi approach.
In the meantime, we're having a great time developing an opinionated, React-based low-code tool (internally, we refer to it as "Visual Basic using React"). We're looking forward to leveraging some of the outstanding work done by James' team thus far!
We'd welcome very much if you could reproduce the steps and report an issue: GH: https://github.com/electric-sql/electric/issues Discord: https://discord.com/channels/933657521581858818/107968886985...
I was thinking about using it for an app that I'm developing but decided to not ship a 2mb binary to every user.
And that led me to discover that IndexedDB is actually quite interesting.
Any plans to integrate EletricSQL with IndexedDB?
We currently use IndexedDB for the virtual filesystem underneath the wa-sqlite [1] WASM SQLite driver in the browser. But yup, that means a ~1.1MB WASM download.
We'll definitely stay with SQLite in the client. One of our key principles is full SQL support (any Postgres supported SQL on the server, any SQLite supported SQL on the client). So we won't go pure IndexedDB without a database on top.
There's a lot of cool stuff going on with OPFS and WASM SQLite atm, so the technical options are evolving fast.
I thought the bundle size of wasm-sqlite would be prohibitive, but it's surprisingly quick to download and boot. Reducing network reliance solves so many problems and corner-cases in my web app. Having access to local data makes everything very snappy too - the user experience is much better. Even if the user's offline data is wiped by the browser (offline storage limits are a bit of a minefield), it is straightforward to get all synced changes back from the server.
WASM is much faster to start up since you don't have all the parsing and compiling overhead.
PowerSync is also a plug-in sync layer. The biggest difference I see is in Electric's use of CRDTs, where we don't rely on them and instead use server reconciliation.
As a team that's been working on online/offline sync for just over a decade, it's great to finally see more products that enable offline-first architectures!
Does this play nice with RLS? I’m running everything on Postgrest at the moment.
Re: RLS, not quite, you'll need to port your rules to our DDLX syntax https://electric-sql.com/docs/api/ddlx -- which is not fully implemented yet (we're working on right now, will be available shortly).
It looks like the "Switch from X" buttons don't go anywhere.
Would something like this be possible?
The team at SkillDevs are maintaining a Daft/Flutter client at https://github.com/SkillDevs/electric_dart
Plus we have a thread to extract the core client-side replication component to Rust to be able to compile for multiple targets.
I've been using https://github.com/cpursley/walex (basically a fork of cainophile via a fork for subabase) to listen to Postgres changes in Elixir.
Sequin is also doing some neat stuff in this space as well: https://blog.sequin.io/all-the-ways-to-capture-changes-in-po...
I notice this is still a 0.X version. How comfortable are you with people using this in production? Are there any success stories so far? And if it's not production ready, is there a roadmap I can check out?
The company I work for is in the process of planning a complete rework of our app and right now is the time for us to choose technologies. I so very much want to use this or something like this.
We don't recommend production use right now. There's a roadmap page here https://electric-sql.com/docs/reference/roadmap -- we particularly need to flesh out shapes, permissions and validation.
Happy to chat (e.g. on our Discord) if you'd like a bit more detail / looking at whether Electric could fit into your tech pans.
And, I may take you up on a chat after I've had a conversation with my team! At the very least, I've joined the Discord and I'll definitely be following you guys closely.
Since you mentioned this — you could also take a look at PowerSync which is similar (disclosure: co-founder) https://docs.powersync.co/ - Currently beta suitable for production use.
The WASM module is just a standard build of SQLite, about 1.1mb - there are many landing pages 10x that size - and something that we think is easily justifiable for the types of apps that people can build with Electric.
In terms of use cases, there are numerous, broad-ranging possibilities - anything from a replacement for GraphQL or a REST API in your existing stack, all the way up to building large collaborative apps. Our intention is that you can build collaborative tools similar to Linear, and we do actually have a demo of this sort of thing (http://linear-lite.electric-sql.com), but it’s certainly not limited to that.
I'm interested in your calculation for compute cost savings. You have savings of 40k$ for 2k queries per second. I'm currently using GCP CloudSQL (Postgres) and have > 2k requests per second (for tables with billions of rows) for 200$/month. Is there a scenario that I'm overlooking?
Edit: Saw the explanation on your website. Apparently you're comparing it to AWS Aurora which charges for the amount of queries.
edit: we based some of the calculator variables on https://fauna.com/blog/compare-aws-aurora-serverless-v2-arch...
This is perfect to create super fast offline first apps like linear.app, with Postgres
I still maintain that we need an interchange standard for write ahead logs, but tools like this are a step in the right direction.
I would like to point out thought why you may be underselling this. Local-first yes, but that dynamic can also apply to edge-networked apps (at least before everyone started equating edge networking with Lambda).
Excited to take a closer look.
Also, it reinforces that fact that everyone should pronounce SQL as 'squeal'.
'S.Q.L.' is far too many syllables, and 'sequel' isn't much better. 'Squeal' is just one syllable, so obviously better.
Also, I love how this project now has me singing "OO girl, shock me like an electricSQL".
I think that "sequel" and "squeal" have basically the exact same consonant and vowel sounds just in different orders.
I mean I know you're making a bit of a joke here, but I'm having fun saying the two words and picking apart the sounds.
Quick question about the business side of things: I saw you're Apache 2.0 and VC-backed. So what's the plan for making money down the line?
[1]: https://electric-sql.com/docs/usage/data-access/shapes#data-loadingWe will be working on the engineering to handle transferring large shapes.
I was wondering: what is the difference between VLCN and ElectricSQL?
Thank you!
James here, one of the co-founders of Electric.
Both projects are doing active-active CRDT-based sync. Our focus is on sync via Postgres and on compatibility with existing Postgres-backed applications. So you can drop Electric onto an existing Postgres-backed system and it works with your existing data model.
There's also quite a lot of difference in the development model, how we handle migrations, shape-based partial replication, etc. And we're not focused on p2p sync -- for us, everything goes through Postgres.
Hope that helps -- you can read a bit more about the system design here: https://electric-sql.com/docs/reference/architecture
On the clientside, is SQLite running in a separate thread? Or is it running on the main thread? Is this the same or different for an electron app vs running in the browser?
With Electron / Tauri it's the same as the browser. You're running in Chromium / native WebView on the front-end side.
Or is there a better tool for that?
If the solution is to fire up a docker image with your app, then stop! Why not just use sqlite first?
Out of curiosity - the demo on the site suggests the local first latency is like 10-40 ms. I would think reading/writing to SQLite locally would be on the order of 1ms or less. Why is that?
Quite right, the latency should be low single figure ms. There is actually a slight bug in how that latency is calculated on the demo and includes an extra render tick, I was digging into it earlier but didn't get a chance to push a fix yet.
2. Is there a way to disable auto resolving of conflicts? i.e to let the user know of the conflict?
How do you decide what queries are relevant?
// Once we have electric, we then establish the data change
// notification subscription, comparing the tablenames used by the
// query with the changed tablenames in the data change notification
// to determine whether to re-query or not.
//
// If we do need to re-query, then we use the saved function to reuse the query
So it re-runs queries on all updates in tables from the query.Basically Electric is horizontally scalable. You can run multiple Electric sync services on top of Postgres and then obviously many clients connecting to the Electric. We're focused on running on top of a single Postgres right now (which we take query load off).
Would love to see something like this natively for iOS.
Technically it’s possible to support different backend databases. But we want to focus on getting Postgres right first.