Lurker News
Hi HN, James, Valter, Sam and the team from ElectricSQL here.

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.

Comments (170) 71 threads

Earlier comments
koolba3 replies#
Is there any validation or authorization for changes being merged back into the root database?

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?

FrancoisBosun1 reply#
Not the author, but PostgreSQL has constraint triggers that can run procedures / functions on insert/update/delete, to allow/reject a given row or statement. That would be one way to confirm that a given update from a client is valid, from the POV of the application.
koolba1 reply#
The situation I’m considering is data that matches the referential integrity and check constraints of the database, but is malicious. For example syncing a “salary update to $1M” for yourself into the source database.

Continue this thread — 1 reply

thruflo2 replies#
Hey,

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.

doctorpangloss1 reply#
> It's similar to RLS but adapted for the context.

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.

kiwicopple#
> Projects like postgrest have been down this journey and spell out their solutions. It's all a little vague with Supabase

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

rmbyrro#
I understand it'd be necessary to implement all auth and check rules in SQL queries using roles and the CHECK statement.

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?

ochiba2 replies#
The route that we went with for PowerSync (disclosure: co-founder) is to allow to define your own function for handling writes, where you would use your (presumably existing) backend application API to persist the writes to Postgres. Therefore you can handle any validation/authZ/business logic for writes in your backend layer.

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/

meiraleal1 reply#
You are really taking the expression `shameless plug` to another level in this thread, aren't you?
ochiba1 reply#
Genuinely excited about this space and it's what I'm focused on full-time so definitely have thoughts to share. I am wary of self-promotion. I do want to contribute things that I feel are relevant to the discussion, since I assume folks would be interested to see different patterns/approaches around local-first/offline-first architecture.

Continue this thread — 1 reply

cebert#
Can you please stop posting as much about PowerSync on a Show HN thread centered on ElectricSQL?
ThinkBeat1 reply#
I get dizzy even trying to envision all the edge cases and challenges making an active - active sync between two different database engines, over what would be tens of thousands (hundres?) of simultaneous connection to clients via a non-reliable, expect network disconnect, and reconnect from a different location from the same client.

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?

thruflo#
Hey, thanks :) and yup, lots of complexity and engineering. We're lucky because we're building on a lot of research and previous work.

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.

TeeWEE2 replies#
Congrats.

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.

vbalegas#
It works similarly: migrations flow as part of the replication stream and clients apply them immediately, so that new data fits the local schema. New columns and etc might not immediately be exposed to the client app, but everything will works for old clients.

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

nerdyadventurer#
I was also going to ask about mobile clients, then saw your comment.
pgm87051 reply#
Awesome! I'm looking forward to trying this out. Currently I get this functionality by using PouchDB on the client with a CouchDB sever. Then on my API server I have some janky code in a cron job to sync changes from CouchDB to PostgreSQL.
samwillis#
Hey, I work at Electric,

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.

Load more comments