from Hacker News

Show HN: Triplit – Open-source syncing database that runs on server and client

by matlin on 6/25/24, 1:53 PM with 100 comments

Hey HN, we’re Matt and Will, the co-founders of Triplit (https://www.triplit.dev). Triplit is an open-source database (https://github.com/aspen-cloud/triplit) that combines a server-side database, client-side cache, and a sync engine into one cohesive product. You can try it out with a new project by running:

  (npm|bun|yarn) create triplit-app
As a team, we’ve worked on several projects that aspired to the user experience of Linear or Superhuman, where every interaction feels instant like a native app while still having the collaborative and syncing features we expect from the web. Delivering this level of UX was incredibly challenging. In each app we built, we had to implement a local caching strategy, keep the cache up to date with optimistic writes, individually handle retries and rollbacks from failures, and do a lot of codegen to get Typescript to work properly. This was spread across multiple libraries and infrastructure providers and required constant maintenance.

We finally decided to build the system we always wanted. Triplit enables your app to work offline and sync in real-time over websockets with an enjoyable developer experience.

Triplit lets you (1) define your schema in Typescript and simply push to the server without writing migration files; (2) write queries that automatically update in real-time to both remote changes from the server and optimistic local mutations on the client—with complete Typescript types; (3) run the whole stack locally without having to run a bunch of Docker containers.

One interesting challenge of building a system like this is enabling partial replication and incremental query evaluation. In order to make loading times as fast as possible, Triplit will only fetch the minimal required data from the server to fulfill a specific query and then send granular updates to that client. This differs from other systems which either sync all of a user’s data (too slow for web apps) or repeatedly fetch the query to simulate a subscription (which bogs down your database and network bandwidth).

If you’re familiar with the complexity of cache-invalidation and syncing, you’ll know that Triplit is operating firmly in the distributed systems space. We did a lot of research and settled on a local first approach that uses a fairly simple CRDT (conflict-free replicated data type) that allows each client to work offline and guarantees that they will converge to a consistent state when syncing. It works by treating each attribute of an entity as a last writer wins register. Compared to more complex strategies, this approach ends up being faster and doesn’t require additional logic to handle conflicting edits between concurrent writers. It’s similar to the strategy Figma uses for their collaborative editor.

You can add Triplit to an existing project by installing the client NPM package. You may self-host the Triplit Server or pay us to manage an instance for you. One cool part is that whether you choose to self-host or deploy on Triplit Cloud, you can still use our Dashboard to configure your database or interactively manage your data in the Triplit Console, a spreadsheet-like GUI.

In the future, we plan to add APIs for authentication, file uploads, and presence to create a Supabase/Firebase-like experience.

You can get started by going to https://triplit.dev or find us on Github https://github.com/aspen-cloud/triplit. Thanks for checking us out and we are looking forward to your feedback in the comments!

  • by thanhnguyen2187 on 6/26/24, 3:01 AM

    Hi Triplit's team,

    Congrats on the launch and thanks for the awesome product! I've been using Triplit in one of my projects [1], and it do work as expected. In my self-promotion on Reddit [2], I posted about Triplit as well:

    > I think Triplit is a nice database and works as expected. It's data model fits well with what I have in mind (more decentralized/P2P instead of having a single centralized database as the source of truth), but there are 2 areas I find lacking:

    > - Server side/self-hosted: Triplit server requires a token for authentication. Triplit's documentation has a section about how to start the server without explicitly showing how to generate the token, which is mildly inconvenient. Therefore, on self-hosting the server, I opted for using their CLI command dev since the command has the token generation that I needed. I know it is not a good security practice as when the command is used as a system service, the token will be logged in plain text, but I have a bigger problem when someone can access that anyway.

    > - Query language: I find their custom query DSL not as expressive as a full-fledged query language (lacking UNIQUE and COUNT like SQL is on the top of my mind). You'll have to do a bit of data aggregating yourself.

    Recently, I found Evolu [3], which is quite similar to your project in terms of scope and functionalities as well. From a quick skim of their documentation, I think the differences are:

    - Triplit have `.subscribe()`, while Evolu don't

    - Evolu's querying is more familiar/advanced (typed SQL via Kysely)

    - In the browser, Evolu seems to use SQLite on top of OPFS, while Triplit uses IndexedDB

    I think there are more intricacies on the way Triplit differs from Evolu, so can you enlighten me on that?

    Really appreciate your comment! Thanks!

    - [1]: https://github.com/thanhnguyen2187/cryptaa

    - [2]: https://www.reddit.com/r/sveltejs/comments/1dndpj8/cryptaa_a...

    - [3]: https://www.evolu.dev/docs

  • by lars512 on 6/26/24, 5:03 AM

    Related question: when you're using databases with great offline sync protocols like this, how do you do schema evolution of your DB, especially in the face of different client versions that you cannot upgrade in lockstep?

    My context here is having worked in the past on a mobile health app, and recalling all the pain we had around this problem.

  • by Kiro on 6/26/24, 8:29 AM

    I don't understand in what apps it's acceptable for the client to be able to write to the database directly. Or how you can get away without any backend logic.

    I have the same questions about Supabase and Firestore so it seems like I'm missing something.

  • by armincerf on 6/25/24, 6:27 PM

    We’ve been using triplit to store user settings that can be managed by admins, it’s important users always feel like the app runs locally and they often don’t have good internet but we still needed sync as the users often switch devices and admins need to see and manage other users settings.

    Overall triplit has been really great, both as a frontend dx and also their support - whenever we find an issue or have a feature it gets handled very quickly by the team which is awesome!

    As soon as they have an answer for HA deployments we will be moving more critical data there instead of Postgres

  • by candiddevmike on 6/25/24, 5:55 PM

    Why did you choose to license this under the AGPL?
  • by satvikpendem on 6/25/24, 6:28 PM

    I believe I saw your video presentations on YouTube [0] from the Local First Discord server [1], so nice to see your Show HN here. I am not using TypeScript so I might not be your target audience, I'm using local-first especially for mobile apps where connections can be spotty unlike the web which by definition (at least on first load) is always connected to the internet, and I'm using Flutter for this use case, as well as a backend in Rust. Other local first solutions are generally agnostic to the client and server because they work directly on the database layer, ie ElectricSQL and PowerSync will sync the client and server database. Other solutions with CRDTs can also work on the client and server with some FFI, ie automerge is in Rust so in my case it'll work via FFI on the Flutter side via flutter_rust_bridge (or WASM on the web) and of course on my Rust backend server, but it looks like you are looking to offer a more classic client-server syncing solution instead needing conflict free resolutions on disparate clients, ie the server is the source of truth.

    So with all that, my question is, is there a reason you guys went with more of a full language level solution rather than being more agnostic to the client and server? It seems harder to support other languages and frameworks other than JS based ones in the future, but I suppose the market for that is already big enough. ElectricSQL etc also have SDKs for TypeScript as well as Flutter and others so they are similar to your solution but it seems like they can support more clients and servers in the future just by building SDKs for them.

    Another question, looks like you eventually want to compete with Supabase but they are already experimenting with database level syncing and CRDTs in Postgres [2] and might catch up with your solution, any thoughts on that?

    [0] https://www.youtube.com/playlist?list=PLTbD2QA-VMnXFsLbuPGz1...

    [1] https://localfirstweb.dev/

    [2] https://news.ycombinator.com/item?id=33931971

  • by curtisblaine on 6/25/24, 5:41 PM

    Since this is LWW, does it mean the amount of information on the client scale linearly with the number of operations? (meaning: the more users modify a database, the more the operations log grows)? Or do you do checkpoints? How does it scale space-wise when the user does millions of ops per day?
  • by munzman on 6/25/24, 10:01 PM

    would be great to have Rust bindings so it could work with Tauri. With tauri growth and the upcoming mobile devices support coupled with SQLite recent hype, this could bridge the gap and solve many problems for offline-first apps, thus become the go to solutions for many dev shops.
  • by tanishqkanc on 6/25/24, 7:22 PM

    I've been using Triplit on my React native app for a while and it works great. Highly recommend. It's the only local-first db that hits these points for me:

    - Good sane query language (not SQL) - Great typescript support - Offline support - React native support

    The cherries on top is that it's open source and self-hostable.

  • by sagarjs on 6/25/24, 6:31 PM

    So it’s not possible to use this with an existing postgresql database?
  • by arcticfox on 6/25/24, 4:39 PM

    This is really cool. Feels like the future of app development.

    But also I’m getting old, and I had the same feeling when RethinkDB came out. Do you guys have any thoughts on how your system compares to what they were doing and what eventually happened to them?

  • by kouohhashi on 6/26/24, 12:20 AM

    I used to use the meteo framework before, and I think if it's similar. By the way, is it possible to use MongoDB on the server side and Triplit on the React side? Or do I have to use a new database called Triplit?"
  • by alex_lav on 6/25/24, 7:19 PM

    This looks great! I would like to use this tool.

    I'm looking through the docs for more info on this line:

    > The server supports different storage adapters, such as SQLite

    What are the other storage adapters? I may just be blind, so if so please forgive me!

  • by syedmsawaid on 6/25/24, 8:24 PM

    Would this be pluggable to any backend? Like say, Ruby on Rails or ASP.NET?
  • by stevage on 6/25/24, 11:12 PM

    Looks very useful. I didn't dive deep, but don't understand yet where you define access controls? I mean, How you say that a given Users can only Access their own data.
  • by 8mobile on 6/26/24, 4:55 AM

    Wow, nice work. I will definitely try it out for my apps and backend. Do you have any benchmarks? Not sure about the AGPL licence
  • by terpimost on 6/25/24, 4:55 PM

    Pretty cool guys. It would be nice to know the differences to other local first like solutions out there.
  • by mrtesthah on 6/25/24, 6:12 PM

    What is Triplet’s syncing latency?
  • by pantulis on 6/26/24, 2:06 PM

    Remotely reminds of Joyent Slingshot. Oh, how times have changed.
  • by Jayakumark on 6/25/24, 4:38 PM

    Is there a pre-existing demo page.
  • by v4akukdX on 6/26/24, 6:09 PM

    Nice!
  • by danman114 on 6/26/24, 12:02 PM

    That's really interesting & cool - I'm gonna look into this for sure!

    I'd like to mention the Meteor.js framework (https://www.meteor.com/) too, which is in a bit of a transitioning phase right now to Meteor version 3, but is a really amazing full-stack app building solution I and many others have been working with for ~10 years.

    It has a lot of batteries included, build system, pluggable frontend frameworks, lots of libraries, based on node.js, meaning it's pretty much compatible with the Node.js environment.

    It's based on a really pretty simple syncing strategy for years:

    It's original client side data provisioning layer is based on having

    a) MongoDB on the Server and b) a javascript-native implementation of a subset of MongoDB in the Client.

    Using a publish / subscribe mechanism the client can subscribe to the subset of data relevant to his current view, eg. dependent on the current user & view.

    Updating is theoretically possible by using a syncing mechanism via writes into the client database and an elaborate allow/deny mechanism, but in practice most people use the following simple workflow:

    Meteor also provides a method-call-mechanism by which a client can call the server to do (or fetch) stuff. So that's basically RPC in a very simple but powerful format.

    These methods also allow for "client side simulations", optimistic updates to the client side database, with the changes getting rolled back / updated once the server part of the method has done it's updating of the database.

    So the workflow for working & updating data in the DB looks like this:

    - Data is "canonical" on the server DB

    - Client subscribes for the necessary data for its client side cache

    - When the user triggers an action, the client calls a method on the server, eg: "likePost(post_id)"

        - The client side simulation can actually increase the like count on the "Post" document in the local minimongo database
    
        - The server then executes the method & increments the like count in its database, if the request is valid
    
    - The client syncs its database after the method has completed to make sure its optimistic update lead to the same results as the server call did on the server.

    - The client updates its UI always reactively as soon as the data in its local db has changed.

    All of this is very performant as long as you keep it cool and subscribe only what's actually needed for the current view,

    Oh, and all of this has fine grained reactivity, on the client. The whole solution is really powerful, while deceptively simple.

    Having the same API on both client and server allows for "isometric" code, meaning code which runs on both the client and the server, so you don't have to have two different versions of helper code, which is really cool too.

    Meteor.js is pretty much a bit like "the old PHP experience" to me: As a full Stack developer I can write powerful apps with only one codebase which is shared in parts between client and server.

    Link to Pub/Sub docs: https://docs.meteor.com/api/pubsub Link to the Method docs: https://docs.meteor.com/api/methods

  • by troyjfarrell on 6/25/24, 5:42 PM

    Looks like it could be a more batteries-included/opinionated alternative to RxDB (https://rxdb.info). The relational queries might help some people who tend to think in SQL as opposed to documents (as in CouchDB or MongoDB) and the WebSockets for synchronization will help people get started more quickly. (RxDB provides interfaces for those who want to implement their own storage engine and/or synchronization backend.)
  • by kvonhorn on 6/25/24, 9:18 PM

    I used to work for a startup called TripIt 15 years ago. Took a double take when I saw your name.