Offline-first, multiplayer SQLite
In this interview I talk to Carl Sverre about his new project: SQLSync. It's an offline-first, collaborative wrapper around SQLite. We cover event sourcing, conflict resolution, VFSes, and more!
Everything we said
Aaron Francis
So today I'm talking to my friend Carl. Carl and I didn't ever work at the same company, but we worked together. Um, he used to be at, at a database company called SingleStore, and I wrote the SingleStore, uh, driver for Laravel, and so we got to work together a bunch. But these days, Carl's doing something extremely interesting, and so I wanted to talk to him.
So Carl, uh, welcome to this not show about SQLite. And tell us, uh, what you're up to and where you're at now.
Carl Sverre
Thanks, Aaron. Uh, super excited to be here. Um, yeah, so I mean, as Aaron said, I, I worked at SingleStore for 10 years. Um, SingleStore used to be called MemSQL, so it was a, a little database company. We started with this dream of making, um, you know, high performance combo of like analytics and transactions in the same system. Um, and I think we did a pretty good job. Uh, SingleStore, uh, after 10 years at SingleStore, I decided that, um, I really wanted to try some new things, still in the data space-
Aaron Francis
Mm-hmm
Carl Sverre
... but just doing my own thing. And so I decided to leave SingleStore and start, um, something new. Currently I am technically an entrepreneur in residence at Amplify Partners. Um, Amplify Partners-
Aaron Francis
Ooh, cool
Carl Sverre
... is a venture capitalist in San Francisco. Um, and I decided to take on this role because it allows me to pursue my own ideas for some, some amount of time. So it was sort of starting my own company without committing 100% into going right into, like, finding product market fit and finding, like-
Aaron Francis
Hmm
Carl Sverre
... that sort of company drive. Um, so it's sort of a, a wiggle room space. Um-
Aaron Francis
Yeah.
Carl Sverre
With Amplify, I have sort of started a company, I guess. Uh, it's called Orbiting Hail. Um, it's just me right now, so it's more just, like, me and a bunch of ideas, uh-
Aaron Francis
Mm-hmm
Carl Sverre
... around data and databases and web technologies. Um, and most recently I released a project, my first project, called SQLSync. Um-
Aaron Francis
Mm-hmm
Carl Sverre
... which is probably what we're gonna be spending most of the time talking about.
Aaron Francis
Yeah.
Carl Sverre
Um, SQLSync is basically a wrapper around SQLite. So it's completely vanilla SQLite. Nothing, nothing changed there. Um, and the wrapper basically, basically implements multiplayer SQLite. So you can have SQLite running in a bunch of different web apps, or the same web app, but in a bunch of different tabs or windows or whatever. Um, and they can all communicate with each other through SQLite as sort of a central database point of communication.
So that's sort of been the-
Aaron Francis
That's pretty cool. So let's- Uh, I do wanna talk, yeah, I mean, I do wanna talk about, uh, SQLSync a lot, but I, this entrepreneur in residence thing, that's super interesting. So you were, you were like, uh, my t- my perspective on you at SingleStore was you were, like, the mad scientist. Like, you knew- You knew, you knew everything.
I didn't ever really know what your job was, but you were like, "Oh, if there's a problem, you just talk to Carl. Carl will fix it." And so you came out of that, and how did you get hooked up with this, uh, this VC firm, and are you the only entrepreneur in residence over there?
Carl Sverre
Um, great, great questions. Yeah. So I started chatting with Amplify about two-ish years before I left SingleStore. Um-
Aaron Francis
Mm-hmm
Carl Sverre
... not, not with the intention of starting anything or leaving SingleStore. Um-
Aaron Francis
Mm-hmm
Carl Sverre
... it's just that while I was at SingleStore, as you said, I was a bit of a mad scientist. My- My last project-
Aaron Francis
I'm, I'm glad you agree with that. I'm glad, I'm glad you felt the same way.
Carl Sverre
Yeah. It was, uh, you know, everyone at startups ful- fulfills different roles. I was definitely in the-
Aaron Francis
Mm-hmm
Carl Sverre
... like, generalist space, where it was sort of, you know, if you don't know who to do something, you can always send it to Carl and he'll figure it out.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, that was sort of a role I really enjoy, uh, at, especially at a small, super agile company like SingleStore. Um, so, so yeah, the last project I had to work on at SingleStore was adding WebAssembly to SingleStore. So the idea that-
Aaron Francis
Hmm
Carl Sverre
... my team had was, what if we took WebAssembly and we allowed people to write WebAssembly programs and embed them directly in the database, um, as user-defined functions, table-valued functions, user-defined aggregates.
Aaron Francis
Cool.
Carl Sverre
So basically add extensibility to the database via WebAssembly.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, and so my team was working on that, which was super, super fun. And through that, I met Amplify, 'cause Amplify was really excited about WebAssembly. Um, so I met, um, Rene and Sunil through that sort of connection within the, like, WebAssembly space and, and everything. Stayed in, stayed in touch with them, and then when I eventually got to the point where I was like, "Oh, maybe I wanna do something new," um, I talked to pretty much everyone in my network, including Amplify.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, and Amplify sort of proposed this idea of doing an entrepreneur in residence. Um, it's not something that, uh, you know, every VC does, or they do with everyone, but for people like me, where I, I knew I wanted to do something, but I wasn't entirely sure what it was, I needed that-
Aaron Francis
Mm-hmm
Carl Sverre
... that space and that time to sort of figure it out, um, that's exactly what the entrepreneur in residence program is sort of designed to, to provide.
Aaron Francis
That is super interesting. It sounds, I mean, it sounds awesome. Um, 10 years, by the way, is forever. So I, I can understand that you, like, were looking for a new challenge after 10 years. I don't know if I've done anything for 10 years. Um, coming up soon, in, in December, I'll have been married for 10 years, so that- ... that counts. But, like, I don't know what my longest job is, maybe four or five years.
Um, so now you're out there. You're the, you're the entrepreneur in residence. You're out there. You, uh, you have created this thing, uh, called SQLSync, and you say it's vanilla SQLite, which I am intrigued, but there's some sort of wrapper around it, and it works in the browser, and I played with your demo for a little bit.
And so, like, give me the, give me the high level of, like, what the, um, I guess, what the use case is, um, and then the high level of how it's implemented, and then we can dive into some technical details.
Carl Sverre
Yeah. Yeah. So, so we'll start with sort of like the use case. Like, why, why would you want something like SQLSync? Um-
Aaron Francis
Mm-hmm
Carl Sverre
... my, my dream of SQLSync was to just make it really easy to make multiplayer web apps that specifically needed, like, a relational data model, right? So, like-
Aaron Francis
Okay
Carl Sverre
... there are many ways to implement multiplayer web apps. There's a lot of companies right now playing around with these ideas. Um, you know, like ElectricSQL, for example, which I'm pretty sure you know pretty well, 'cause I think they're-
Aaron Francis
Mm-hmm.
Carl Sverre
You're working with ElectricSQL right now, right?
Aaron Francis
I'm working with, uh, Turso, with lib SQL
Carl Sverre
With, with Turso. Sorry, sorry
Aaron Francis
Yeah, yeah, yeah
Carl Sverre
There's too many, there's too many SQL companies
Aaron Francis
I, there's a lot, yeah.
Carl Sverre
So you're working with Turso, but, uh, there's also Electric SQL which is doing, um, really similar things to-
Aaron Francis
Mm-hmm
Carl Sverre
... to do like, you know, uh, collaborative sort of, um, actually they're using sort of more like Postgres. Al- th- they also use SQLite under the hood. Uh, so there's a bunch of companies doing this. The idea was, can I make this just like super, super easy? That was like idea one.
Aaron Francis
Mm-hmm.
Carl Sverre
Idea two was, can I eliminate the, um, like can I, can I make the way in which multiplayer actually is implemented simpler to reason about? Uh, so that, that was sort of option two, and we can talk more about how I did that and like how I think I did that. I hope it's easier to reason about at least. Uh, and then, uh, so, so that was the two things.
And then option three was like, can I keep this as vanilla SQLite as possible? Like, can we make this-
Aaron Francis
Mm-hmm
Carl Sverre
... like very, very much just like it's just regular SQLite and then we're just doing something creative and, and sort of magical to make it multiplayer? Um-
Aaron Francis
Okay
Carl Sverre
... so it was sort of trying to combine those goals, uh, into something called SQLSync.
Aaron Francis
Okay. So this is great. I love this so much because when you're, when you're the, the host of the show, you get to ask all the questions, and I have so many questions 'cause I understand very little of what this is, what this is, and so this is like my learning opportunity right here, and it delights me. So let me ask you this. You, um, you talked a lot about like real time and collaboration.
Um, I'm not super familiar with something like Partykit. Is this in the same space of something like that? Or do you know what that is? Because I've seen a lot of people talking about like, "Add real time with Partykit." But I, a- as far as I understand, your distinction is relational database real time. Is that close?
Carl Sverre
Um, that's actually like a really, really good question, like a really insightful question, because we can sort of break up how you implement multiplayer for an app-
Aaron Francis
Mm-hmm
Carl Sverre
... into a couple different domains or, or like-
Aaron Francis
Okay
Carl Sverre
... different areas. Partykit satisfies the transport part of multiplayer. Basically, you can think of pol- Partykit as providing like a session backend that a bunch of-
Aaron Francis
Okay
Carl Sverre
... different clients can all connect to and can exchange messages.
Aaron Francis
Okay.
Carl Sverre
And then once you have a Partykit backend, you can host a data structure in that backend that all of these-
Aaron Francis
Okay
Carl Sverre
... different clients can sort of communicate and, and exchange information about.
Aaron Francis
Okay.
Carl Sverre
And so Partykit you can think of as essentially a, um, a sort of like a, a stateful serverless process that a bunch of different clients can all connect to without-
Aaron Francis
Okay
Carl Sverre
... having to pre-configure the existence of that server. So like if in traditional server architectures, I would spin up like my VPS or like my Amazon EC2 instance or, or whatever.
Aaron Francis
Mm-hmm.
Carl Sverre
Some instance that would be my server, and then all my clients would connect to it, right? So I'd have one big server for all my clients. Partykit sort of takes it one step further to say, well, actually, what if we have sort of like one server, but we make those servers dynamic so they come up whenever they're, they're needed, and they go away when they're not needed.
Aaron Francis
Okay.
Carl Sverre
And we associate each server with like a s- a concept of like a session.
Aaron Francis
Okay.
Carl Sverre
So if you wanted to use Partykit to implement like a chat, for example, you would implement-
Aaron Francis
Got it
Carl Sverre
... a party, which is essentially a durable session server. Uh, you would implement-
Aaron Francis
Silly, but yeah. Okay.
Carl Sverre
... a party for each of your different like chat rooms, right?
Aaron Francis
Okay.
Carl Sverre
And so everyone who's connected to the same chat room would be connected to the same durable sort of like dynamic serverless server.
Aaron Francis
And in that model, I know y- uh, uh, I know you don't write Partykit, but this is, this, you seem to know everything about it. Um, in that model, who owns the durable part? Is Partykit a SaaS? Or who... Like the serverless thing, someone has to orchestrate it. Who is that?
Carl Sverre
I, I will caveat, I may not know everything about Partykit. I probably make some mistakes. So if anyone on the internet-
Aaron Francis
That's great
Carl Sverre
... catches me on something, please let me know. Uh, you know, I'm, I don't-
Aaron Francis
They, they will. Don't worry.
Carl Sverre
Yeah.
Aaron Francis
They'll let you know.
Carl Sverre
But, but as far as I know, um, Partykit, uh, was based on or, or is based on Cloudflare Durable Objects. Um-
Aaron Francis
Got it. Okay
Carl Sverre
... I'm not sure if they have like other, other kinds of backends.
Aaron Francis
Cool.
Carl Sverre
But you can think of this durable object abstraction as like a, uh, the infrastructure that is necessary for something like Partykit to exist, right?
Aaron Francis
Okay.
Carl Sverre
And the reason why I sort of, um, know a bunch about it is because single, uh, sorry. As I say, I almost said single store.
Aaron Francis
Yeah.
Carl Sverre
Uh, it's been a couple of years and I still say it.
Aaron Francis
Yeah.
Carl Sverre
Um, SQLSync, um, uses durable objects as its infrastructure provider, right? For the exact same reason that Partykit does. Um, so-
Aaron Francis
Okay. And we're saying, when we say durable objects, we're saying capital D, capital O.
Carl Sverre
Yeah.
Aaron Francis
So Cloudflare Durable Objects.
Carl Sverre
Yeah, exactly.
Aaron Francis
Okay, cool.
Carl Sverre
So the Durable Object service at Cloudflare.
Aaron Francis
Got it. Okay.
Carl Sverre
Um, and S- SQLSync needs that same sort of functionality because basically the way that SQLSync works from a networking perspective is that we have a bunch of clients, so we have a bunch of different, like let's say that you and I are working on an app together-
Aaron Francis
Sure
Carl Sverre
... using SQLSync as our multiplayer system, right?
Aaron Francis
Okay.
Carl Sverre
So the to-do app, which I guess we can like link somehow in when you post.
Aaron Francis
Yeah, I'll, I'll put it in some notes somewhere, yeah.
Carl Sverre
Uh, the to-do app is a good example. So let's say we're both using this to-do app, so it's a shared to-do list. Um, what's happening that, what, what SQLSync does is it uses Cloudflare Durable Objects to allocate a durable object that is for the database that backs that to-do list that we're both working on.
Aaron Francis
Okay.
Carl Sverre
And so our two web browser tabs, for example-
Aaron Francis
Mm-hmm
Carl Sverre
... are not communicating directly with each other like peer to peer.
Aaron Francis
Mm-hmm.
Carl Sverre
It's communic- they're both communicating with that central durable object as being, that's sort of what, what we call in SQLSync terminology, I call it like the authoritative version of SQLite. Like it's, it's the authoritative database.
Aaron Francis
Okay. So in other terms, would source of truth be right?
Carl Sverre
Yeah.
Aaron Francis
Okay. So, uh, let me say it back to you to make sure I have it. Um, so it sounds like Partykit is similar but different. It sounds like Partykit, um, primarily ... sends and receives messages. Is that close enough, you think?
Carl Sverre
I think that's, that's a more, or, or rather, like, PartyKit is more general than, than SQL Sync-
Aaron Francis
Okay
Carl Sverre
... in the sense that you, you take PartyKit, and that provides a nicer and, like, easier to use thing than Cloudflare Durable Objects for-
Aaron Francis
Okay
Carl Sverre
... initializing these, like, shared objects and making it easy to connect to them and managing all of that, and then you sit your app on top of that, and you need to provide your own data structures and your own message types, and you need to provide your own-
Aaron Francis
Got it
Carl Sverre
... conflict management and all of these, these different aspects of multiplayer.
Aaron Francis
Okay.
Carl Sverre
SQL Sync-
Aaron Francis
Okay
Carl Sverre
... SQL Sync technically could be built on top of PartyKit, I guess is a good way to say it.
Aaron Francis
Got it. Okay. So then, um, maybe it's more accurate to say, uh, PartyKit is a little bit lower on the abstraction tree, and SQL Sync is a little bit higher and handles more for you. Is that right so far?
Carl Sverre
Yep. Yeah.
Aaron Francis
Okay. So then on the-
Carl Sverre
It's, like, more, more opinionated, basically.
Aaron Francis
Okay. Then on the SQL Sync side, you also, um, rely on a durable object as the authority or the source of truth, and then presumably, um, you've got, uh, SQLite databases in the browser that's doing some sort of, uh, local-first stuff and then, uh, coordinating with the source of truth to then have that be distributed to the other browser or browsers.
Is that-
Carl Sverre
Yep
Aaron Francis
... is that close?
Carl Sverre
Yeah, exactly. So, so-
Aaron Francis
Okay
Carl Sverre
... now it comes down to the question of, like, how do you do that coordination? And that's-
Aaron Francis
Hard
Carl Sverre
... that's a big aspect-
Aaron Francis
Hard.
Carl Sverre
... of the multiplayer space.
Aaron Francis
Yeah. Okay. So I think I have a little bit of the lay of the land architecture stuff. Um, so how do you do that coordination? Like, that sounds like, honestly, um, this whole, like, local-first discussion that has been going on on Twitter that is just very, it's very tiring, but local-first seems really hard because then you have, uh, you have two potential sources of truth, and maybe N, many, if, you know,
a bunch of people are connected and writing stuff to the database. Um, then how do you do the conflict resolution? I know there's, like, proper, you know, computer science algorithms for it, but tell me from a, an accounting major, how do you do conflict resolution? That seems really hard.
Carl Sverre
Yeah. It, it is super, super hard. Um, so there's, there... Oh, man. There's so many different ways to address- ... this topic, and so feel free to, like, stop me and, and ask me-
Aaron Francis
Okay
Carl Sverre
... clarifying questions as I go, 'cause it's a pretty complicated, and I would... It's not even just complicated. There's a, there's a lot that is involved with conflict-
Aaron Francis
Mm-hmm
Carl Sverre
... handling, right? Um, so the first thing that I think is important to, uh, to talk about is in, in my mind, I split up conflict management into, like, two categories, um, like, two sort of broad categories, and I call them... I don't know if this is the, the best term for it, but it makes sense to me, so I'll, I'll use it to explain it.
Aaron Francis
Mm-hmm.
Carl Sverre
I call them, like, physical conflict handling and logical conflict handling.
Aaron Francis
Okay.
Carl Sverre
So physical conflict handling is conflict handling that ensures that the data structure that everyone sees-
Aaron Francis
Mm-hmm
Carl Sverre
... is not corrupted.
Aaron Francis
Okay.
Carl Sverre
So-
Aaron Francis
You said, you said physical conflict handling ensures that the data structure that everyone sees is not corrupted.
Carl Sverre
Yes.
Aaron Francis
Okay.
Carl Sverre
So I'll give you, like, a, a simple example of that. Um, let's... Okay, wait. Let me actually think of a good, simple example.
Aaron Francis
Yeah, yeah. Make it simpler. Make it simpler.
Carl Sverre
Yeah. Uh, man, like, all the simple cases are, like, so simple that they're, that they're almost not that instructive. Uh, but-
Aaron Francis
Does, does... Let me ask you a clarifying question. Um, does structure, you, you were, you were, um, careful to say the data structure is not, uh, corrupt. Does that stand opposed to something else? Is it the data that you count as logical conflict? So physical is structure and logical is data?
Carl Sverre
Yeah, sort of. So, so I guess a, a way to say this is, like, for example, let's say that we have a, um, like a set. A set is-
Aaron Francis
Okay
Carl Sverre
... it's, like, a list without an order. We don't care about the order.
Aaron Francis
Mm-hmm.
Carl Sverre
We just care about the contents of the ses- set.
Aaron Francis
Okay.
Carl Sverre
Uh, so, like, let's say we have a set data structure, and that data structure, um, is, like, if you're using, like, JavaScript and you're just doing, you know, basic web app programming, it's probably just represented as, like, a JavaScript array, right? Or, or a JavaScript-
Aaron Francis
Yeah
Carl Sverre
... map, depending on how you manage your set.
Aaron Francis
Okay.
Carl Sverre
Um, the super simple data structure, and, like, if we are, uh, if we are trying to get to a consistent view of that set, so, like, you and I have two different versions of the set and we wanna get to, like, the same version of the set, there can be con-
Aaron Francis
Okay. Can we, can we make it concrete? Can we say I have some items and you have some items?
Carl Sverre
Yep.
Aaron Francis
Okay. So Aaron has, um, items A and C, and Carl has items A and B. Is that a fair starting point?
Carl Sverre
That's a great starting point. So I have AB-
Aaron Francis
Okay. So I have A and C.
Carl Sverre
You have-
Aaron Francis
You have AB. Carry on.
Carl Sverre
So given that sort of starting state, um, physic- Like, when I talk about physical sort of correctness, let's say that, uh, you s- like, just we're gonna implement, like, a very simple, uh, set algorithm.
Aaron Francis
Okay.
Carl Sverre
You're just gonna set, send your whole set to me.
Aaron Francis
Okay.
Carl Sverre
So I have AB, and you have AC. So you send me A, AC, and I look at my two sets. I have AB and AC.
Aaron Francis
Mm-hmm.
Carl Sverre
And I, like, merge them together, right?
Aaron Francis
Okay.
Carl Sverre
And now I have ABC. I see the, the full set of both of our-
Aaron Francis
Yep
Carl Sverre
... our sets. Um, there are two things that are happening in that merging, though, that's like-
Aaron Francis
Yeah
Carl Sverre
... that's very subtle. One thing that's happening is, like, there's an actual, like, data structure manipulation, where I'm manipulating the, the JavaScript data structure, which is like I have an array on the left side, I have an array on the right side. I'm iterating through the array and, like, checking to see which objects-
Aaron Francis
Mm-hmm
Carl Sverre
... are not contained and m- moving them in, right? There, there are physical operations happening And then there's a logical operation, which is when I'm merging these two things, I don't wanna end up with A, B... A, A, B, C. I don't wanna end up with that.
Aaron Francis
Right.
Carl Sverre
I wanna end up with-
Aaron Francis
Okay
Carl Sverre
... A, B, C because it's a set.
Aaron Francis
Okay.
Carl Sverre
So that's what I mean when I'm talking about physical and logical. Physical is, like, the actual act of manipulating the data structure.
Aaron Francis
Uh-huh.
Carl Sverre
And logical is, like, the act of enforcing business logic.
Aaron Francis
Got it. So let me see if I got this. Um, physical would be putting the two sets together. Logical would be looking at that result and saying, "Hey, AA is nonsense." Those were similar items in the discrete sets, and when we put it together physically, potentially we got A, A, B, C. But then we add the logical part and it's like, "Come on, y'all, A, B, C.
Let's do A, B, C."
Carl Sverre
Exactly.
Aaron Francis
Is that right?
Carl Sverre
Yeah, that's exactly right.
Aaron Francis
Okay.
Carl Sverre
So, okay, so, so that's, that's, in my mind, the distinction between, like, physical and logical. And-
Aaron Francis
Okay. Love it
Carl Sverre
... in that example, it's, like, so simple that it's, it's almost, like, not that interesting. But let's talk about a relational database. So I think-
Aaron Francis
Yeah
Carl Sverre
... probably most people watching, uh, your, your, your shows and your, you know-
Aaron Francis
Mm-hmm
Carl Sverre
... your channel and stuff would... are familiar with, like, relational databases.
Aaron Francis
Mm-hmm.
Carl Sverre
But generally you have tables, um, which are basically like sets, interestingly enough.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, and you have tables, and one really common thing that, that exists inside relational databases is like a foreign key. Uh, so you have-
Aaron Francis
Mm-hmm
Carl Sverre
... table A and table B, and, like, a row in table A references a key of a row in table B.
Aaron Francis
Okay.
Carl Sverre
Right? So that's a foreign key relationship. Um, when we are, like, when we are implementing a multiplayer system for something like a relational database, we have to ensure that when, when a client... So, like, let's say that I have my version of the database, you have your version of the database.
Aaron Francis
Sure.
Carl Sverre
When we are manipulating these two databases, we have to ensure that two things are, are true. One is that my, my physical database never becomes corrupted. Like, if, if it's SQLite, SQLite has a internal file, file format, storage-
Aaron Francis
Right
Carl Sverre
... format, um, that's quite sophisticated. There's, like, B-trees, there's a lot of internal pointers. There's a lot of-
Aaron Francis
Mm-hmm
Carl Sverre
... uh, like, pretty low level sort of things that have to be correct, otherwise SQLite will just be like, "I don't know what this is." Like, it's just a bunch of-
Aaron Francis
Yep, you mucked it up. I can't open it. Game over.
Carl Sverre
So, so this is the physical side of rep- of, of, like-
Aaron Francis
Okay
Carl Sverre
... multiplayer. We have to make sure that no matter what happens, that whenever you or I look at our database, it's always in a correct and consistent state from a physical-
Aaron Francis
Okay
Carl Sverre
... perspective.
Aaron Francis
Seems important.
Carl Sverre
That's, that's state, that's sort of the first aspect of multiplayer. The second aspect of multiplayer is we also want to ensure the logical correctness, i.e.-
Aaron Francis
Mm-hmm
Carl Sverre
... if I have a foreign key that points at, like, something else and that thing doesn't exist, um, that's, like, a logical error, for example.
Aaron Francis
Yep. Okay. Love it.
Carl Sverre
And so going back to, you know, the original, the, the source of this question is just, like- ... how does SQL Sync work and how do-
Aaron Francis
Mm-hmm
Carl Sverre
... like, how do you implement multiplayer and data structures? Um, the reason why I went down this, like, very long tangent now that I, like, think about it, is that-
Aaron Francis
Hey, listen. We got all the time in the world. And this is great so far, so don't be shy.
Carl Sverre
Okay, cool.
Aaron Francis
Keep going.
Carl Sverre
So the reason I went down this tangent is because I actually think that it's very important when you're reasoning about multiplayer systems-
Aaron Francis
Mm-hmm
Carl Sverre
... to think about these two things as, like, separate things that both have to exist.
Aaron Francis
Okay.
Carl Sverre
But you need to, like, be able to reason about them separately, because achieving physical consistency may not result in achieving logical consistency.
Aaron Francis
Right.
Carl Sverre
And so you may need, like, another layer on top to, like, achieve that logical consistency. Okay.
Aaron Francis
Okay.
Carl Sverre
So that, that, that's aside number one.
Aaron Francis
Mm-hmm.
Carl Sverre
So-
Aaron Francis
Ah, there's more.
Carl Sverre
So, so now, uh, I will talk about how... I think the next, the next logical thing to do is talk about how SQL Sync actually does the replication aspect, like the multiplayer aspect.
Aaron Francis
Please.
Carl Sverre
And then I think probably from there, there might be some interesting things we could talk about, about, like, other systems and how they differ from SQL Sync.
Aaron Francis
Great. Love it.
Carl Sverre
So, so SQL Sync, um, the, again, going back to, like, my original goal of SQL Sync, I wanted to make something that was hopefully easy to reason about. Now, any multiplayer system is not easy to reason about, but-
Aaron Francis
Sure
Carl Sverre
... I wanted to try to keep it, like, as simple as possible. Um, so SQL Sync models the distributed state as, um, every single, every single... So I'm gonna, let's, let's give things terminology so we can talk about them 'cause-
Aaron Francis
Mm-hmm
Carl Sverre
... I don't have many slides to show. So this is just-
Aaron Francis
Yeah
Carl Sverre
... gonna be, like, in our head. Um, so we're gonna call the code that is running inside, like, a web browser tab.
Aaron Francis
Okay.
Carl Sverre
We're gonna call that the SQL Sync client.
Aaron Francis
Great.
Carl Sverre
So the client is gonna be, like, the code that's running, like, on your computer.
Aaron Francis
Okay.
Carl Sverre
And then we're gonna call the code that's running in the Cloudflare Durable Object-
Aaron Francis
Mm-hmm
Carl Sverre
... the SQL Sync coordinator.
Aaron Francis
Coordinator. Cool. Love that. Great name.
Carl Sverre
Right? So we have a client-
Aaron Francis
Yeah
Carl Sverre
... we have a coordinator. And-
Aaron Francis
Mm-hmm
Carl Sverre
... the goal of the client is to provide-
Aaron Francis
And can I pause real fast? Um, you could have potentially n many clients connected to one single coordinator.
Carl Sverre
Exactly.
Aaron Francis
Okay. Carry on.
Carl Sverre
Um, and actually I'll clarify even one more thing, which is every coordinator maps one-to-one to a SQLite database.
Aaron Francis
Every coordinator maps... Okay, so every coordinator has one SQLite database that is the authority.
Carl Sverre
Yep.
Aaron Francis
And that is part of the coordinator's purview, is keep the authority, and then all of the clients connect to that coordinator that is sitting next to a single SQLite database.
Carl Sverre
Yes, exactly. So-
Aaron Francis
Or has it within it or whatever
Carl Sverre
... if you, like, as a, as a web browser tab, like, let's say that you wanted to open two different SQL Sync databases.
Aaron Francis
Mm-hmm.
Carl Sverre
In this case, you would be connected to two different SQL Sync coordinators.
Aaron Francis
Got it. Okay.
Carl Sverre
Each coordinator manages one SQLite databases, basically.
Aaron Francis
Cool. Love it. All right. Perfect. Keep going.
Carl Sverre
Cool. So, uh, so we have this coordinator up in the cloud somewhere, some serverless magical thing. Um-
Aaron Francis
Yes
Carl Sverre
... and we have our, our local client, and then both you and I have a local client because we're, like, let's say going back to the to-do list thing, we're both working on the same to-do list. Um-
Aaron Francis
Yeah
Carl Sverre
... that means that we're both connected to the same coordinator. Now, SQL Sync, uh, we'll, we'll just talk about, like, the basic replication flow. So the first thing that happens is, like, let's say that I add a to-do in my local SQLite database.
Aaron Francis
Let's make it groceries. We're sharing a grocery list.
Carl Sverre
Sure.
Aaron Francis
We're, we're best friend roommates. So you add milk. Great.
Carl Sverre
Great. I add milk to my, my local SQLite database.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, so the first thing that happens is that SQLite just straight up commits that and just shows me right away. Like, no network connection at all has happened. Um-
Aaron Francis
Okay. So in, in the client, in the actual browser, you've got a SQLite database that has milk written into it.
Carl Sverre
Bingo.
Aaron Francis
Okay.
Carl Sverre
So, so SQL Sync's first and primary goal is committing that to the local state, like thoroughly-
Aaron Francis
Mm-hmm
Carl Sverre
... on the local, uh, client, and then immediately re- showing that to the c- to the, uh, to the code so that the client can render it.
Aaron Francis
Okay.
Carl Sverre
Um, so there's no need to have any kind of like, you know, in a lot of, like, front end, like React code, for example-
Aaron Francis
Mm-hmm
Carl Sverre
... or, or View code, you would have your, your like, your database state over here, and then you'd have, like, your client-side cache code, right?
Aaron Francis
Yeah.
Carl Sverre
So the whole idea is we eliminate that client-side cache. You just, you can, you can synchronously query SQL Sync and get back that milk right away.
Aaron Francis
Okay. So you have in this, in this architecture, you have achieved, uh, local first in that you hit, you type in M-I-L-K, Enter, boom. It's immediately in the UI. Now, it could, and we haven't gotten there yet, but it could take a second to get out to Cloudflare and to the other person.
Um, and if that fails, presumably you do something in the local UI, but local UI is immediately updated, so you have achieved this panacea of, of local first that everyone is after. Is that good so far?
Carl Sverre
Yep. That's exactly right.
Aaron Francis
Okay. All right. Carry on.
Carl Sverre
Sure.
Aaron Francis
What happens next?
Carl Sverre
So, so now SQL Sync's happy. It's like, "All right. Great. We've committed this thing." Um, but it does, it does it in a little bit of a tricky way, and this is where the first aspect of where SQL Sync gets smart.
Aaron Francis
We love, we love trickies. Yes.
Carl Sverre
Yeah.
Aaron Francis
This is great.
Carl Sverre
So the SQL Sync uses regular SQLite, but SQL Sync-
Aaron Francis
Okay
Carl Sverre
... tricks SQLite.
Aaron Francis
Yep.
Carl Sverre
And what it does is it, uh, what I did is I implemented a, a VFS. Uh, VFS stands for-
Aaron Francis
Yeah, you did
Carl Sverre
... like, uh, virtual file system.
Aaron Francis
Yeah.
Carl Sverre
And so when SQLite goes and tries to, like, write to the disk, it thinks-
Aaron Francis
Uh-huh
Carl Sverre
... that it's, like, writing to, like, a normal file system.
Aaron Francis
Mm-hmm.
Carl Sverre
But my VFS code tells SQLite that it's like it, it handles all the writes on behalf of SQLite and it-
Aaron Francis
Mm-hmm
Carl Sverre
... it just does some magical things and it tells SQLite, "It's all good. Like, you're good. We have the right, you know, you can read it. It's all good." Um-
Aaron Francis
Okay. So let me, let me pause there for, for people who aren't in the internals as much as you are. Um, the, the architecture of SQLite is such that, um, you know, there's, there's a bunch of stuff on top, virtual machine. When you finally get to the bottom, it has to write to the disk. It's pretty important. Um, and the way that that is implemented in SQLite, to my understanding, is through this, there's this abstract layer called the VFS, and then there are two concrete implementations, one for Unix system, one for Windows systems,
and then everybody has their own. Um, three, what's the third?
Carl Sverre
Memory.
Aaron Francis
Memory. Yes. Okay. Perfect. Um, so you have these three that are, uh, concrete from DRH himself, SQLite guy. Um, but you can totally write your own VFS, which is what I thought Lightstream was, but that's not the case. Um, so you have written, you have written some sort of, uh, VFS that conforms to the interface, so SQLite is happy to hand you data and trust that your VFS is good, and you do something with that
data. And so this is kind of like one of the ways that you can modify SQLite is you just kind of sneak in there and you're like, "I'm writing to the disk now. Give me all the data." Is that right? Hey, I hope you're enjoying this. Just as a reminder, you can learn more about SQLite at highperformancesqlite.com. I'll get out of your way.
Back to the interview.
Carl Sverre
Yep, exactly.
Aaron Francis
Okay.
Carl Sverre
So, uh, so my VFS presents, like if, uh, just like a little aside on the VFS stuff 'cause-
Aaron Francis
Yeah
Carl Sverre
... some of your viewers might find this, this sort of cool. Um, so SQLite VFS looks like a sort of, uh, when you implement it, you're basically implementing, like, a Unix file system abstraction. Like you have-
Aaron Francis
Mm-hmm
Carl Sverre
... the ability to, like, create files, delete files, um, truncate files.
Aaron Francis
Read and write. Yeah.
Carl Sverre
I don't even think you can rename files. It's a very simple file system abstraction. And then once you have the file handle, so like you give SQLite, like, this file handle thing, which it just thinks that it's this, like, opaque blob, um-
Aaron Francis
Mm-hmm
Carl Sverre
... and this file handle has all your normal file system operations, like, you know, writing to the file, reading from the file, and so on, right? So, uh, it's a very simple abstraction. And so what, what the SQL Sync VFS does is it actually internally maintains, um, this l- it's like a, a, like, it's like a layered file system basically.
Aaron Francis
Okay.
Carl Sverre
So it, it maintains what, what it calls the, like, snapshot, which is the-
Aaron Francis
Mm-hmm
Carl Sverre
... which is a snapshot of the SQLite database that has been fully committed on the authoritative coordinator.
Aaron Francis
Okay. So, ooh, that's good. Okay. So hang on. So the VFS knows somehow through communication with the coordinator, um, it has some sort of high watermark of this has been committed into stone and you can trust it.
Carl Sverre
Yes. So-
Aaron Francis
And then beyond that is, like, the pending or something.
Carl Sverre
Exactly. So, so, like, if you think about the first time you, like, load the webpage. So you load the webpage-
Aaron Francis
Mm-hmm
Carl Sverre
... like the grocery list app. It has no database, but let's say that we already have, have been using this database for a while, so it has a bunch-
Aaron Francis
Mm-hmm
Carl Sverre
... of grocery list items. The first time I load that app on my, like, phone, for example, it's gonna download that SQLite snapshot, boom.
Aaron Francis
Mm-hmm.
Carl Sverre
Locally. So it has a full copy of the database as of some, like, known authoritative
Aaron Francis
Okay
Carl Sverre
... and so that's, that's its snapshot, and that's where it's gonna start from. And then when I go into the app and I say, like, create new grocery list item, add milk, it's going to internally, the VFS, like, when SQLite goes and writes pages into the file, so it tries to edit-
Aaron Francis
Mm-hmm
Carl Sverre
... the SQLite file, it's going to keep those edits to the file as, like, a separate layer. It's gonna present to SQLite, like, as Squ- when SQLite looks, it's gonna just see one file that's just contiguous.
Aaron Francis
Mm-hmm.
Carl Sverre
But internally, I sort of split it out into snapshot and then, like, all the edits, like local edits, basically.
Aaron Francis
Okay, hang on. So you have your snapshot that you can trust, and then somebody writes eggs to the to-do list or the, the grocery list. Do you write-- Are you keeping track of, like, do you go ahead and write that, um, to the SQLite database just in like a normal form and then write it aside to a pending changes file?
Or is there some way in the SQLite database that you mark it as not committed or pending or whatever?
Carl Sverre
So w- I do store some, some information about the transaction that was executed, and-
Aaron Francis
Mm-hmm
Carl Sverre
... we'll talk about that as well. But intern-internally, like, on the file system abstraction side, the file system just keeps, like, the last authoritative snapshot, which is like the, the base version of the file, and then it just keeps track of, like, all the edits to the file. And these are just, like-
Aaron Francis
Okay
Carl Sverre
... raw edits to, like, the bytes of the file. It doesn't even know what those edits are. It just treats-
Aaron Francis
Got it. Okay
Carl Sverre
... it like opaque changes to the file. Um-
Aaron Francis
So it's almost like a bin log that's... Is that right?
Carl Sverre
It's not even like a log because it just, it, it simply has, like, two layers. It just has, like, snapshot, which I, which is that, like, you know, version one.
Aaron Francis
Uh-huh.
Carl Sverre
And then it has, like, all the changes to that, to that version as-
Aaron Francis
Okay
Carl Sverre
... just, like, essentially like a sparse page map, essentially.
Aaron Francis
Okay. Okay. All right. Keep going. Keep going.
Carl Sverre
Okay. But-
Aaron Francis
All right
Carl Sverre
... the reason that SQLSync does this, and, like, the value of this, like, why, why is this valuable? It allows SQLSync to instantaneously roll back to the snapshot. Like, all SQLSync does-
Aaron Francis
Yeah. Okay
Carl Sverre
... is drop this, like, layer of-
Aaron Francis
Yeah
Carl Sverre
... like, all your local changes, and it's instantaneously identical to the original snapshot from the server.
Aaron Francis
Yeah.
Carl Sverre
Right?
Aaron Francis
Okay.
Carl Sverre
And this is a very cool property that we're gonna need for, like, the la- a later point. So-
Aaron Francis
Okay. Love it. That's good foreshadowing. I love that, yeah.
Carl Sverre
Okay.
Aaron Francis
When are we gonna need it? All right, keep going.
Carl Sverre
So, so what we've, what we've established so far is, like, when we add milk to our grocery list-
Aaron Francis
Mm-hmm
Carl Sverre
... uh, we, we are doing regular SQLite stuff. SQLite just runs the transaction just like normal. SQLite sees, like, a normal file system and writes the change to the normal file system like nothing has changed from SQLite's perspective.
Aaron Francis
Mm-hmm.
Carl Sverre
And all we've done internally is we've just sort of tracked, this was like the last version of the database that we, like, know is fully consistent and shared with the authoritative state, and then here's-
Aaron Francis
Mm-hmm
Carl Sverre
... all, like, the local changes that have happened. So now the question is: what's next? So what's next is, um, let's talk about transactions.
Aaron Francis
Mm-hmm.
Carl Sverre
So regular, like, relational transactions, you know, you have, like, your begin insert one row, insert two rows, you know, select whatever, commit, right? This is, like, how relational databases work. Um, SQLSync has a transaction system that it forces you to use. So one thing about-
Aaron Francis
Okay
Carl Sverre
... SQLSync that is weird is you can't just write directly to SQLite. Like, it doesn't give you, like, a normal, like, SQLite connection handle that you can just use and run an insert command.
Aaron Francis
Okay. That's interesting.
Carl Sverre
Instead, it forces you to... And this is a design decision that's specific to SQLSync. Uh, it's not necessarily the perfect design decision, and it's something I'm gonna change in the future, but let's just say what it does right now.
Aaron Francis
Okay.
Carl Sverre
Um, what it does is it forces you to write all of your transactions, uh, in WebAssembly.
Aaron Francis
It forces you to write all of your transactions in WebAssembly. It forces who to write their transactions?
Carl Sverre
You as in the developer of the app.
Aaron Francis
Okay.
Carl Sverre
So as a developer of an app who uses SQLSync-
Aaron Francis
Mm-hmm
Carl Sverre
... you write what SQL, SQLSync calls, like, the mutator, and-
Aaron Francis
Okay
Carl Sverre
... the mutator is essentially a WebAssembly blob, like a blob of WebAssembly-
Aaron Francis
Mm-hmm
Carl Sverre
... code, which you can compile from Rust or other languages that support WebAssembly. Um-
Aaron Francis
Okay
Carl Sverre
... you write some, some code, you compile it to WebAssembly, and this blob implements an API. And on the, like, on the input side of the API, the, the side that your app sees-
Aaron Francis
Okay
Carl Sverre
... essentially it looks like almost like a normal web API. Like, if you think about implementing, like, a REST API or, like, a GraphQL API-
Aaron Francis
Mm-hmm
Carl Sverre
... um, on, like, the input side, it's just a bunch of handlers. So for example, in this grocery list app, we would have like, like, add grocery item, remove grocery item. You know, all of our, all of our, um, any type of endpoint in our app-
Aaron Francis
Mm-hmm
Carl Sverre
... that is changing the state of the system.
Aaron Francis
Okay. Okay. I think that makes sense. Um, so if I'm the developer and I'm writing the add grocery item endpoint in air quotes, what am I talking to if not a raw SQL connection or raw SQLite connection?
Carl Sverre
So I, I guess I, I was a little bit overly aggressive about that. Inside your WebAssembly code, you get, like-
Aaron Francis
Mm-hmm
Carl Sverre
... a completely normal SQLite connection.
Aaron Francis
Got it. Okay. Okay.
Carl Sverre
It's just that you have to-- You can only access that SQLite connection from inside this WebAssembly object.
Aaron Francis
Got it. Okay. So you have this concept, this, this firewall concept where, um, this, what did you call it? The mutator?
Carl Sverre
The mutator, yeah.
Aaron Francis
The mutator is allowed to talk to the SQLite database, whereas the application code must talk to the mutator.
Carl Sverre
Exactly. And so it, it-
Aaron Francis
Okay
Carl Sverre
... like, if you actually look at your-- When, when you're done with building your app on top of SQLSync, when you look at the code, it sort of looks like all your mutations are just, like, hitting, like, a web endpoint Very similarly to how-
Aaron Francis
Cool
Carl Sverre
... they would look if you were using, like, a regular web server.
Aaron Francis
That's kind of cool. Okay.
Carl Sverre
So maybe I need, need to name these things differently. I think the mutator thing is sort of too taut.
Aaron Francis
I, I don't know. I mean, there's a lot to learn, and I'm, I think I'm following so far.
Carl Sverre
But-
Aaron Francis
So I think we're doing okay. So ke- carry on.
Carl Sverre
Yeah. So, so basically we have this mutator, which you can think of as just like, like a really lightweight API server, because it's-
Aaron Francis
Mm-hmm
Carl Sverre
... this WebAssembly blob, and it can run, like, because it's WebAssembly, we can actually just run it directly inside of the client. Um, so it's almost like we've brought, like, our API layer, the part of our API layer that is able to mutate the database, it's almost like we've-
Aaron Francis
Mm-hmm
Carl Sverre
... teleported that down into the client. Um-
Aaron Francis
Okay
Carl Sverre
... so-
Aaron Francis
Why must it be in, in Wasm?
Carl Sverre
It doesn't really have to be in WebAssembly, uh, in terms of, like, all of the ways I could build this. Um-
Aaron Francis
Uh-huh
Carl Sverre
... the reason why I chose WebAssembly is a couple of things. The first thing is that the mutator logic has to run on both the server and the client, and we'll talk about that in a little bit. But basically, like-
Aaron Francis
Both the coordinator and the client?
Carl Sverre
Yes. The, it runs-
Aaron Francis
Okay
Carl Sverre
... on the coordinator and runs onto the client. Um, and so we needed a portable, like an-
Aaron Francis
Oh
Carl Sverre
... easily portable sort of abstraction for this, like-
Aaron Francis
Okay
Carl Sverre
... arbitrary code that the developer-
Aaron Francis
Mm-hmm
Carl Sverre
... is writing. Um, I could have used JavaScript. I think that would be, like, the other logical choice, 'cause we can run JavaScript on the client, we can run JavaScript-
Aaron Francis
Sure
Carl Sverre
... on the server. But the reason why I went with WebAssembly is because JavaScript is harder to bundle in, like, a really clean and, like, sort of, uh, sanitized way.
Aaron Francis
Boy, isn't it? Okay. That makes, that makes a lot of sense. Okay. Cool.
Carl Sverre
Whereas WebAssembly's sort of naturally bundleable. Like, you get, like-
Aaron Francis
Right
Carl Sverre
... one WebAssembly object. Like, you're done. That's, that's-
Aaron Francis
Mm-hmm
Carl Sverre
... the bundle. And I, I know that I don't have to worry about sandboxing, I don't have to worry about anything. It just is this nice, like, opaque blob that I can treat as an API.
Aaron Francis
I, I don't wanna, I don't wanna jump ahead, but I think I see where this is going if, if, if the mutator must live in the coordinator as well. But, um-
Carl Sverre
Yeah
Aaron Francis
... all right. So keep going. So now we have, we've got a, a, a SQLite database in the client. Um, we have this firewall that is this WebAssembly, uh, mutator, and then on the application, uh, side, the application code speaks to the WebAssembly thing as if almost as if it's an API.
Um, but it's all, it's all right there, it's all local, it's all in the client. And then that mutator talks directly to the SQLite database.
Carl Sverre
Yep.
Aaron Francis
Okay.
Carl Sverre
So, uh, if we sort of visualize what that code looks like in the client. So I go click on, like, the add grocery button. I type in, like, milk. I hit enter.
Aaron Francis
Mm-hmm.
Carl Sverre
That is literally going, like in my React code or my View code, it's gonna be-
Aaron Francis
Mm-hmm
Carl Sverre
... calling that mutator, which is like add grocery item mutator in my WebAssembly code.
Aaron Francis
Mm-hmm. Mm-hmm.
Carl Sverre
And that internally is going to run a transaction against SQLite. It's going to mutate the database and, and that's what happens, like, locally.
Aaron Francis
Okay.
Carl Sverre
So then what SQL Syncdb does, and this is the fun part, is that we-
Aaron Francis
Okay
Carl Sverre
... record, in a log, we record that we ran add grocery, like the add grocery mutation.
Aaron Francis
Mm.
Carl Sverre
And all we have to store is essentially the name of the mutator in the WebAssembly object.
Aaron Francis
Mm-hmm.
Carl Sverre
And then all the arguments to the mutator.
Aaron Francis
Mm-hmm.
Carl Sverre
Right? So we just have this relatively, like, lightweight log, and this log actually could be po- potentially represent very large changes to the database. Like, you could have a mutator that said, "Delete everything." You could have a-
Aaron Francis
Mm-hmm
Carl Sverre
... mutator that said, like, "Add a million rows to the database." So the rep-
Aaron Francis
Sure
Carl Sverre
... the change size doesn't actually relate directly to the, like, mutator size.
Aaron Francis
Mm-hmm. Totally.
Carl Sverre
Um, and that's sort of a nice property. You can think of these mutator, this mutator log as essentially a representation of all of, like, the business logic transactions that happened on your app.
Aaron Francis
This almost feels like event sourcing, where you're just keeping track of the events to later replay them.
Carl Sverre
This is exactly re- event sourcing, actually.
Aaron Francis
Yes.
Carl Sverre
So you nailed it.
Aaron Francis
Love it. I was hedging. I was hedging ahead. I didn't wanna come in too confident.
Carl Sverre
Yeah. I guess, I guess exactly is probably not right, but it's very, very close.
Aaron Francis
Oh, don't walk it back now. I was right. I was exactly right.
Carl Sverre
Well, I have to walk it back 'cause I know this is going on the internet, and like-
Aaron Francis
Yeah, that's true
Carl Sverre
... somebody'll be like, "You're not allowed to say exactly."
Aaron Francis
Good point.
Carl Sverre
Um, okay. So basically we record this log locally that says, okay, like Aaron on his computer said that I'm gonna add milk, and then he said I'm gonna add eggs, then he said I'm gonna add whatever. And this is just a local-
Aaron Francis
Mm-hmm
Carl Sverre
... log. Again, we haven't hit the internetwork yet. When your computer has network access, your computer's gonna connect to the coordinator.
Aaron Francis
Okay.
Carl Sverre
And it's going to send the log of all the mutations you did on the database.
Aaron Francis
Okay.
Carl Sverre
Just the log. It doesn't send any changes you made to the database. It sends the, like, description of the changes you made to the database, i.e. the-
Aaron Francis
Okay
Carl Sverre
... list of mutations.
Aaron Francis
So to clarify, there are two, um, as far as I understand, there are two sets of like, uh, uncommitted changes, we'll say. In the SQLite database you have a high watermark of what is committed by the coordinator, then you have other stuff on top of that.
Carl Sverre
Mm-hmm.
Aaron Francis
But that's not what we're sending. What we're sending is a stream of, um, names of functions and function arguments to be replayed by the coordinator.
Carl Sverre
That's exactly right.
Aaron Francis
Okay. All right.
Carl Sverre
So now the coordinator, uh, is connected to both of our computers, let's say. So it's connected to my computer, it's connected to your computer, and both of us are, like, anxiously adding all of our grocery list items for the upcoming week because, like, we know-
Aaron Francis
Yep
Carl Sverre
... we need to go to the grocery store.
Aaron Francis
Yep.
Carl Sverre
And so the s- the coordinator receives this, like, completely random list. Some of the items come from Carl, some come from Aaron, and what it has to first do is it has to decide on some ordering of all of these mutations.
Aaron Francis
Mm-hmm.
Carl Sverre
And so the server, the coordinator, has the advantage that it's like a single authoritative sort of state.
Aaron Francis
Okay.
Carl Sverre
And so it just decides on the ordering is based on when it receives each of these mutations.
Aaron Francis
Okay.
Carl Sverre
So it has its own global log, and that log contains all of our mutations put together into one log ordered by whenever it was received by the server, by the coordinator.
Aaron Francis
Okay. So there's no timekeeping in the clients.
Carl Sverre
Exactly. So the client doesn't-
Aaron Francis
Okay
Carl Sverre
... need to worry about time. Um, we just send the transactions as we create them, uh, or not the transactions, the mutations as we create them-
Aaron Francis
Mm-hmm
Carl Sverre
... through the coordinator. The coordinator simply orders all the mutations into this global log that is per database that it manages.
Aaron Francis
Cool. Mm-hmm.
Carl Sverre
And then it does, what it does is it just simply applies that global log using the same WebAssembly object that we're running inside our clients-
Aaron Francis
Mm-hmm
Carl Sverre
... onto the SQLite database. And so it's essentially building a new version of C- the SQLite database that contains all of my ch- my changes and all of your changes.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, and we'll talk about the... Well, let's, actually this is a good time to talk about conflicts, because this is exactly-
Aaron Francis
Yes
Carl Sverre
... where conflicts actually is gonna happen, right?
Aaron Francis
Right. So if you're offline for 20 minutes, and I'm not offline and I add milk but I remove eggs, and then I don't know what the conflict there would be. Uh-
Carl Sverre
Yeah. It turns out grocery lists are really easy to do automatic conflicts.
Aaron Francis
Yeah, shoot. Um, let's say I change the name of eggs to eggs, parentheses, two dozen.
Carl Sverre
Yeah.
Aaron Francis
And you delete eggs, then what?
Carl Sverre
Yeah, exactly. That's a, that's a pretty s- We can start there, and then we can add more conflict, or more complex conflicts later.
Aaron Francis
Okay.
Carl Sverre
Um, so this is where we can talk again about this, like, why did, why did Carl talk about physical and logical conflicts? Like, why does that matter?
Aaron Francis
I just thought it was interesting, but sure.
Carl Sverre
Yeah.
Aaron Francis
Tell me more.
Carl Sverre
It is interesting. I mean, I'm a huge nerd about this stuff, so this is super interesting to me. But, um, because we've ordered, like, all of the mutations globally on the coordinator, and the coordinator just runs, like, one mutation at a time against-
Aaron Francis
Mm-hmm
Carl Sverre
... against the SQLite database.
Aaron Francis
Mm-hmm.
Carl Sverre
We've eliminated the possibility of there to be any kind of, like, physical conflicts in the SQLite database.
Aaron Francis
Okay.
Carl Sverre
Because SQLite knows how to handle running one transaction at a time.
Aaron Francis
Mm-hmm.
Carl Sverre
If we can represent the transaction in SQL, SQLite guarantees that it will not corrupt the database, right?
Aaron Francis
Correct.
Carl Sverre
Now, this would be at odds with, like, a naive version of, of multiplayer would be like, "Let's, I'll just edit the database and you edit the database, and let's just exchange all the changed, like, low-level pages, like the actual-
Aaron Francis
Mm
Carl Sverre
... changed bytes in the database."
Aaron Francis
Yeah.
Carl Sverre
"And let's just hope that it works." In this case, we'll probably corrupt the database very quickly, because we're just-
Aaron Francis
Yes
Carl Sverre
... like exchanging, like, random bytes about a file. Um, hilariously, SQLite will actually handle, like, a lot of changes this way, um, without corrupting. Um, but there will be a bunch of changes which it can't handle, like for example-
Aaron Francis
Mm-hmm
Carl Sverre
... it moves a B-tree around or it changes, changes certain things.
Aaron Francis
Vacuums something up on one side.
Carl Sverre
Exactly. There's a whole bunch of changes that could happen which, in which exchanging all the edits we did would, would involve physical corruption. So the first thing that SQLSync does really well is that it, it completely solves physical corruption by just basically guaranteeing that there's, like, a single writer. There's only, like we're writing-
Aaron Francis
Mm-hmm
Carl Sverre
... each of these transactions in order in, like, the coordinator. We, we can eliminate any kind of, like, physical conflict corruption.
Aaron Francis
Yeah, and you're staying outside of, like, the realm of, uh, danger. So you're just, like, interacting with SQLite as it was made to be interacted with. You're not mucking about with the bytes in the file.
Carl Sverre
Exactly. So then there's, there's the logical conflict. So the logical conflict that you described, which is, like, basically, uh, you change the name of a, a grocery list item and I delete it.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, that, that conflict, like, w- needs to be decided. Like, what happens in that case ne-
Aaron Francis
Mm-hmm
Carl Sverre
... is a decision that the, the developer has to make. That is fundamentally a business logic decision. We can come up with-
Aaron Francis
Mm
Carl Sverre
... a bunch of different solutions, right? One solution would be the last writer wins, right? Like-
Aaron Francis
Right
Carl Sverre
... if I delete and then you edit, your edit just-
Aaron Francis
Mm-hmm
Carl Sverre
... disappears because it-- Or, or potentially we want the edit to, like, restore the, the item, right?
Aaron Francis
Mm-hmm.
Carl Sverre
I delete it, then you edit it. We want that edit to, like, bring the item back. Um-
Aaron Francis
Right
Carl Sverre
... potentially we want to do some kind of, like, weird merge, although you can't really merge a edit and a delete, but let's say we both edited the, the title at the same time.
Aaron Francis
Mm. Okay.
Carl Sverre
Um, the merge would be like, let's look at the titles and then let's, like, merge them together in some way.
Aaron Francis
Okay.
Carl Sverre
Right? This is fundamentally a business logic decision. Like, it's something a developer has to decide, and the decision is going to have a lot of trade-offs. Like, some of those decisions are gonna be much more efficient to implement than others. Like, last writer wins is probably the most efficient, just overwrite whatever's there and-
Aaron Francis
Mm-hmm
Carl Sverre
... move forward. Whereas some kind of, like, conflict detection which involves some kind of, like, three-way merge or some kind of, like-
Aaron Francis
Mm-hmm
Carl Sverre
... more complex thing is gonna be more, more sophisticated. SQLSync allows the developer to manage that themselves inside of their WAS- Wasm, their WebAssembly logic.
Aaron Francis
Okay.
Carl Sverre
The reason we use WebAssembly instead of just having, like, the... I mean, we could just express the mutations as just simply SQL commands, right? Like, why do we need-
Aaron Francis
Sure
Carl Sverre
... to add the WebAssembly layer? We add the WebAssembly layer because that layer is where you logically should handle mutations, or-
Aaron Francis
Okay
Carl Sverre
... conflicts, sorry. So-
Aaron Francis
And you need some sort of, you need some sort of programming language to do that.
Carl Sverre
That.
Aaron Francis
You can't just have SQL commands.
Carl Sverre
So you need to, like, encode the business logic of conflicts-
Aaron Francis
Mm-hmm
Carl Sverre
... somewhere into the system. And SQLSync does that by allowing you to encode that into your WebAssembly program that you write.
Aaron Francis
Okay.
Carl Sverre
And so your WebAssembly program, which let's say that it handles, uh, it has a rename operation. And let's say-
Aaron Francis
Okay
Carl Sverre
... that it sees that you, you as the developer want to encode the property that a rename operation should, like, restore an item back from the dead if it was deleted.
Aaron Francis
Okay.
Carl Sverre
So in order to implement that in SQLSync, you'd have to implement, first of all, you'd have to keep around the state of a, a deleted item. And so traditionally-
Aaron Francis
Mm-hmm
Carl Sverre
... in most relational systems, the way we do that is that rather than deleting a row, we just have a, a column which represents that the row was logically deleted.
Aaron Francis
Mm-hmm.
Carl Sverre
So your delete operation would simply be mark that delete column to be true, and your rename operation-
Aaron Francis
Oh
Carl Sverre
... would be rename the colu- like, column, and then also mark that as false if it was marked true.
Aaron Francis
Okay, so hang on. Um, okay. So I as the developer have to anticipate that one possibility would be that I rename something that you have deleted. And in my... So knowing that, knowing that that is true, I would have to modify my delete mutation to tombstone it or, like, soft delete it or something.
And in my rename operation- Would I, in my rename operation, always rename it and set deleted at to null? Or is there some sort of conflict method that is called? Or do, or all of the... Like, does the rename, setting the deleted to null, does that handle the conflict itself? Do, do you see what I'm asking?
Like, is there a on conflict callback?
Carl Sverre
Yeah. So, um, there's no on conflict conflict. You just, you should implement the conflict management in pure SQL.
Aaron Francis
Got it.
Carl Sverre
And SQL's pretty good at managing this type of conflict. Um, in a more complex type of conflict where, for example, let's say that I rename column and you rename a column.
Aaron Francis
Mm-hmm.
Carl Sverre
Uh, and we do it at the same time, and the question is, like, we wanna get, like, a merged version of those two things.
Aaron Francis
Yeah.
Carl Sverre
Um, you would have to add some additional metadata to your, to your data structures, like to your actual tables, to be able to-
Aaron Francis
Hmm
Carl Sverre
... help your mutator know when it should perform, like, a merge and when it shouldn't perform a merge.
Aaron Francis
Hmm.
Carl Sverre
Um, but the nice thing is, is that the mutator is, like, arbitrary logic. It's, it's, it's basically-
Aaron Francis
Sure
Carl Sverre
... you know, um, just arbitrary code. And so the idea is that, like, you can run a select query in your mutator. Like, you can, you can-
Aaron Francis
Oh
Carl Sverre
... query the database, you can get back some information about it, and then you can make a informed decision based on that observation. Um-
Aaron Francis
Cool
Carl Sverre
... which would be exactly what you would do if you were implementing some kind, some kind of basic-
Aaron Francis
Hmm
Carl Sverre
... multiplayer inside, like, a traditional application architecture, like a traditional back-end API.
Aaron Francis
Mm-hmm. Okay.
Carl Sverre
So the idea of SQLc is to, like, provide a experience of, like, writing this, like, mutator layer that is more similar to, like, a traditional API on top of a re- a regular relational database than needing to learn about, like, automa- like, data structures that implement some kind of, like, automatic conflict management. Um-
Aaron Francis
Right
Carl Sverre
... so this is, like, a major thing where, where SQLc is sort of at, at odds with, like, other systems.
Aaron Francis
Yeah.
Carl Sverre
Um-
Aaron Francis
Yeah. So if I'm understanding, if I'm understanding correctly, um, all of the... I think it's important that you said, uh, the conflict resolution is a business logic layer. Because at that point, I, I, I happen to agree that some library deciding who wins may not be what I would've decided, may not be what is right for my users or my app or whatever.
And so what, the stance that you have taken, um, is that conflict resolution must be handled by the developer, and the way that you do that is by providing this stream of events that can be played locally and at the coordinator. And I guess the code is the same, but it doesn't matter until it gets to the coordinator where it has to do the actual conflict resolution.
Carl Sverre
Yep, exactly.
Aaron Francis
Okay.
Carl Sverre
So the, so just to, like, fast-forward to sort of the, like, full round trip. So, so far we've, like, we've essentially both edited the database. We've both set-
Aaron Francis
Mm-hmm
Carl Sverre
... our, like, change streams up to the, the coordinator.
Aaron Francis
Mm-hmm.
Carl Sverre
The coordinator's built, like, a new version of SQLite, like the database-
Aaron Francis
Mm-hmm
Carl Sverre
... that, that reflects all of our mutations together with conflict handling-
Aaron Francis
Yeah
Carl Sverre
... and everything. So that, like, there's this new authoritative version that's like, "This is the version that we should all see," basically.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, so the last step of this is getting, making it so that both of us can see those, like, new correct authoritative versions, um-
Aaron Francis
Mm-hmm
Carl Sverre
... and continue to make changes over time, right?
Aaron Francis
Okay.
Carl Sverre
And so the way that that last step works is that the coordinator sends down raw changes to the, like, underlying file that backs SQLite. So this is where we're actually-
Aaron Francis
Okay. Hang on
Carl Sverre
... sending down, like, physical changes to the SQLite file to each of our clients.
Aaron Francis
Changes. Okay. So you're sending down raw changes. So it's in the coordinator, in the coordinator you have, uh, you have the database at a particular state. You receive a dozen events. You play those dozen events, and then do you do a diff of what it was before the dozen events came in, like what the actual underlying SQLite file was before the dozen events, after the dozen events, and then you send the diff,
which is just like a bunch of bytes back to each client?
Carl Sverre
Yes. Uh, yes, we send a diff. Uh, the way we produce the diff is very efficient, so we don't have to do like-
Aaron Francis
Okay
Carl Sverre
... a full diff where we compare the two states of the system. Um-
Aaron Francis
Uh-huh
Carl Sverre
... but basically, yes. Like, so SQLc is going to, on the coordinator, it's gonna collect essentially the specific set of bytes that have changed between the last time that it sent a snapshot to you and to me. Um, and interestingly, like, those two snapshot points can actually be different, and this is a very complex thing. Because you could, like, let's say that you came online after I came online.
Aaron Francis
Oh.
Carl Sverre
Your snapshot could actually be, like, reflecting a different starting point than mine.
Aaron Francis
You're totally right.
Carl Sverre
And so the coordinator actually takes that into account, and it builds custom diffs for every single client to be able to-
Aaron Francis
Geez
Carl Sverre
... fast-forward them as efficiently as possible to the latest version of the-
Aaron Francis
So does every client send their last known state along with the stream of events, or their last, uh-
Carl Sverre
Sort of
Aaron Francis
... committed state?
Carl Sverre
Yeah, sort of. It, you could think of it that way. Like, essentially the coordinator knows, like, the last committed, like the last authoritative LSM on every single client.
Aaron Francis
Okay. And then just for my edification, are you, like, listening to SQLite somehow and seeing which pages are changed? How, how are you doing that efficiently?
Carl Sverre
VFS.
Aaron Francis
VFS. It's always the VFS. Son of a gun. Okay. Yeah. Forgot about the VFS.
Carl Sverre
The same VFS that runs on the client runs on the coordinator. The coordinator's VFS does a slightly more sophisticated thing, which is that it-
Aaron Francis
Mm-hmm
Carl Sverre
... it sort of tracks changes as, as a, like, a log of changes, like a, a series of diffs to the, say, SQLite file. Um, and then it can use that log to be able to efficiently build-
Aaron Francis
Of course
Carl Sverre
... these, like, delta records to send to different clients.
Aaron Francis
Of course. That's freaking... That's awesome. Okay, so then at that point, um, the coordinator has a diff, a change set that could be sent to Carl and Aaron, and those could be different.
Carl Sverre
Yep.
Aaron Francis
But they are correct for the clients that are connected.
Carl Sverre
Yep.
Aaron Francis
Then what happens?
Carl Sverre
Okay, so now your client. So, like, think about your computer.
Aaron Francis
Yep.
Carl Sverre
Your computer actually has three different versions of the database at that point, when it- ... receives the diff from the server. It has the original authoritative state from when you started.
Aaron Francis
Yep.
Carl Sverre
It has local changes to the SQLite file that you've made.
Aaron Francis
Mm-hmm.
Carl Sverre
And then it has remote changes that the coordinator's made that may actually reflect other, like other people's changes that you don't even know about.
Aaron Francis
Sure. Okay.
Carl Sverre
Right? So you have sort of three different versions of, of the database file. And the way that SQLSync works, and this sounds really insane, but it totally works, is-
Aaron Francis
This all sounds insane. But I love every little bit of it. So yeah, tell me, tell me the insane part.
Carl Sverre
All right, so this is the insane part. The insane part is basically SQLSync performs like a rebase operation. It's very similar to like how Git-
Aaron Francis
Hmm.
Carl Sverre
... Git rebase works. So step one is we throw away all your local changes. So-
Aaron Francis
Naturally
Carl Sverre
... tech, like temporarily SQLSync rolls back the database to before you changed anything. So now there's only one version.
Aaron Francis
Which rolls it back to the last authoritative state from-
Carl Sverre
Yeah
Aaron Francis
... the coordinator.
Carl Sverre
Yeah.
Aaron Francis
Okay.
Carl Sverre
So the la- it rolls back to the last, like, version that we s- we started from before we made any local changes, like the last state-
Aaron Francis
Okay
Carl Sverre
... we know are, is, is correct. Then what it does is it fast-forwards to the, the coordinator authoritative version.
Aaron Francis
Mm-hmm.
Carl Sverre
So now it, now, now it just has one version of the database. It has the s- the, the new authoritative version.
Aaron Francis
Mm-hmm.
Carl Sverre
And then what it does is it needs to basically... So, so locally on your computer, you have a series, like you have a list of mutations.
Aaron Francis
Mm-hmm.
Carl Sverre
And some of those mutations have been applied by the coordinator, and some of those mutations are still pending, like they haven't yet been applied. Maybe you made them really, really recently, or they're just like they haven't been processed yet on the coordinator.
Aaron Francis
Okay, so hang on. You're saying that the coordinator sent me back a diff, and I've got, uh, 10 mutations and, um, th- five of those may be represented in that new stream of bytes or that, that new diff. Um, but it's possible that between the time the first five were sent off and handled by the coordinator and come back, I've written five more.
Carl Sverre
Exactly. So to make this-
Aaron Francis
Okay
Carl Sverre
... a little more concrete, like let's say that you add like eggs and then you add, uh, milk, and then you add broccoli, right?
Aaron Francis
Yep.
Carl Sverre
So the coordinator, you send like just because of, you know, everything is happening concurrently and in parallel.
Aaron Francis
Mm-hmm. Mm-hmm.
Carl Sverre
So it's all happening at the same time. So you send to the coordinator eggs and milk, but you haven't sent broccoli yet. So broccoli is still-
Aaron Francis
Mm-hmm
Carl Sverre
... sitting there in your like outbound queue, right?
Aaron Francis
Mm-hmm.
Carl Sverre
But you, you know that I have all these three mutations, like these three mutations matter to the user. I, I can't, I can't suddenly make like eggs disappear. I have to continue-
Aaron Francis
Right
Carl Sverre
... to show it. Um, so we send those two mutations, eggs and milk, to the server. The, the coordinator applies those mutations and then sends us back a diff of the database. And that diff of the database now may include eggs and milk, but you don't know if it does or not.
Aaron Francis
Mm-hmm.
Carl Sverre
So what-
Aaron Francis
Right, because it wasn't, what was sent back was not mutations. It was a, a diff-
Carl Sverre
Like-
Aaron Francis
... of a underlying file that's opaque
Carl Sverre
... and it's basically completely opaque. Like we don't-
Aaron Francis
Yeah
Carl Sverre
... look at this thing. We just sort of treat it as like bytes. We apply it to SQLite.
Aaron Francis
Yeah.
Carl Sverre
And then the question, we have to ask the question is like, of those three mutations that we had, we have eggs, milk, and broccoli, were any of them, like are any of them applied? Like are any of them committed-
Aaron Francis
Mm-hmm
Carl Sverre
... in the authoritative version?
Aaron Francis
Yeah.
Carl Sverre
And the way that SQLSync does this, and this is an idea that I came up with at SingleStore, um, when I was like working on some, some, uh, projects that they have there, uh, and, and it applies really well to SQLSync.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, and so the, what this is, is that SQLSync on the coordinator, when it applies mutations, so the mutations are editing your database, right? Like they're, they're adding milk, they're adding eggs to your database. In addition, SQLSync implements, like adds a hidden table inside SQLite. It's a regular SQLite table.
Aaron Francis
Oh, yeah. Yeah.
Carl Sverre
And this table is h- has one row per client that has ever worked on the database.
Aaron Francis
One row per client. Okay.
Carl Sverre
So both, like we're both connected to the same database.
Aaron Francis
Mm-hmm.
Carl Sverre
Uh, that table will have two rows.
Aaron Francis
Yeah.
Carl Sverre
One represents Carl, one represents Aaron.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, and that, that row just simply records the client ID and the last, like the highest sequence number from that client, like the highest mutation ID from that client.
Aaron Francis
Ah. So importantly, every mutation has a, an increasing ID.
Carl Sverre
A monotonic ID. Yeah.
Aaron Francis
Monotonic, there it is. Okay. Um, so you, I send off IDs one through five. Um, and at the coordinator level, you update the last one that I saw was five for Aaron.
Carl Sverre
Yep.
Aaron Francis
Then you send me back the diff, which includes the secret hidden table. And so then if I'm jumping ahead, the diff is applied. So all my local changes, um, to the actual database are thrown away. My mutations are hanging out. You apply the diff. Then I'm guessing we inspect the secret table and say, "This is committed state at the coordinator.
You can trust it." The last thing that the coordinator's, the last mutation that the coordinator saw was number five, so we'll truncate anything below, or five and below, and then replay six through 10 locally and send them off?
Carl Sverre
Bingo.
Aaron Francis
Hot dang.
Carl Sverre
Hot-
Aaron Francis
How about that? That is pretty freaking cool.
Carl Sverre
And that is a full end-to-end multiplayer relational database.
Aaron Francis
Wow. That is very awesome, I must admit. And it's s- I mean, it's not simple, but it's like straightforward.
Carl Sverre
It's pretty straightforward.
Aaron Francis
Like that's a-
Carl Sverre
Yeah
Aaron Francis
... that's a, that's a good... I don't know anything about these systems, but that's a good one. That's a great one
Carl Sverre
Yeah. What-
Aaron Francis
Huh
Carl Sverre
... what I like about it is that it's, like, I wanted to try to keep this, again, like my goal is I want vanilla SQLite, right?
Aaron Francis
Sure.
Carl Sverre
So if it's vanilla SQLite, I can't change how SQLite represents, like, its internal data structures, because if I do that-
Aaron Francis
Nor would you want to, yeah
Carl Sverre
... I'm not vanilla, right? So if I want completely vanilla SQLite, I need it, all of its internal rel- data structures, how it stores data, how it manipulates B-trees, how it does all of that management-
Aaron Francis
Right, right, right
Carl Sverre
... has to be vanilla, unchanged.
Aaron Francis
Mm-hmm.
Carl Sverre
And so I knew by definition, like, I couldn't manipulate the, like, physical layer of SQLite, so I have to keep that. And then the second thing is, is like, how do you implement conflicts? And, you know, you can do logic, logical conflict management where you actually add a layer between the, like, the, the user and SQLite, and you have to, and, and basically you have to process all of the, like, writes yourself.
Aaron Francis
Mm-hmm.
Carl Sverre
And you have to basically logically do, uh, do your own sort of like writes to your own tracking data structures, and then forward them to SQLite. Like, you act at like, as like an intermediary between the client and SQLite.
Aaron Francis
Okay.
Carl Sverre
And I didn't wanna do that either because I wanted to expose like a raw SQLite transaction API to the user. Like, if you can, if you wanna use a SQLite extension, you can install it into SQLSync and use the extension.
Aaron Francis
Mm.
Carl Sverre
Like, there's nothing about it that knows literally anything about your data. It's completely opaque to SQLSync. And that was, like, that was the goal. So I, so I basically built all of the things, all of the trade-offs that SQLSync has, and all of the, the things about it that are interesting and unique are all driven from the fundamental goal of, like, completely vanilla SQLite.
Like, your app-
Aaron Francis
Mm-hmm
Carl Sverre
... should mostly feel like working with regular SQLite.
Aaron Francis
This is, um, this is something I feel like keeps coming up, uh, when talking to people about SQLite, is they don't necessarily want to touch SQLite. And I feel like a big part of that is, one, SQLite is very good, but two, SQLite is incredibly stable.
Carl Sverre
Yeah.
Aaron Francis
And so to get in there and start dorking around with the pages yourself, that, I mean, frankly, that seems very risky to me because, you know, they've spent, what is it, 24 years now making it super stable. And so it seems like a lot of people are coming at it from the outside. Like, you've got your, you've got your VFS, um, Lightstream has its, like, little sidecar that's, like, keeping a transaction open and kind of sneaking into the wall file and mucking around with that.
And I really, like, it seems like, um, it almost seems like a, a fun challenge. Like, how can I do everything I want without opening this black box?
Carl Sverre
Yeah.
Aaron Francis
And it sounds like you did it, which is pretty awesome.
Carl Sverre
Yeah. It, it did feel really cool. Like, uh, SQLSync I'm really proud of. I, I think it's, it's a, it's a really-
Aaron Francis
You should be. Okay. So tell me, um, SQLSync is fully open source?
Carl Sverre
Mm-hmm.
Aaron Francis
Okay. And what is the plan, like, what's the plan for, uh... So is, is everything you described where you're at right now, all that's handled, all that's done?
Carl Sverre
Yep. Yeah. So SQLSync-
Aaron Francis
Okay. So what's the plan moving forward?
Carl Sverre
Yeah. So SQLSync, as of like right now, um, it works, uh, pretty well, but it doesn't scale very well.
Aaron Francis
Okay.
Carl Sverre
Like, that's sort of the, the fundamental, like, limitation of it. Um, one of the biggest limitations of SQLSync today is that it requires every single client and coordinator to have a full copy of the entire database.
Aaron Francis
Sure.
Carl Sverre
And so it works pretty well for small databases, like in the, like megs size.
Aaron Francis
Mm.
Carl Sverre
Um, it works great. Uh, it, it's super fast at replicating state. It, it can, um, you know, you can handle, like, relatively high amount of mutations. Uh, it's a fairly good system for just small databases.
Aaron Francis
Okay.
Carl Sverre
But if you scale up to, let's say, a gigabyte, that's, you're just automatically a no, non-starter based on the, the need to have to sync the entire system to every single device.
Aaron Francis
Um, so the limitation there is sync time. Is there a limitation of SQLite in the browser?
Carl Sverre
Uh, yeah. I mean, browsers are pretty limited in, like, how much they can store.
Aaron Francis
Mm.
Carl Sverre
Uh, like browser tabs, um, you have, you have memory limits that you're allowed to, a certain amount of memory you're allowed to consume as, like, a JavaScript process or, like, a web worker process.
Aaron Francis
Mm.
Carl Sverre
And then there's also, like, storage limits. So if you're storing in, like, IndexedDB, or if you're storing in OPFS, or you're storing in, like, any of the different ways of storing data inside the browser, all of those have their own limits. Um, and those limits get pretty bad pretty quickly.
Aaron Francis
Mm.
Carl Sverre
Um, and generally, like, I try to avoid ever putting, like, more than, like, half a gig of data in, in a browser tab.
Aaron Francis
Okay.
Carl Sverre
Um, once you get higher than that, like, there are ways you can get higher than that, but it's, it becomes a lot more iffy and sketchy, and it's almost at that point, you're like, you might as well build a desktop application where you can have, like, dedicated storage.
Aaron Francis
Mm.
Carl Sverre
Um, and you can actually work with the user. Because there's also the aspect of, like, most browser storage-
Aaron Francis
Mm
Carl Sverre
... is not even, like, known by the user as to, like, where that storage is, right?
Aaron Francis
Mm-hmm.
Carl Sverre
It's treated as more of like a cache layer than, like, a durable storage layer.
Aaron Francis
Right.
Carl Sverre
When you're storing, like, database state that you potentially don't wanna lose, um, you may wanna actually know where that is. You might wanna be able to back it up. Like, you might wanna be able to access it out of band. So long story short, the current state of SQLSync doesn't really scale due to that limitation.
Aaron Francis
Mm-hmm.
Carl Sverre
Um, it has a couple other things that I'm not super happy with about it. Those are very, very technical and, like, very specific to, to how I implemented it.
Aaron Francis
Mm-hmm.
Carl Sverre
Uh, one of the things is, like, I'm, I'm not super happy with, like, using WebAssembly.
Aaron Francis
Mm.
Carl Sverre
WebAssembly is great for systems people, like, for, for low-level programmers, for people who are willing to go get their hands really dirty. It's pretty, like-
Aaron Francis
Yeah
Carl Sverre
... schmatzy at this point. Um, it's also great for Rust programmers 'cause it actually-
Aaron Francis
Mm-hmm
Carl Sverre
... has, like, a nice user experience in Rust. But pretty much everyone else, it sort of sucks. Um, so I'm thinking about I'd like to have, like, more of like a JavaScript experience, 'cause I think JavaScript has-- It's just way easier to integrate into your app if you could just write, like, a JavaScript mutation, as opposed-
Aaron Francis
Yeah
Carl Sverre
... to having to, like, build a WebAssembly module just to implement a mutation.
Aaron Francis
Yeah.
Carl Sverre
So those are some of the, the changes that I wanna do. But the short-term change, like the thing that I'm working on right now. So basically, I released SQLSync ba- in, like, late last year. Um, this year, since January, I've be-been working basically full-time on a new project that I'm calling Graft.
Aaron Francis
Okay.
Carl Sverre
And so Graft is a storage engine that-
Aaron Francis
Okay
Carl Sverre
... will power SQLSync.
Aaron Francis
Okay. Go on.
Carl Sverre
Uh, and yeah. It's the first time I've actually talked about it. Um, so-
Aaron Francis
Okay. You don't have to reveal anything you don't want to.
Carl Sverre
No, no, this is good. This is good. It, it forces me to, like, put it into words. Um-
Aaron Francis
Okay
Carl Sverre
... basically, Graft is a, a storage engine that I'm building. What is a storage engine? Like, it's essentially, it, it let-- it gives you sort of a file-like abstraction. Like, it's here's a, here's a blob, and it lets you read and write bytes to that blob. Um-
Aaron Francis
Okay
Carl Sverre
... but it, it implements the way it reads and writes bytes to that blob. It internally uses these data structures that are very efficient to synchronize over the internet. Um, and so for example-
Aaron Francis
So what does this re- what, what does this replace in SQLSync?
Carl Sverre
It replaces the VFS layer that-
Aaron Francis
Got it
Carl Sverre
... I implemented, basically.
Aaron Francis
Okay.
Carl Sverre
So the VFS layer in SQLSync, uh, works reasonably well, but it-
Aaron Francis
Uh-huh
Carl Sverre
... um, it was implemented in a very, like, simple way. Like, we just simply have, like, a layered file system. We track all the edits to the file, like, at every single transaction point.
Aaron Francis
Mm-hmm.
Carl Sverre
And so it has a lot of duplication over time. Um, like, if you edit the same page multiple times, for example, you'll store, like, multiple copies of that page. But the clients don't actually need to care about every single version of that page. The client literally only cares-
Aaron Francis
Right
Carl Sverre
... to be able to fast-forward to, like, the latest version of the page-
Aaron Francis
Right. Okay
Carl Sverre
... when the client synchronizes. So Graft is a, a s- a sort of special purpose storage data structure that is really efficiently designed to do fast forward replication. Um, so it's, that's, that's its whole thing. Uh, and I'm building it as, like, a low-level library that is independent from SQLite and from SQLSync.
Aaron Francis
Mm-hmm.
Carl Sverre
Because it itself is actually just a useful way to basically say, like, let's say you have a file in point A, and you wanna have that file in point B.
Aaron Francis
Mm-hmm.
Carl Sverre
Graft will do that for you. It will copy it. But where Graft differs from something like, let's say, Rsync, is that-
Aaron Francis
Okay
Carl Sverre
... Graft will continuously replicate changes to that file forever. And it will do so using a close to minimum amount of bandwidth.
Aaron Francis
Okay.
Carl Sverre
So that's what I'm trying to-- That's what I'm building now, and that, that will eventually power SQLSync. Um, and also I'm experimenting with using it to do some other really cool things. Uh, one of the ideas I had was, um, in the, like, data science and, like, AI model space, being able-
Aaron Francis
Mm
Carl Sverre
... to, like, efficiently move, like, portions of AI models and, like, uh, specifically, like, training data sets or, like, weight files-
Aaron Francis
Sure
Carl Sverre
... between different machines and being able to only-- You only need to move, like, what you need right now. Um, so yeah, like, the other property of Graft that's cool is, like, it does fast-forward replication, and it also does lazy replication. So I can s-
Aaron Francis
Hmm
Carl Sverre
... you can, when you're reading from Graft, you can choose to only read a subset of the file. And so in SQLSync land-
Aaron Francis
Hmm
Carl Sverre
... what this means is that I'll be able to run 20 gigabyte databases and have them run inside your browser tab.
Aaron Francis
Okay. So hang on. Um- Let me, let me let my brain catch up here a second. So you currently have this VFS that sneaks in, keeps track of stuff. You're writing Graft. Is that... Does that sit next to the SQLite file, or is that what the s- is it like a file system itself?
Like-
Carl Sverre
Pretty much
Aaron Francis
... I, I don't fully understand how it works.
Carl Sverre
Yeah. So you can think of Graft as providing, like, uh, it's, it's a, it's basically it provides you a file system abstraction-
Aaron Francis
Okay
Carl Sverre
... without the-- It doesn't provide the, like... There's two parts of a file system. There's, there's, like, the directory metadata tier of the file system, and then there's, like-
Aaron Francis
Okay
Carl Sverre
... a file s- the file store.
Aaron Francis
Okay.
Carl Sverre
The directory metadata tier is the thing that implements, like, directories, file metadata, all of the things that you think of as, like, when you go in, like, LS inside your Unix terminal.
Aaron Francis
Mm-hmm.
Carl Sverre
That's the, uh, that's, like, the, what I think of as, like, the metadata tier. Graft doesn't provide that, but what Graft provides is it provides, like, a file back end. So if you-
Aaron Francis
Got it
Carl Sverre
... if you create a file and then you back it with Graft, um, the Graft will provide the, like, it will store the bytes of that file, and it will-
Aaron Francis
Mm-hmm
Carl Sverre
... add all these superpowers. Like, you'll be able to, like, replicate the bytes of those files somewhere else, and that somewhere else will be able to only pull the bytes that it needs of that file. It won't have to read the entire file.
Aaron Francis
Man, I thought SQLSync was, like, wizard stuff. This is, I feel like this is next level. This is wild. Because at that-
Carl Sverre
If it works
Aaron Francis
At that, at that... Yeah. I guess. At that point, the whole, like, you're opening yourself up to physical corruption, right? Because you're just, like, slinging bytes around, aren't you?
Carl Sverre
Well, so Graft has a property which is that it's single writer. So Graft does not implement any kind of multiplayer layer.
Aaron Francis
Okay.
Carl Sverre
It literally is just, like, I have a writer at point A, and then I have infinite number of readers.
Aaron Francis
Got it.
Carl Sverre
And I want all of those readers to see the same version of this file-
Aaron Francis
Got it
Carl Sverre
... all the time.
Aaron Francis
Okay.
Carl Sverre
And I want them to be able to update themselves, like, to the latest version of the file very efficiently. And I want them to be able to, um, stream the version, like, the, the file as it changes, and I want them to only have to pull the portions of the file that they care about. But it doesn't implement any kind of multiplayer layer. So it will-
Aaron Francis
Got it
Carl Sverre
... it will implement the, like-
Aaron Francis
Okay
Carl Sverre
... coordinator to client synchronization aspect of SQLSync.
Aaron Francis
Got it. Okay. That makes a lot of sense and is very, very cool. So at that point, if I'm following correctly, at that point you've got, um, Graft at least on the coordinator. Um, and the whole, the whole rest of the system basically stays the same more or less, and you're sending streams of events.
And the difference is when in the coordinator, those are written to the underlying .SQLite file. Graft is in there, it's got its hooks in that file, and it knows the bytes that were changed, and it just, it just sends those bytes out in a more efficient way than is currently happening with, uh, SQLSync.
Carl Sverre
Yep, exactly.
Aaron Francis
Cool. So cool. Man, that is wild.
Carl Sverre
Yeah. So that's what I've been working on for the last, uh, almost six months, I guess. Um, and I-
Aaron Francis
Cool
Carl Sverre
... I'm getting hopefully close to the point where I can actually have something released with that, but that, this, this project, like SQLSync was a, about three months of like full-time work. Um-
Aaron Francis
Nice
Carl Sverre
... and this project is probably about twice as big as SQLSync. Um, so I'm hoping-
Aaron Francis
Yeah
Carl Sverre
... to finish it up in like the next month or two.
Aaron Francis
Wow, cool. Um, and so one thing we didn't touch on, but it sounds like SQLSync might be great for desktop apps. Is that right?
Carl Sverre
Yeah, it's, it's another area that I wanna really explore is like desktop and mobile apps. Um-
Aaron Francis
Yeah
Carl Sverre
... like, it, it, it, I sort of originally designed it really focused on like the browser, but there's nothing-
Aaron Francis
Sure
Carl Sverre
... really inherently specific about it. Uh, the nice thing about SQLSync being pure SQLite is it sort of works wherever SQLite works.
Aaron Francis
Right, yeah.
Carl Sverre
Um, one thing I am starting to experiment with though, which will make it even better, is like in SQLSync there's, there's two layers of SQLSync. There's the SQLSync wrapper, which implements like the WebAssembly logic and like the multiplayer, like rebase logic and stuff. That's outside of SQLite.
Aaron Francis
Mm-hmm. Mm-hmm.
Carl Sverre
And then there's like the part that's inside SQLite, which is like the VFS layer, the physical layer-
Aaron Francis
Sure
Carl Sverre
... stuff like this. Um, with Graft, one of the things I'm experimenting with is building it as a completely native SQLite extension.
Aaron Francis
Mm.
Carl Sverre
So my current like Graft test system that I use, um, actually just uses like, I use Brew install SQLite.
Aaron Francis
Mm-hmm.
Carl Sverre
And then I like install s- Graft into that SQLite, and I can run Graft.
Aaron Francis
Just like .load-
Carl Sverre
Yep
Aaron Francis
... Graft?
Carl Sverre
Yeah.
Aaron Francis
Got it. Rules.
Carl Sverre
So I'm going to the point where like you'll actually be able to drop Graft into like iOS native SQLite and just add it as an extension, or you could drop it into like wherever, you know, bun SQLite. Like whatever implements native SQLite, you should be able to-
Aaron Francis
Mm-hmm
Carl Sverre
... drop Graft in, um, and suddenly you get basically like, Graft will implement just the physical tier of SQLSync. So it will give you like one way-
Aaron Francis
Mm
Carl Sverre
... super high performance replication, and then s- then SQLSync will be implemented as like another sort of layer that will be very, very lightweight, so you can add it directly on top of SQLite without having to edit SQLite.
Aaron Francis
Mm-hmm.
Carl Sverre
So I'm trying to build this in sort of like a more modular way, so it'll be easier to-
Aaron Francis
Mm-hmm
Carl Sverre
... port SQLSync to more places.
Aaron Francis
Wow. Dude, this is awesome. I, I, I knew that like SQLSync was cool, and I played with it in the browser and was like, "Oh, this is really cool." This is blowing my mind. This is very, very cool.
Carl Sverre
Oh, I'm glad you enjoy it. And, and thanks for getting me to like open up and talk about it because, uh-
Aaron Francis
Yeah, of course
Carl Sverre
... I, I really, it's been bottled up in me, so it's, it's fun to chat about.
Aaron Francis
Yeah. I, I had the easy job here. Okay, so, um, two things. Uh, what is next for SQLSync and Graft? I know that, uh, what's next for SQLSync is Graft, but like where's Carl in a year or two years? Is this, um, does this become a, a hosted offering?
Does this just bolster your credibility? Like, what's, what's the goal for Carl here?
Carl Sverre
So my immediate goal is make something that's useful. Like, I don't really care-
Aaron Francis
Okay
Carl Sverre
... about, um, the, the next steps as much as I care about making something that's actually useful and not just like a toy. Um, SQLSync-
Aaron Francis
Mm-hmm
Carl Sverre
... as of right now is more of a toy than something that like I would be comfortable someone, with someone building like a product on top of. Um, so-
Aaron Francis
Okay
Carl Sverre
... my goal right now is to make something that's like I could really stand behind and say, "I would use this to build a real product." And ideally, actually, I'd like to build a product on top of it. Like, like make something real that, that people can play with and use that's more than-
Aaron Francis
Sure
Carl Sverre
... more than just a toy demo.
Aaron Francis
Mm-hmm.
Carl Sverre
So that's sort of the, the medium term goal. Um, well, short term goal is finish Graft. Medium term is-
Aaron Francis
Yeah
Carl Sverre
... make something useful that is, is interesting. And then like we'll see where that goes. Uh, if it goes towards people are like, "Hey, this is really useful and I wanna build a product on it," then I think I wanna start a company that's focused on that, like just completely making it as easy as possible for people to build interesting things on top of that technology. Um-
Aaron Francis
Cool
Carl Sverre
... if it's sort of like people are like, "Well, it's useful, but maybe there's other solutions or the, the market's too saturated," whatever, like, you know, if it doesn't make sense, um, I think there's a lot of ways I can either pivot this or, or sort of go into like a slightly different direction. One of the reasons I'm building Graft the way I'm building Graft is because I don't wanna pin myself completely into like multiplayer SQLite.
I wanna be-
Aaron Francis
Mm
Carl Sverre
... a little bit broader. Um, and so because-
Aaron Francis
That's smart
Carl Sverre
... Graft is just a storage engine, you can use it to do a whole bunch of cool stuff. Like if you're just replicating, uh, you know, cool like model training data, for example-
Aaron Francis
Mm
Carl Sverre
... like you can use it to do that. Um, so I'm trying-
Aaron Francis
That's good
Carl Sverre
... I'm trying out a little bit more of an experimental like breadth first approach.
Aaron Francis
Yeah.
Carl Sverre
And then hopefully get some feedback later this year once I launch some stuff open source.
Aaron Francis
This, um, this aligns with one of my, um, deeply held beliefs that progress creates progress and motion creates motion. And like you started with SQLSync and it's like very good, and then you realize, hey, there's this other thing that is needed and it's- ... potentially a lot broader and a lot bigger, but you never would've gotten there-
Carl Sverre
Yeah
Aaron Francis
... without following your curiosity in the first place.
Carl Sverre
Exactly.
Aaron Francis
Yeah.
Carl Sverre
And I've always, uh, yeah, I mean, I, I really like, like you, you've been a proponent of, like, just, you know, you have to build your own luck. Like, you have to-
Aaron Francis
Mm-hmm
Carl Sverre
... do that, and I, I believe in that, like, really strongly. Like, I think by, by pursuing what I find most exciting and most interesting and taking it very seriously and getting really nerdy about it, um, A, I'm, like, learning. B, I'm, I'm getting myself in a position where I can help others. And then C, I'm hopefully building something that I would wanna use myself. And if I wanna use it-
Aaron Francis
Mm-hmm
Carl Sverre
... then maybe someone else would wanna use it, is sort of my thought.
Aaron Francis
God, I love that. I'm so happy for you. This sounds like it's a lot of fun, and this sounds like purely within your zone of genius, and I'm just so-
Carl Sverre
Thank you
Aaron Francis
... freaking pumped for you.
Carl Sverre
Very kind of you, Aaron. Thank you.
Aaron Francis
Yeah. Well, thank you for telling me all of this. Um, tell people where they can keep up with you, where they can keep up with SQLSync, and where they can hopefully, um, soon in the next few months learn more about Graft.
Carl Sverre
Yeah, I mean, so sqlsync.dev, uh, is like the main host URL for SQLSync. Uh, there you're gonna find, like, a blog post I wrote that goes pretty much into detail about a lot of the rationale ri- behind SQLSync, how it works. Um, you'll also be able to find the to-do list demo, which I encourage you to play with, try to break it, see what happens. And then, uh, you'll also find a link to my, um, the, like, SQLSync, uh, Discord server.
Aaron Francis
Yeah.
Carl Sverre
That's probably the best place to go. Like, if you're interested in any of the stuff I talked to you, you wanna chat with me, go on the Discord server, ping me. Um, you know, I'm always happy to answer questions, always happy to find more people who, like, like nerding about these topics.
Aaron Francis
Mm-hmm.
Carl Sverre
Uh, and I'm happy to chat about it.
Aaron Francis
Awesome. Well, thank you again for your time, and it's just always so fun to talk to you, so thanks for doing this.
Carl Sverre
Same with you, Aaron. It's nice to see you.
Aaron Francis
Yeah, you too. All right, we'll talk soon.
Carl Sverre
Yep. Talk to you later. Bye.