Ruby on Rails + SQLite with Stephen Margheim
In this episode of Database School, I sit down with Stephen Margheim, a Rails and SQLite expert, to explore his unique journey from academia to software development. We dive deep into the challenges and solutions he encountered while optimizing SQLite for Rails, including his work on reducing database locked errors and improving performance for production environments.
Everything we said
Aaron Francis
All right. Well, welcome back to this, uh, this ad hoc podcast that we're calling Database School, where I get to talk to people who are database geniuses, so no pressure, Steven, uh, and learn a bunch of stuff. So today, I'm here with my friend Steven Margheim, and I know him from, uh, from Twitter.
I've been following him for a while. And the reason that I know you is you are the Rails SQLite guru. And so you started popping up everywhere. Um, I don't know when you got into it, I'll be curious to hear that story. But I have read, I think, everything you've written about Rails and SQLite, and I've never written a line of Rails in my life.
Um, but it's very instructive and very helpful for me, especially as, you know, Rails and Laravel are quite similar. Um, so reading a lot of your stuff has been incredibly helpful. So, uh, with that introduction, welcome, Steven. I'm glad we finally get to hang out.
Stephen Margheim
Yes. Thank you. Me too.
Aaron Francis
So you are, um, we were just talking, you are an American living in Germany doing Rails work and have been big into SQLite. So can you tell me a little bit, uh, just here at the beginning, a little bit about how you ended up where you are? You came from a non-traditional path, kinda like I did. Um-
Stephen Margheim
Mm-hmm.
Aaron Francis
So tell me how you got into Rails and how you ended up in Germany.
Stephen Margheim
Yeah. So I am not a software developer by trade in, in the sense of getting a degree at university in computer science. I was a, um, a classicist. I studied Greek and Latin literature and philosophy. Um, and I was, uh, in a graduate program in Philadelphia, and I actually got my first laptop, uh, as a graduate student.
I had worked in, in undergrad at the computer lab. It just bounced around and did everything in the cloud. And so I sort of felt this obligation to, like, use this expensive new tool to its fullest. So I started reading all of these different blogs from academics, uh, who had a bent towards building tools and using tools and sort of, like, building out workflows.
Um, and just got really into it. And the sort of natural progression, like you, you download a script from somebody's blog. I, I started off with a lot of AppleScript. And you're like-
Aaron Francis
Mm-hmm
Stephen Margheim
... "Oh, this is interesting," like, "I wonder if I could just change this 10%." And you open it up and you're like-
Aaron Francis
That's how they get ya
Stephen Margheim
... "Eh, this is kind of readable. Let me just... How much Googling does it take?" Of course, at the time, no AI. Had to Google everything like an animal.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, and you get the computer to do that, and like, "Wow, computers can do that? I wonder if I could get this 10%." And over the course of three years, I just went deeper and deeper and deeper into the rabbit hole until I eventually realized-
Aaron Francis
That's fun
Stephen Margheim
... that I was spending way more time-
Aaron Francis
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.
Stephen Margheim
... leave academia and get a job. And the job I got happened to be a Rail shop. I didn't know any Rails, had not done any web programming. Um, so I learned Rails, uh, learned about the web and building for the web, and just fell in love, uh, with all of it. And for my second job, I, um, I sort of realized I was 28.
This was the first job opportunity I had where I wasn't, um, basically begging. Like, I had some experience and some expertise. And, um, I recognized, like, wow, this is basically the complete opposite job market from academia because I actually have some optionality. Um, and I thought, you know, I'm 28. I'm single.
I don't have a ton of stuff. I probably could, like, literally live anywhere in the world and have a job and support myself. Um, and I thought I kind of had an obligation to my future self to at least give myself the chance to do something bold and interesting. So I'm nine generations removed from a German immigrant, so I literally went on a job board, and I filtered for Rails, and I filtered for Germany.
I applied to some jobs. I got lucky enough to get a job, and they were willing to, uh, pay to relocate me, and I've been in Germany for the last almost six years.
Aaron Francis
Wow. That is... Yeah, that's wild. I, I, you know, like I hinted, I came from, uh, an, an accounting background, and we actually, uh, we went to school just down the road from each other. So there wa- there would've been a time-
Stephen Margheim
Yeah.
Aaron Francis
Uh, I went to T- Texas A&M. I've learned you went to Baylor. So there would've been a time when we were bitter rivals. Thankfully-
Stephen Margheim
Yeah.
Aaron Francis
... that is all behind us now that we're grown adults. But, uh-
Stephen Margheim
Yes
Aaron Francis
... yeah, so it's funny, but-
Stephen Margheim
For the most part, yes, it is.
Aaron Francis
For the most part.
Stephen Margheim
Completely behind me.
Aaron Francis
Great. I can't, I can't wait to get into it. Um, so yeah, very similar, very similar backgrounds. Um, except I did not move to Germany. I have been in Texas my entire life, so you've got, you've got one up on me there. And so how did you go from, uh, how did, how did you pick up Rails coming from I'm an academic tinkering with AppleScript to I'm a professional enough Rails developer to get a job?
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.
Stephen Margheim
Well, I, um, got lucky, is the short answer. Um, I-- It took me a while. I had to apply to a lot of places and had a lot of conversations until I found, um, a team whose hiring process was oriented more around, uh, show us what you can do and talk with us about how you're doing it and how you're thinking, more so than it was give us a paper trail to make us feel-
Aaron Francis
Nice
Stephen Margheim
... safe and confident that, uh, we can just hire you. And so, um, I was able to demonstrate that I could think, uh, clearly and could articulate my thoughts clearly in a way that, uh, and it was just for, you know, a junior programmer role, um, that they were willing to, to give me a chance. And my first week, they sat me down and said, "Here is a tutorial Uh, online free tutorial to learn Rails.
Your first week is to go through this. Uh, so I had not seen or heard anything about Ruby s- uh, seen or heard anything about Rails.
Aaron Francis
Good for them.
Stephen Margheim
Uh, and they, yeah, they trained me. Um, and I am incredibly lucky and appreciative. That was the, the internal sort of consulting team for Penn Medicine. So the whole medical complex, the medical school, and the hospitals, um, they have an internal IT team, um, that does various things, and one portion of that team builds web applications that are, you know, back office tools for various-
Aaron Francis
Mm-hmm
Stephen Margheim
... different teams and departments throughout, uh, the medical complex. And they, they gave me a chance, and they taught me Rails.
Aaron Francis
That-- Good for them. That, that is incredible. Um, and I, I will get to SQLite in a second, but I can't imagine, I don't know very much... L-let me rephrase. I don't know anything about academia, but I can imagine coming out of academia and having the job qualification not be a paper trail and not be a line of people that are saying, "This person is smart." It, it's more, "Show me what you can do." I imagine that was a massive shock based on the water you had been swimming in, huh?
Stephen Margheim
Yeah. It was, um, quite different. Um, in certain ways similar, uh, in other ways, though. Like, I, I was mostly studying these dead languages, right? So no speaking, just reading and writing. And, um, so a lot of what I was doing was how do I take the way that I naturally think, which is, you know, in English and thoroughly in that culture, but, like, sort of stretch and adapt my brain to be able to ar- you know, conceive of thoughts in different, uh,
languages.
Aaron Francis
Hmm.
Stephen Margheim
And there are some sort of low-level thought pattern similarities to, like, having to read Latin, uh, like reading Ruby or C or PHP or what have you.
Aaron Francis
That's fascinating. Not, uh, not something my brain has ever experienced because I don't, you know, speak or read any other languages. But I can imagine the interesting and kind of, uh, fun, like, corollaries between learning a, a language and learning a language, you know? It's kinda-
Stephen Margheim
Yeah
Aaron Francis
... kinda the same. That's interesting. Um, okay. So you got into Rails, and now fast-forward to today, you're Rails SQLite guy. Like, if anybody- ... in the Rails community is like, "Ah, what's the deal with SQLite?" They're like, "Ah, I don't know. Just ask Steven." So how did you-- Like, where did, where did this come from, this line of your career? Because you've gotten stuff, like you've gotten some pretty core stuff in Rails itself changed to support SQLite better, which I wanna talk about.
But where did the original interest in SQLite come from?
Stephen Margheim
Yeah. So, um, the job that I moved to Germany for, um, I eventually became the engineering manager for that department. And over a few years, the nature of that role, uh, shifted and evolved, like, to a place where-- And I, I still love programming.
I wanted to still be doing programming. So, um, I became sort of the front-most, uh, person in our R&D department.
Aaron Francis
Hmm.
Stephen Margheim
Um, so I would take opportunities to, uh, build new applications. Uh, oftentimes they were, you know, for, like, individual customers who had very specific needs, or we wanted to just sort of dip a toe in the water of a new business vertical and see, like, how the market reacted. And pretty quickly, I came to realize that I was under a unique set of constraints, right?
I was in meetings almost all day. Uh, just the nature of the role. And so aside from, like, being able to spin up an application, like create something from nothing quickly, and Rails is good at that, and when you, uh, know how to use the tool well, like, you can do that quickly. Um, but when you are actually running it in production, um, you have to maintain it.
And so whether that's fixing bugs or, like, adding a small feature, um, I had these small requirements, but I just did not have a lot of time. And so I eventually came to realize that, like, really the constraint I had was I need to be able to have someone tell me what they need, and 30 minutes later have that delivered.
And that's just, like, an incredibly fast turnaround time.
Aaron Francis
That is.
Stephen Margheim
And as I iterated through various things, I just started off using Heroku and sending stuff up, and, um, I realized that every single indirection, every single dependency, every single, um, additional component in the overall system added time to me going from, "You need this," to, "I'm giving this to you." And so I just-
Aaron Francis
Sure
Stephen Margheim
... I just was stripping and stripping and stripping away. And I, uh, after maybe two applications I had, like, built, I just decided to try, um, using the defaults. Like, if you just run Rails new in your terminal, it'll use SQLite. And I, I knew a lot about SQLite from my old, uh, sort of building tools as an academic.
Um, that's the, the database that you use, and I worked with other applications whose data was stored in SQLite and all that. Um, so I felt comfortable with it. And, um, really quickly I recognized, okay, the operational simplicity that this gives me-
Aaron Francis
Hmm
Stephen Margheim
... actually makes it possible for me to do 30-minute turnarounds. Pardon me. So I fell in love with that reality. Uh, and especially when you contrast that with the sort of standard situation when you're working in an app for a company that's been around for 10 years and has had, you know, 100 different programmers touch it.
Um, you know, you, you can barely even understand what's happening in one controller in 30 minutes.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, and y- yeah, it honestly was addictive, that experience of like, I can keep the entire system in my head. I can work with the system. I understand everything. Like, I-- there really are only two tools. Like, there's just Rails and there's just SQLite.
Aaron Francis
Mm.
Stephen Margheim
And if I can master those tools, um, I have an incredible amount of leverage available to me, and I can really build anything I need and, and without swimming in so much complexity that it's just impossible for me to have these kinds of quick turnarounds between calls. So that's how I stumbled into it.
Um, and then as these things go, like you fall in love with that part of the experience, and so as you start bumping into problems, you're like, "Well, I don't want to give up on this. So I'm willing to go through the pain to fix these problems so that I can keep this one thing I really love, which is this operational simplicity, uh, and sort of the simplicity of the mental model in the system." And so I, uh, found myself ready and willing to start doing work to smooth
the rough edges I started bumping into.
Aaron Francis
You mentioned, uh, y- you said simplicity, complexity, and operations. You, you said those three words a bunch of times. And so in your, in your role, it was more about operational simplicity and reducing the complexity of running the app.
And so when you, when you had that limited time constraint, SQLite pushed you to be able to do more things because it was easier to deploy. You don't have to keep a database server running. You don't have to put the database server somewhere. And so that was allowing you to... Is, is that, um, conceptual compression?
DHH always talks about that.
Stephen Margheim
Mm-hmm.
Aaron Francis
Is that like, is that a, a flavor of that, or is that something different?
Stephen Margheim
Yeah, I think it is. So for me, it was two kinds of simplicity, and the big one was operational simplicity, to say I only need to worry about one machine.
Aaron Francis
Yeah.
Stephen Margheim
I only need to worry about two dependencies on that machine, right? Can I get Ruby installed on that machine, and can I get SQLite installed on that machine?
Aaron Francis
Mm-hmm.
Stephen Margheim
And then when, you know, like you have this mental model in your head of like, what is this application doing? You know, what are all of the routes? How do they relate? Um, what is going on there? Like, I only need to understand these two things, right? There's not like, well, how exactly does Redis work? And how does... I'm using this Redis server with these configuration variables for caching-
Aaron Francis
Yeah
Stephen Margheim
... but this other Redis server with these other configuration variables for background jobs.
Aaron Francis
Mm-hmm.
Stephen Margheim
And but I'm using, you know, this MySQL database for active record, and so for this one code path where I'm saving these three rows to the database and then kicking off a background job, which I guess is gonna be there 'cause I'm sending an email, and then I'm caching this view. It, it's hard to keep all of that in your head. And when everything is just, it's just SQL.
It's just SQLite. There are files on the disk. There are these pieces of software I need to have running, and I can take a reasonable amount of time and master them. Like it's, you've done it, right? Like you can actually just read all of the valuable docs on SQLite.
Aaron Francis
Mm-hmm.
Stephen Margheim
It, it takes, uh, a couple afternoons. Um, so that simplicity was massive, and then the, that ties into the sort of simplicity of the system, right? It's like I actually can keep a mental model of what this application is and how it works, like in my head.
Aaron Francis
Mm-hmm.
Stephen Margheim
And so when I haven't looked at it for three weeks, and then I get a Slack message like, "Ooh, here's a bug I just bumped into," I can get back into flow inside of 10 minutes and then fix the bug in 10 minutes and deploy the bu- deploy the fix in 10 minutes.
Aaron Francis
Mm-hmm.
Stephen Margheim
Whereas like when you're dealing with these larger setups that are coordinating these, you know, six different tools, if you happen to be trying to fix a bug in a code path that touches five of those tools, like you have to just get all of that back into your head. Like, right, how do we configure Redis for background jobs?
Aaron Francis
Mm-hmm.
Stephen Margheim
Does that matter? Is this a part of the bug?
Aaron Francis
Mm-hmm.
Stephen Margheim
So that kind of simplicity was just, it was a completely different experience when you have like those actual requirements. Like I got the Slack message, and I have this call in 30 minutes, and it's a show-stopping bug. H- how do I make this work? Uh, and I, I could not find any setup that, at least for me, allowed me to do that until I just went all in on SQLite and said, "If I have any kind of persistent data need, I am working with a SQLite database file."
Aaron Francis
So I like a few things about that, and then I have a follow-up question. One, one, one of the things I think I like the most about this is there is, um, in, in the zeitgeist, uh, basically we only speak in absolutes. And what you're saying runs completely counter to all of that, saying, "Here was my situation.
Here was the way that I... Here, here are the constraints that I, Steven, had. Here's the way that my brain works, and I solved all of those constraints together." And it turns out vanilla Rails with vanilla SQLite was the answer for this set of constraints, which includes your own preferences and your own tastes.
And so I just want to say that I very much appreciate that, um, the, the point of view that's like, "What is my reality and how can I solve it?" Instead of saying, "No, SQLite and Rails is the only way you should ever do anything." You, you never said that. I don't know if you believe that, but you never said it. I don't think that's true, but I think what you have done is say This is a great solution for where I'm at, and I just, I really appreciate that.
Um, and then you can address that or the, the follow-up question comment is, before we get into more of like the nitty-gritty SQLite details, does your point of view on simplicity and operational simplicity, uh, extend to other areas? Like obviously you and I are gonna talk a lot about the database, but is this a point of view that you hold kind of across the fullness of the stack and maybe the fullness of products or your life or the whole thing?
But where do you stand on maybe, um, operational simplicity as like a, a philosophy or where do you stand on operational simplicity as compared to where the industry is going? So whichever route you wanna take.
Stephen Margheim
Yeah. Well, the, my answer to that question ties into your, your first statement as well, um, because I, I agree with you that the, the tenor of conversation that tends towards absolutes, um, is harmful to thinking well and thinking clearly about problems.
And so I am actually fairly passionate and dogmatic about the philosophy that you should try to build something as simply as possible, and that applies to every single layer of the stack, to every single aspect of the problem.
You should try, like how far can I get with just forms, with just GET and POST requests, with just HTML native elements, with just redirects? Like how far can I get with that? Can I actually... Like let's challenge ourselves to build modern, beautiful, uh, interactive experiences with just those tools.
And let's, like how many features and how many users can you serve on a single machine? Like do you have to have a coordinated fleet of machines? Like I've got my one machine with my load balancer, and it's talking to my three different machines with my application process on it, and I've got my one machine for my background jobs, and it has its backup, and I have my other machine for the cache, and it has its backup, and I have my three different machines that are physically separate to present a logically
consistent database, and it has its backups, and all of that is network and coordinated. Um, is that necessary on day zero? Like is that what it's, is that what we're presenting to people coming into web development? Yeah.
Aaron Francis
Mm.
Stephen Margheim
Come on in. The water's warm. All you have to do is learn everything about everything on day zero. All you have to do is understand like web application firewalls and port forwarding and how to have public and private connections between machines and why we would even do all... It's like, no. Like can, what can we do with, with less? And I think it's valuable both like for us as an industry, right?
Like to have a path for beginners to have like a graduation of complexity over time as your needs change and evolve and grow. But I think it's also really valuable for us as individuals, as like pr- practitioners and craftspeople, to remind ourselves that there is this temptation, I have felt it, uh, regularly, right?
To be like, "I want to make the right choice." And it's so easy for the thought there to be, well, the right choice is the choice that allows me to do what I need to do today, and if I change nothing, will allow me to do what I think will be the case in one week. And if I change nothing, it will allow me to do the thing that I think will be the case in one month, in one year, in five years.
And we like presume this series of like escalating complexity and escalating, um, needs, and like I should choose on day zero something that will work on year five.
Aaron Francis
Mm-hmm.
Stephen Margheim
And it's like we have no idea what's gonna happen on year five. And why, if you wanna give yourself the best chance of surviving until year five, like build a system that you can manage in 30-minute increments because it's so fast and so simple and so lean that it actually is in your head. That's gonna give you your best chance to being at year five and then like crossing that bridge. But I think it's so easy for us to get stuck on like, well, the right thing to do-
Aaron Francis
Mm
Stephen Margheim
... is to like, I, it's day zero, I'm trying to bootstrap my little app. I should use Vitest. If it's good enough for YouTube, it's good enough for me. It's like you don't have YouTube's problems.
Aaron Francis
No.
Stephen Margheim
You're not ingesting terabytes of data every 10 seconds. You... So do you have to take on all of that complexity? So that is, uh, a, a deep part of my philosophy that, uh, is fed by my interest in, in, in SQLite and also-
Aaron Francis
Mm
Stephen Margheim
... in meaningful ways originates from my work with SQLite and Rails.
Aaron Francis
Hmm. So if I am hearing correctly, um, I think I heard a, a few reasons why this philosophy is dear to you. One being it's easier to onboard people. It's easier to onboard people into a particular project, but also onboard people into the industry itself. And that was something that DHH said in the interview with him about SQLite, was the f- like people are going to give up, but the, the fewer reasons or the fewer
blockers we put in their way, the fewer people are going to give up. And so if starting with Rails new spins up a, you know, a SQLite file, and then you can keep going, and you don't have to stop to figure out what is Postgres, what is MySQL, is it running, how do I connect to it, then more people are gonna get further down the road.
So it sounds like that is one of your reasons. But it, I think at a baser level, it sounds like- Maybe the root reason is you believe simplicity is inherently good, and there's really nothing below that. Like it-- From that springs a lot of reasons, like it's easier to onboard, it's easier to come back to, it's easier to put the whole app in your brain, it's easier to pick a thing and move forward and fix it later.
Those all spring from a base reality of simple is better than complex. Is that right?
Stephen Margheim
Yeah. Yeah. I, it is right, and I, like, I honestly have this somewhat, like, geometric image in my head, like always, like, like from when I was a kid. Like, when I think about problems, they, they have a kind of geometry to them.
Aaron Francis
Mm.
Stephen Margheim
And it just... It's like the solution needs to fit the problem, right? Like, uh, I'm trying to remember this, you know, famous video of like, uh, a, a child putting blocks into this little toy, and there's like the star hole and the square hole-
Aaron Francis
Mm-hmm
Stephen Margheim
... and the circle hole and the triangle hole, and they're able-- they're just putting everything into the square hole.
Aaron Francis
Mm-hmm.
Stephen Margheim
And, uh, it is... It just doesn't feel right. It's like-
Aaron Francis
Mm
Stephen Margheim
... the star should go in the star hole-
Aaron Francis
Mm-hmm
Stephen Margheim
... and the square should go in the square hole. And even if you can rotate the triangle and fit it into the square hole, it's not where it's supposed to go. And so-
Aaron Francis
Not satisfying
Stephen Margheim
... if a problem only needs these things, like, that's all that I should apply to it. Um, and it is just like a aesthetic requirement-
Aaron Francis
Mm-hmm
Stephen Margheim
... in my, in my bones.
Aaron Francis
Yeah. It sounds so like, um, so visceral. Like I, I was gonna say, you know, why is simple better than complex? But we're at the bottom. Like we are at the, we're at the core. We're in your gut, and it's just, it just is. Like, it just feels correct, and I, I, I mean, I feel that exact same thing. It's like there's a sense of satisfaction when it clicks and you're like, "Yep, that's, that's the right thing." And usually that is, like, what is the simplest way forward.
And so yeah, I, I very much appreciate and, and, uh, vibe with all of that. So you're at, you're at the spot where you're, uh, pushing, you're pushing SQLite, vanilla Rails as far as it can go, and you start bumping into rough edges. So tell me about your journey from I'm using it out of the box to I am changing what's in the box so that other people have a better, like, a better first run experience.
How did that journey, including and up to now, your writing, you write a lot, so how did that progress?
Stephen Margheim
Yeah. So if anyone was trying to run a vanilla SQLite, a vanilla Rails application two years ago, they are 100% going to have experienced the same pain points I did. Uh, and the biggest, most prevalent one is that you're just gonna start seeing database locked errors in your logs.
Um, and you'll be using the application and, like, you don't see all these massive problems, but like you look at your logs, and they're just, like, streaming. Um, so it started there, and I wanted to understand, like, what is happening. Like, it's a, it's a problem. It definitely looks like a problem in my logs. It says error, and it looks scary.
Uh, I don't see a lot of degradation in the experience. I, I'm a little bit confused what's going on here. Um, so I, I, I dug into that and learned, uh, a, a lot of different things. We can get into those details. Uh, and as I went, I-- This was me, my phase of, like, reading more of the docs.
And I just started bumping into these things more and more, where the pattern was always the same. There's a feature that is available in Rails for working with databases. Um, so a great example here is, like, defining generated columns. Um-
Aaron Francis
Love generated columns
Stephen Margheim
... there's a whole-
Aaron Francis
Talk, talk to me about generated columns, buddy.
Stephen Margheim
Yeah, they're beautiful. I love them too. And I remember when that feature was released, uh, in Rails and, and all of the documentation for Postgres. I was like, "Ah, that is cool," you know? I just like that. Um, and not available for SQLite. Okay, that's fine. I'm just, like, reading the docs 'cause I'm trying to figure out, like, how does SQLite work? Like, what are these database locked errors?
Like, let me just master this tool a little bit more. And I just bump into, huh, SQLite does generated columns. Oh, it's done it for, like, a decade. Like it's- ... it, this is an old feature.
Aaron Francis
Yeah.
Stephen Margheim
Why can't I do that in Rails? Uh, and then I bump into another thing, like here's a feature. Uh, Rails has it, but Rails with SQLite doesn't have it, but SQLite has it. So Rails has it, SQLite has it, but when you put them together, for some reason, it's not there. Um, so I, you know, this is another one, just sort of like aesthetics.
This is just ridiculous, you know? All the code, all the hard work other smart people have done-
Aaron Francis
Yeah
Stephen Margheim
... it just seems that no one has cared enough to take the time to tie the knots. So I started bumping into, uh, there was like a handful of them, maybe four, maybe five. And I, I mean, I was like, I'm, I'm literally just copying and pasting code with minor tweaks. Um, this is, I mean, this is good work for AI, but it wasn't, uh, popular at the time.
So, um, I started opening those pull requests and, and digging more into, like, where are these database locked errors coming from and-
Aaron Francis
Mm
Stephen Margheim
... what's really going on there. And that was a, a longer investigation to really understand everything that was happening there, which we can get into. Um, and that energy and that activity just happened to, in some ways, I guess, coincide with ... um, this growing feeling that I had that I wanted to revive my blog.
Aaron Francis
Mm.
Stephen Margheim
I'd been quite active writing when I was in a- academia and sort of-
Aaron Francis
Mm
Stephen Margheim
... contributing back to here's how I'm using technology to be a better academic. Um, and it sort of died down as I transitioned out, and, um, I couldn't even get it running again years later. Like, all the, all the tools didn't even work-
Aaron Francis
Classic. Boy
Stephen Margheim
... on my new machine. Yep.
Aaron Francis
Talk about simplicity. That's a classic.
Stephen Margheim
Yeah.
Aaron Francis
My static site generator doesn't run anymore.
Stephen Margheim
Exactly. Um, and so I... And I'll just be 100% honest with you. Uh, no cap, as they say. Um-
Aaron Francis
As, as people not our age might say, yes. Much younger.
Stephen Margheim
Yeah, I mean, but I'm, I'm, I'm super hip. Um-
Aaron Francis
You're very cool. I agree.
Stephen Margheim
Yeah. But, um, you honestly did play a large role in, uh, helping me to, like, just get over that hump and say, like, "This is something I want to do. It's something that I enjoy doing. I feel... It makes me feel better," right? To just, like, I generate a l- or I receive a lot of value from people writing and creating videos and doing podcasts.
Um, and it has transformed my life, uh, my ability to get into tech and to find this thing that I really love and that I have some, uh, capacity for, and I do feel a kind of moral obligation to, to pay that back. Uh, and yeah, it was around the time of your Laracon somewhere talk, right?
Aaron Francis
Mm-hmm. Yeah.
Stephen Margheim
Of, like, make your own luck. Like, you just gotta do it. Do the work. Like, do work and then tell people about it.
Aaron Francis
Mm-hmm.
Stephen Margheim
So, um, it was just a perfect storm. It's like I'm, I'm doing this work, I'm investigating these things, bumping into some stuff. I see a route forward to get more involved in Rails Core.
Aaron Francis
Mm-hmm.
Stephen Margheim
Uh, and I was having this general low-level feeling like I wanna start writing more, and I wanna build a blog again, and then you just put it over the top. It's like, yeah, you know? Um, I resonated with a lot of what you said in that talk, and was like, "Okay, I'm just gonna do it." I took a couple afternoons, just got a basic blog up and running, and I, uh, I was just following your script, and I said, "Okay, here's the task.
I am going to publish something every day for the next week."
Aaron Francis
Hmm.
Stephen Margheim
So what, what am I doing? And I had the first two. Uh, my very first post I had, uh, bumped into and then, like, explored. Um, I want to do database branching the way that PlanetScale hypes it up. Um, I hope I'm allowed to use the word.
Um
Aaron Francis
You are. We'll, we'll bleep it out, but you're allowed to use it. No, I'm just kidding.
Stephen Margheim
But, um, it was... So I, I found like, hey, uh, I actually like doing branching with SQLite much better because I can bind my Git branch and my database branch together because it's just a file, and-
Aaron Francis
Mm-hmm
Stephen Margheim
... it turned out that it was quite easy to wire things together with Rails to, like, get this, uh-
Aaron Francis
Mm
Stephen Margheim
... behavior, where every time you have a new branch, uh, you can spin up a new database, and you can have it seeded and ready to go, um, from the jump. So, like, I had that first post, and then where am I gonna go? And so over the course of that week, I... Everything kicked off, and-
Aaron Francis
Hmm
Stephen Margheim
... then you start having conversations with folks, and those lead to more things. And I eventually, um, had a video call with the maintainer of the SQLite gem for Ruby, who I later found out was the manager of the Shopify team that employs many of the Rails Core contributors.
Aaron Francis
Hmm.
Stephen Margheim
And so when I was sharing with him my thoughts around like, "Hey, like, there's this feature in SQLite and in Rails, but not in the SQLite adapter in Rails, and there's this one, and there's this one, and there's this one," um, he was like, "Yeah, you should totally do that. Let me know when you open those pull requests, and I will ping people to actually look at them." And this is, like, the big hurdle. There's just too many pull requests.
Aaron Francis
Yeah.
Stephen Margheim
You know, Rails has, like, basically never had less than 400 open, I think.
Aaron Francis
Hmm.
Stephen Margheim
And so, um, yeah, that first week, uh, of really pushing hard, I just stumbled into some useful connections, and people actually looked at my pull requests. And then once you have a couple merged and you have a little bit of, like, credibility, it's easier.
Aaron Francis
Mm-hmm.
Stephen Margheim
People will look at the next ones and stuff. And so, yeah, from there, uh, it just sort of flowed, and now I've, I've gotten maybe 13 or 14, uh-
Aaron Francis
Wow
Stephen Margheim
... uh, changes into Rails, commits into Rails. Um, and the story for SQLite is massively different and massively better. Like, it is my personal goal. I, I love Laravel, and I love the community, but I want Rails to be the world's best web framework for running SQLite-backed web applications, and I feel confident that we are presently in the lead.
So-
Aaron Francis
That's great, and listen, I love Laravel, but I have nothing but respect for Rails, and I think that is a noble goal, and you should pursue it at all costs. Well, you should pursue it as much as you want. Um, I didn't know any of that, and I'm, I'm very honored that I played some small part in, in reviving the, the writing era of Steven.
That, like, that makes me really happy, so I- I'm honored, and thank you. Thank you for telling me. Um, and this, like, I, I need to start writing these, these case studies down because it is very easy if, if people weren't listening closely, they might have missed you decided to write for one week, and it changed your life.
Like, within, within one week- You're on the phone with the, the big boss inside of Shopify, you know, the big Rails person inside of Shopify, and you're like, you have an inside track. And it was, it was one week. And obviously that's not gonna happen for everybody, but it happened for you, and that was as a result of I am going to...
I think it's interesting 'cause it's like I'm already doing the work, and that's, that's the hard part, right? Doing the work is the hard part. Um, but then the emotional hard part is I'm gonna, I'm gonna tell everybody about it. And one week into that, you're on the inside track into getting PRs landed in, in Rails. And that just, that, I, that just makes me so happy.
I'm so thrilled to hear that that was, that was your experience.
Stephen Margheim
Yeah. And I, I mean, I'm thrilled to be chatting with you, uh, about it. But it was, it, it genuinely did happen just like that. Uh, and also, I think the other important takeaway, like as I look back on it, is that I did not, I didn't have a week's worth of content ready when I started.
It was also like, and I think that the other part of it, and this was something that I, I learned from you and that you talked about, and I do think it is important, is that, um, consistency helps a lot. Like you build up momentum. Like you can, you can take velocity, but you can also get acceleration, and you get acceleration through consistency. And so, you know, the very first blog post that I wrote, and I, you know, I tweet about it, and I push it out, then I have some followers, but not like many, and, um, a bit of something.
But about like on day five, right? Like it's just getting shared and like more people are hearing about it. And I am very, very confident that a large part of that was just through the consistency of like by day five, people are like, "This guy is like, I guess he's the SQLite guy, right?
He's just talking about it a lot." Um, and yeah. So you don't have to have it all figured out at the beginning. Uh, but consistency is an underrated point of leverage.
Aaron Francis
Mm.
Stephen Margheim
It just, uh, helps a lot. And, um, but then, yeah. You know, increase your surface area for luck.
Aaron Francis
Mm-hmm.
Stephen Margheim
And you s- you can get lucky.
Aaron Francis
You can. And it's, it's interesting that you can almost brute force your way into being the guy of something. Like if you just, if you just write about Rails and SQLite enough, people are like, "Yeah, he's the Rails SQLite guy. Oh, I, I'm in the Rails world and I have a SQLite question. Where's that Rails SQLite guy? What's his name again?" And it's, it's just you.
And that, that is like, that is an underrated or under, uh, discussed reality of at least our industry, maybe the world, is you can just do the thing enough and people will associate you with that thing, and then you become the person who is like inex- inextricably linked with the thing.
And I think, um, the false notion that people have is that there's a cabal somewhere that like, that promotes or nominates a person to be the person of something. And it's like, no, it's just kinda, it's all organic. And once-
Stephen Margheim
Mm-hmm
Aaron Francis
... you talk about something enough, people associate with you, that with you, and then there's no stopping it. Then it's a r- then it's a runaway train because everybody is continually piling on that, "Yes, Steven is the Rail SQLite guy." And I think that is under-discussed and strategically, uh, exploitable. Not in like, not in a bad way, but like you can-
Stephen Margheim
Yeah
Aaron Francis
... take advantage of that strategically if you want to be the something, like if you wanna be the person of something, you can, you can just do that yourself, and I think that's pretty exciting. Um, okay. I wanna talk about, um, I wanna talk about some of the nitty-gritty, uh, gory details that you discovered. 'Cause you wrote this really interesting, in my opinion, uh, really interesting backoff thing instead of the busy, the straight busy timeout.
Um, you discovered that the Ruby... I, I, you'll have to forgive me on some of the specifics.
Stephen Margheim
Yeah.
Aaron Francis
The Ruby or Rails blocker was blocking or, or the wait, um, the original SQLite wait was blocking, and so that just kinda hosed everybody and everything. And then you have a couple of, um, blessed or suggested pragmas that you discovered work best with Rails. So any gory detail you wanna talk about, uh, the audience is technical, so don't be shy.
But tell us some of those things you discovered and where you fixed them, whether that's in Rails or this SQLite gem that you mentioned or somewhere else. Talk us through that.
Stephen Margheim
Yeah. So we'll start with one of the earliest contributions, which were around, uh, the initial pragmas that you set up when you initiate a connection to SQLite. So Rails already had a layer in its stack, so whenever it would spin up a new connection to put into its connection pool, it would, uh, turn on foreign keys.
So it would run that pragma.
Aaron Francis
Mm-hmm.
Stephen Margheim
And so every connection, foreign keys are turned on. Um, and if anyone, uh, goes and Googles, you know, like what are the recommended pragmas for SQLite, you'll find a very consistent overlap of six or seven pragmas.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, so the very biggest one, and, and you talk about this in your course, right, is that like getting into WAL journaling mode out of the default, uh, rollback journal gives you concurrent reads. It's gonna open up your performance characteristics a lot, especially for, uh, the kinds of applications that are common in Rails and Laravel, which are CRUD-centric applications, so those tend to have a power distribution between reads and writes.
80% of your operations are gonna be reads. Um- Then you can, when you're in WAL mode, it's a safer sort of risk trade-off to downgrade your synchronous pragma so that you're not flushing to disk after every single operation, which is one of the most expensive operations that SQLite does.
So that's just gonna give you more, uh, performance headroom. Uh, and then like, you know, increasing the size of your memory cache. Like, this is another thing that I think it's, it's easy for people to not register, right? Like, ah, Redis, it's so fast. It does everything in memory. It's like every single database in the world does most of its work in memory.
The disk is there as the persistent like backup. It's just like it's gonna eventually get there. But, um, most databases want to do as much as possible with data that it keeps in memory. And with SQLite, you can configure like how, how big of a block of memory does it have to hold data?
And, um, you know, especially again, if you're coming from this mindset of like, what am I doing on day zero when I'm just starting an application up? You can get incredibly far with like your entire database living in memory. Like you can have the speed of Redis with SQLite. Um, and like I actually had a DM from a guy who was like, "Hey, we're trying this experiment out of like replacing Redis with SQLite for this particular, uh, application.
And we're running these benchmarks, and SQLite is faster than Redis, but we're getting like the full like power of SQL. So I know I set something up wrong. Can you help me figure out what I did wrong?" And we look at it-
Aaron Francis
You help me make it slower.
Stephen Margheim
Make it... It's like, "I, I must be... You know, where's my bug?" And we look at it, I'm like, "No, no, no. You see, uh, your data is smaller than the memory range. SQLite's doing everything in memory, and it's just better tuned for the operations you're asking it to do than Redis is for that in-memory work." So there's that. There are a few others. Um, so that one was easy.
Like I didn't have to introduce a new sort of like concept or layer of abstraction into Rails.
Aaron Francis
Mm-hmm.
Stephen Margheim
It was just in that place. Instead of running one pragma, run seven. Um, and that was an immediate improvement. Um, and-
Aaron Francis
Is that the first one you landed?
Stephen Margheim
That was the first notable one. The very first one I landed was allowing for the pipe CONCAT operator in default function. So Rails has a, an allow list of the functions you can use when you're defining, or the operators and, and method SQL functions you can use when you're defining a default function. Uh, and the pipes weren't in there, and SQLite doesn't have like the CONCAT all caps, uh, function.
Aaron Francis
Mm-hmm.
Stephen Margheim
So that was my very, very first one.
Aaron Francis
Hmm.
Stephen Margheim
Uh, and then the new set of pragmas was the second. But the really gnarly change was around these database lock errors. And it took me-
Aaron Francis
Yeah
Stephen Margheim
... a while to really track down like what was going on. So I'm gonna try my best to explain it because-
Aaron Francis
Yeah, take us there
Stephen Margheim
... it's, it's deeply connected to the way that Ruby works. So Ruby, like other programming languages, Python is the same way, uh, can be multi-threaded, right? You can spin up multiple different threads, um, that are executing Ruby code. Ruby is an interpreted language. Um, but the way that it is architected, and this is true of, of many of these interpreted languages, you know, created in the '80s and '90s, uh, it only allows
for one, let's call it execution context of Ruby, to be running at a time. So it has what it, it calls a global virtual machine lock or a global interpreter lock. Um, so at the Ruby level, like the C code that is executing your Ruby code, um, it bottlenecks everything down to, um, we can only have concurrency, not parallelism, right?
So you can spin up multiple threads, and they will coordinate on like, "Hey, I wanna go, I wanna go." "Okay, you get to go. You can run some Ruby. You take a break. Now you come in, you can run some Ruby. You take a break." And, um, this, you know, concurrency works great. Like you don't need parallelism to get fast performance. Uh, and for web applications, it works, uh, pretty well because you necessarily have a mix of I/O.
So you have a web request come in, and you need to run some Ruby to like process, parse that text blob that came in and-
Aaron Francis
Mm-hmm
Stephen Margheim
... generate like a request object and then pass it through your router and do things. And then as it gets into Rails, you're gonna need to do some, uh, some CPU work. You're gonna need to talk to a database. You're gonna n- go and make some other third-party HTTP requests. So when your thread that is processing that particular web request is making a third party HTTP request, the Ruby interpreter's like, "Cool.
You're hanging out. You're doing external I/O. I'm gonna let this other thread run." And then that thread will be, "I'm gonna pick up a new request." And so you've got like this standard image of concurrency that you've seen, where you've got these three different threads running and, uh, they're able to do different kinds of I/O in parallel, but you're only able to do Ruby, uh, sequentially.
So when you're working with most databases, like everything's great because when you make a call to MySQL or you make a call to Postgres, whether it's on another machine or on the same machine, it's in a different process. So it is definitionally external I/O. That's how the computer sees it. You don't have to do anything for Ruby to see that's different I/O.
Aaron Francis
I see where this is going.
Stephen Margheim
And so I'm just gonna- Do some other Ruby.
Aaron Francis
Mm-hmm.
Stephen Margheim
SQLite is great, and one of the thing that makes it great is that- ... it isn't an external process. You don't have the overhead of interprocess communication. It's embedded into your process of the web application.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, and typically, this is a really great speed boost. Um, like, just that interprocess communication is, like, a, can be about a 10X difference in performance. But in the context of Ruby, what that means is that the Ruby interpreter, when you are talking to SQLite and SQLite is running, like SQLite's C code is being executed, the Ruby interpreter doesn't think that that's external IO.
It thinks-
Aaron Francis
Mm
Stephen Margheim
... I have to keep running this Ruby thread. And so no other Ruby threads are able to run any Ruby.
Aaron Francis
Mm.
Stephen Margheim
So you can't pick up and start processing another web request. And so it bottlenecks your performance. You've got this multi-threaded web application. You're supposed to be able to be processing five requests concurrently, and you can't. And what... So that's like-
Aaron Francis
So let me, let me, let me say it back to you
Stephen Margheim
... core situation. Yeah.
Aaron Francis
We'll see if I got it. Let me say it back. So, uh, Ruby is concurrent, and so typically it's just bouncing back and forth between three, four, five, six, uh, as many concurrent processes as are running. As a process reaches out to some third party, uh, the, I guess you said, the C code looks at that and says, "All right.
We're waiting on somebody else. Let's pick up another Ruby thread and do some work, and then we'll come back to this guy when it's done reading off the disk or waiting for the API or whatever." That works great, very elegant, very fast. Um, because of the way that Postgres and MySQL exist, which is over a network, that is by default external.
And so Ruby looks at that and says, "Great. External request. We'll see you in a little bit. I'm gonna go do some other work." However, with SQLite, it is not by default an external request. And so if you hit SQLite, and it says, "I'm busy. Let's wait for five seconds," Ruby says, "Okay, I'm still working, and nobody else can do any work because I'm still working with this, this SQLite thing that is not external." Is that right?
Stephen Margheim
Right.
Aaron Francis
Okay.
Stephen Margheim
So by default-
Aaron Francis
So there's the problem
Stephen Margheim
... from the point of view of the Ruby interpreter- Yeah, it sees the SQLite work as Ruby work, and so it doesn't switch out to another concurrent thread to process web requests.
Aaron Francis
Mm.
Stephen Margheim
And so yeah, you, you, you took the next step to the core problem, which is, so you set up basically all web application frameworks these days will set up, uh, timeouts for database connections. Say, "Hey, uh, if I reach out to the database, and it doesn't respond in five seconds, like, just cancel," you know?
Uh, and typically that's, you know, that's a configuration that you set on the database. So you tell the database, "Just, just quit after five seconds." That's, that's the default in Rails, five seconds, whatever you set it to. And so because... So we have this one situation, the way that Ruby deals with concurrency and can only have one Ruby thread executing Ruby at a time.
Then you have the other re, uh, sort of bottlenecking situation, which is on SQLite's level, right? That SQLite only supports sequential writes. So when you're in WAL mode, sure, you can have concurrent reads. You can send five different connections at SQLite and have them run five completely different select statements, and SQLite will serve them, um, in parallel, those five different connections.
But if you're doing an update or an insert, uh, it won't. It has to line those up and say, "I'll do this update, and then once it's done, I'll do this insert." And so this bottleneck, um, the w- the way that SQLite provides some control to you as a developer is to say, um, how long are you willing to let a write operation wait
before it runs? And so when you set a timeout in Rails, that's what you w- that is what you are setting. That's the configuration that you're actually doing in SQLite. And, um, what SQLite will do is it has this basic implementation, and it's in SQLite C code, that's gonna do, like, a... It's not quite exponential back off, but it will check in one millisecond, and then two milliseconds, and then three milliseconds, then five, and then 10, and then 10, and then 10,
and then 25. And, um, to say, "Hey, uh, am I able to do a write? Like, can I acquire the write lock?" That's how SQLite manages these things. Um, and it's gonna run through those. And so what happened is that when you put these two things together, um, a Ruby thread is processing a web request.
It gets into the Rails application. The Rails application makes a call. It's like, you know, post.create. That gets converted by all the Rails code into a command to SQLite to run an insert. And that Ruby thread sends that command to SQLite in, you know, embedded inside that same process.
And the, there is some other web request, some other connection to the database that is currently working on that database. And you, your current request is not able to make that write, and so it just enters into SQLite's backoff. It's like, "Okay-
Aaron Francis
Mm-hmm
Stephen Margheim
... I'll, I'll hang out. I'll check back." And while it's doing that- Ruby thinks Ruby stuff is still happening, and so it, Ruby doesn't switch out. And so the combination of these two things basically mean that, uh, you go from a multi-threaded web application to a single-threaded web application- ... uh, as soon as you have two that, that hit.
And, um, so this was one of the core problems. There's two core problems. This is the first and gnarliest, um-
Aaron Francis
Mm-hmm
Stephen Margheim
... sort of this combination of the, the way the place where Ruby bottlenecks and the place where SQLite bottlenecks, and the nature of how they connect-
Aaron Francis
Mm-hmm
Stephen Margheim
... uh, will completely bottleneck your entire application. And so you would look at it and you would see two things. One, it would be incredibly slow. Like, you run basic benchmarks and your requests per second would look odd. Like-
Aaron Francis
Mm-hmm
Stephen Margheim
... "Huh, I thought SQLite was the fast database." And then the second thing you would see is you would start to see randomly these, these database locked errors, because if you had... So especially when you're running benchmarks and you're sending a lot of requests-
Aaron Francis
Mm
Stephen Margheim
... concurrently, there would always be the unlucky ones, like the very worst ones who are at the end of the line, and at the very end of the queue, they don't get to the front of the queue within the timeout period, and they would throw. Um, but like when you're running the application in production and you don't have like a ton, you know, you don't actually have that much concurrent load-
Aaron Francis
Right
Stephen Margheim
... you very rarely see it. Um, so you have that core problem. I'll pause there before we talk about the second problem and the solutions.
Aaron Francis
Yes. Okay. So this makes, this makes perfect sense to me. Knowing what I know about SQLite and learning what I have now learned about Ruby, this makes sense to me why this would be a, um, kind of like a, a funny coincidence of errors here. Because the way that they work together creates a pretty huge problem, and it's interesting that, um, by its nature, SQLite kind of worked backwards up the stack to make Rails single-threaded or Ruby
single-threaded based on its like its nature. Um, and that is, uh, it feels, it feels very funny that it's poisoned the whole stack because of one technical implementation. And so I'm, I'm interested, I think I know where... Uh, this is helpful for me 'cause I've read all your writing about this stuff and didn't quite, like not knowing Ruby and Rails, didn't quite grasp, uh, the severity of it, and now this makes perfect sense.
So, um, let's hear either the solution or the second, the second gnarly problem, um, 'cause I'm curious what could be more gnarly than that.
Stephen Margheim
Yeah. So the solution to this problem is that we have to stop using SQLite's backoff implementation.
Aaron Francis
Mm.
Stephen Margheim
Uh, because there's no way... I can't change that code, so I can't tell it, "Instead of like using OS level sleeps-
Aaron Francis
Mm-hmm
Stephen Margheim
... I need you to use a sleep that Ruby is aware of so that Ruby sees, 'Ah, you're sleeping. You're just idling.'" Um-
Aaron Francis
I see
Stephen Margheim
... and so luckily-
Aaron Francis
Because tell me the default implementation in SQLite. That is something that Ruby can't see as idle. Is that right?
Stephen Margheim
Right. So when it calls... It, it's basically sleeping for different durations of milliseconds.
Aaron Francis
Uh-huh.
Stephen Margheim
But it's just at the lowest level. So it's calling your operating system sleep. And, um, Ruby-
Aaron Francis
And, uh-
Stephen Margheim
... has a Ruby sleep, which basically says like, A, I will tell the operating system to sleep, but B, because I'm Ruby and I am single execution context, I will flag, "Hey, this thread is idling. Allow another thread to start running interpreted Ruby."
Aaron Francis
Got it. Okay.
Stephen Margheim
Um, so it has that side effect. Um, but SQLite has the two levels. You can use their default implementation of how to handle a situation where a connection, uh, is busy, or you can write your own.
Aaron Francis
Hmm.
Stephen Margheim
And the Ruby driver for SQLite exposes that interface. And so we wrote a Ruby implementation of a backoff. And instead of... And we call sleep from the Ruby programming language, and sleep in the Ruby, Ruby programming language tells Ruby the interpreter, "Hey, oh, this is ex...
Like I'm idling. Another thread can now come in and execute." Um, so-
Aaron Francis
So it-
Stephen Margheim
... that was all it really took. Once I understood what was actually happening, and that took a long time, the solution was, was relatively straightforward.
Aaron Francis
So SQLite exposes, um, several different ways you can customize it, like a whole lot of different ways, and I've never, uh, I've never done it because I have never needed to do it. So the one that I think you were hooking into is the, the busy handler.
Is that right?
Stephen Margheim
Yes.
Aaron Francis
Okay. So you're hooking into the busy handler. Um, the SQLite gem, the Ruby SQLite gem already had a hook into the SQL or, or the busy handler. Is that right?
Stephen Margheim
Yes.
Aaron Francis
And then what are you writing? Um, because from my understanding, those hooks, you're writing the C code that modifies like SQLite itself. So are you writing C code that is somehow Ruby flags that sleep process or how-
Stephen Margheim
Right
Aaron Francis
... what is the actual detail there?
Stephen Margheim
So I did not have to write C code for this solution.
Aaron Francis
Hmm.
Stephen Margheim
The SQLite gem in Ruby is the driver, and what, what that means is that it has a bunch of C code which, uh, uses SQLite's C APIs-
Aaron Francis
Mm-hmm
Stephen Margheim
... and uses Ruby's C APIs to tell-
Aaron Francis
Oh
Stephen Margheim
... the two how to talk. And so it allows me to write Ruby- And drive SQLite's C APIs. And so somebody has-
Aaron Francis
Uh-huh
Stephen Margheim
... to write the code to bind them together. So it had already been done-
Aaron Francis
Cool
Stephen Margheim
... that somebody wrote the C code that exposed SQLite's busy handler C public function-
Aaron Francis
Mm-hmm
Stephen Margheim
... exposed it through Ruby, and allows me using the gem-
Aaron Francis
Mm
Stephen Margheim
... to have a Ruby object that represents the SQLite database, and to define a-
Aaron Francis
Hmm
Stephen Margheim
... code, a block of Ruby code that will be executed when SQLite calls the busy handler. So SQLite calls the busy handler itself internally in C.
Aaron Francis
Mm-hmm.
Stephen Margheim
The Ruby SQLite driver has C code that says, "Hey, I've registered a busy handler."
Aaron Francis
Mm.
Stephen Margheim
So SQLite calls the driver's C code, and that C code says, "I'm now gonna execute some user-defined Ruby."
Aaron Francis
Mm-hmm.
Stephen Margheim
And it executes that Ruby-defined Ruby, and then once it's done, it tells the SQLite C code in the Ruby driver C code, "The user-defined Ruby code is now done," and that's, like, how it flows. So it, uh, it had to do that-
Aaron Francis
That is pretty awesome.
Stephen Margheim
Yeah. And it-- I actually have now become a bit of a C programmer, uh, 'cause I've, I've contributed a few, uh, improvements to the SQLite gem, which are just those things. Like, here's a SQLite C function that is a public function exposed in their-
Aaron Francis
Mm-hmm
Stephen Margheim
... public API, but it's not exposed to end users in Ruby-
Aaron Francis
Right
Stephen Margheim
... through the driver. So let me learn enough about C and about the SQLite C API and the Ruby C API to just bind those two things together. Again, this is-
Aaron Francis
So you're, you're-
Stephen Margheim
... a very common theme of the work I do. I just, all smart people do really complicated work on two problems, and then I just come along and tie a little bow between them. And, um-
Aaron Francis
I feel that, yeah. And then, and then we get all the credit-
Stephen Margheim
Reap the benefits
Aaron Francis
... and it's awesome.
Stephen Margheim
Yeah.
Aaron Francis
Yeah.
Stephen Margheim
And then I just, I, I tweet about it more, so so...
Aaron Francis
There's actually a lesson in there. Um, so this is really interesting to me, that a lot of, um... It sounds like a lot of this SQLite Ruby gem is building, um, either bridges or bindings or whatever from-
Stephen Margheim
Yeah, bindings, yeah
Aaron Francis
... SQLite C into, uh, user-defined Ruby, which is really fascinating to me. Because one of the things that I have always felt, um, with the SQLite C API is it's beyond me. I know that that's not objectively true, but I have felt that way because I don't, I don't know any C. I don't really have a desire to know any C.
I think should I encounter a situation like you did where I needed to do that, that would be enough to push me to be like, "Ah, th- I'm trying to solve a problem, so along the way I will learn something." But I've never had to do that, and so it feels really inaccessible to me. But this whole, like, let's build a lightweight bridge binder thing to get over back to the language that is more accessible to me, that's really cool.
Because I think in your, um, in your blog post about the new backoff that you wrote, it was all Ruby. And I was like-
Stephen Margheim
Mm
Aaron Francis
... "I don't understand how this is working. How do we get, how do we get from here to there?" But now it all makes sense. This is very cool.
Stephen Margheim
Yeah. So it, it's amazing that there are these great people in open source. I mean, so the, the PHP SQLite driver is doing the same thing. Um, and across all the different languages, JavaScript and Python and Ruby, there are people, somebody or groups of people, have gone in and, like, written low-level systems code that, you know, where you have to really master SQLite, and you have to really
master the programming language you're binding into.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, and yeah, I mean, without that work across all of the different programming languages that, um, have drivers for SQLite, it would definitely not be the most widely used database in the world.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, but yeah, once you start to understand what's going on, and it, it gets demystified a little bit, and huh, like, I wonder if I could do that. And then, like, I would really like to be able to get that information out of SQLite. I mean, I futz around a little bit.
Aaron Francis
Mm.
Stephen Margheim
At this point, AI had come around, so I definitely, uh, used the, the aid of AI a bit. Like, okay, tell me, t- what is... Here's a line of C. What is this? Why are there all these asterisks? Like, what's happening?
Aaron Francis
Right. Yeah. What the hell does any of this mean?
Stephen Margheim
Oh, it's a pointer. What is a pointer? So I had a lot of great conversations-
Aaron Francis
Interesting. What is a pointer?
Stephen Margheim
... with AI.
Aaron Francis
Tell me more about that. Yeah.
Stephen Margheim
Yeah.
Aaron Francis
So what was your, um, what was your... This one thing that you did differently was you used a Ruby-friendly sleep. Did you do-
Stephen Margheim
Mm
Aaron Francis
... anything differently in this busy, uh, handler? Did you change the backoff algorithm?
Stephen Margheim
Yeah. So this was the other big change. Um, and honestly at some point I'm, I'm interested if I can, if there is some archeology around, like, where this came from. Um, but the SQLite's implementation where you have a backoff.
So at first-
Aaron Francis
Mm
Stephen Margheim
... you're gonna wait one millisecond, and if you can't write, you'll sleep for two milliseconds, and you check again. And you, if you can't, you'll wait five milliseconds, and you'll check again. Um, I don't know where it came from, but in the context of multithreaded applications, like when you have multiple connections open to the database, it kills long-tail performance.
Aaron Francis
Hmm.
Stephen Margheim
Because it penalizes old queries.
Aaron Francis
Hmm.
Stephen Margheim
And if you have a lot of contention, so if you're, if you have traffic, then you are-
Aaron Francis
Oh, I see
Stephen Margheim
... going to get old queries. So what that means is that, so you think about, let's just say it's one, two, and five milliseconds So on the third retry, this query is gonna wait five milliseconds. And the th- the thing that really unlocked me understanding what was going on was recognizing that when I think about backoff queues, for me, the mental model that comes to my mind, 'cause I know it, is background jobs.
Aaron Francis
Mm-hmm.
Stephen Margheim
And in the world of background jobs, the, the semantics are subtly different. Because when you say wait one second, what you're saying is wait one second and then execute, and you have a guarantee effectively that it will execute in one second. So you're just saying, "Just wait to run this amount of time." In SQLite, what's happening is that you're saying, "Check back in, in one millisecond, and maybe at that point you'll be able to execute or maybe not." And in that kind of context, if
you have a lot of contention, as soon as you have a query have to wait, retry three times, on that third retry, it's gonna wait for five milliseconds. And that means if new-
Aaron Francis
Mm
Stephen Margheim
... queries are coming in and they're only waiting one and then two milliseconds, every new query will be able to try twice before this query can try again once, and it just gets worse and worse and worse. And so-
Aaron Francis
Mm-hmm
Stephen Margheim
... once you get into the fifth or greater retry for any particular query, the odds of it going to 20 retries go way up. And once you're at 20, the odds of it going to 100 retries go way up. And so-
Aaron Francis
That makes sense
Stephen Margheim
... under contention, you will always have some queries that go all the way to the end, that never execute. The end-
Aaron Francis
Because all the new guys are sneaking-
Stephen Margheim
Because all the new guys-
Aaron Francis
... in front of them.
Stephen Margheim
Yeah. Keep sneaking to the front of the line.
Aaron Francis
Hm.
Stephen Margheim
Exactly. And so for multi-threaded contexts, it is really important that you don't use the busy timeout. And the busy timeout is just SQLite's own default implementation of a busy handler.
Aaron Francis
Hm.
Stephen Margheim
It's using the same API. It's just like, "Here, here's a busy handler that we've built ourselves."
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, but y- if you are in a context where you're going to have multiple connections open to SQLite, which is going to be the default for most web applications today, um, I strongly believe that you just can't use the busy timeout. You have to define your own busy handler, and you have to simply retry the same.
So the way we implemented it is that every query, every retry just waits one millisecond and checks back in.
Aaron Francis
Hm.
Stephen Margheim
And after doing various benchmarks, what we found was that, like, the worry obviously is, is CPU thrashing, right?
Aaron Francis
Mm-hmm.
Stephen Margheim
Like you're just checking in a lot, and is that going to create other problems, like create pressure on the rest of the system? And in our benchmarks and testing, the answer was no. Um, of course, always, right? Like write your own benchmarks, test things out. Could be different in different contexts with different programming languages, who knows what. But in general, that philosophy of don't actually back off.
Just give every query, every retry the exact same chances of getting an, the write lock and actually executing.
Aaron Francis
Mm-hmm.
Stephen Margheim
And that will, uh... Like I have these graphs in the blog post of like the, the P99 or P9999 latency when you go from like four concurrent web requests to eight to 16 to 32, and they're just stairstepping and you get-
Aaron Francis
Mm-hmm
Stephen Margheim
... some, you know, your, your P99 will be five seconds. Um, and you just change that, boom. It immediately flattens out and you'll get the, like your P four nines will be like 500 milliseconds, as opposed to like 15 seconds. That was honestly like-
Aaron Francis
Mm-hmm
Stephen Margheim
... the jump for when you have like 64 concurrent web requests you're throwing through a benchmark. So that's a massive-
Aaron Francis
How did it-
Stephen Margheim
... change
Aaron Francis
... how did it feel when you, when you cracked that? Like when you first typed in one comma one comma one comma one instead of 125 1520, how did it feel when you, when you ran that benchmark?
Stephen Margheim
Yeah. Ecstatic. I won't lie. Uh, especially for that one, because it was such a deep rabbit hole of like-
Aaron Francis
Yeah
Stephen Margheim
... what is happening here? Like what, like to, it took a long time to really investigate like all the different layers and, uh-
Aaron Francis
Hm
Stephen Margheim
... and, and then like the, the, for that particular point, it was like I thought I had figured it all out, just the change to be like, "Don't do C-level backoffs, do Ruby-level backoffs."
Aaron Francis
Yeah.
Stephen Margheim
And then, um, and I do the initial benchmarks and everything looks good, and I just notice like just, it's like that's a little bit of a stairstep. I'm just looking at P99s and like around 32, it's like a little bit like, eh, I wonder. And I just look at the, at the benchmarking tool I have also has four nines, so P99.99. And I just look at that and I put it in a graph, and it's like, whoop, which is the exact same shape that my P99s had that I was trying to solve.
And I'm like-
Aaron Francis
Right
Stephen Margheim
... "What is going on?" And, uh, then, so it's like I, I thought I was done. I thought I had-
Aaron Francis
Mm
Stephen Margheim
... cracked it out on this like really complicated problem, then I immediately see this other problem, and once I cracked that, I'm like, "Oh, okay. Everything is coming together, and I have like every chart I have is completely flat." Um, uh- ... that's when I knew like, okay, this is... It completely changes your experience working with SQLite in Rails applications.
Aaron Francis
Hm.
Stephen Margheim
Like before these changes, it was bad. It just was.
Aaron Francis
Yes.
Stephen Margheim
I, I have, I've given some conference talks and I have this big slide and I say, "It is not viable out of the box. Like, don't do it. It won't go well. You're gonna be angry. You'll have problems. It's not worth it." Um, and in the new release of Rails, all of these fixes will be in there by default.
And so-
Aaron Francis
Mm
Stephen Margheim
... starting in the fall, and, and a lot of them are already there in that we have a, a release candidate out for Rail 7.2 right now, and a lot of these fixes will be in there. Not all of them, but a lot of them. Um- We're finally getting to a place where the out-of-the-box experience actually is production ready, and that was one of, like, two major pieces, um, to get it done.
And so when I finally cracked, like, that final lever, it's like, "Oh, this is a great relief."
Aaron Francis
Yeah. That's amazing. I, I am well acquainted with the idea of cracking a really hard problem and then realizing that there are more problems underneath. And, you know, sometimes being able to crack those, but it sounds like you, you got to the very bottom of it, and that is, like, that is very impressive, and you should be, you should be proud of yourself.
And I'm glad that you are-- you have been writing about it because this is incredibly interesting. Like, this is, this is interesting work. Um, and I have, as I've mentioned, thoroughly enjoyed reading about it. So it sounds like some has, some have already landed in 7.2. The, uh, fullness of the fixes will land in 8.
Do y'all do major version?
Stephen Margheim
Yep.
Aaron Francis
Okay. So it'll land in 8, and that sounds like you said that's coming this fall. Um, so what is... As, as we wrap up here, what is, like, your-- what does your future hold? I think you've given a few talks on SQLite and Rails. Um, so it sounds like you are firmly, um, you're firmly in the, I don't know if it's the content game or, or whatever, but you're, you're out there, and people, people know you as this guy.
So what is your future? What do you want it to be? What do you see yourself doing in the next couple of years? Um, and where should people follow along?
Stephen Margheim
Yeah. So I have found that really my core driver, for whatever reason, and I honestly don't even know all, all of the reasons. But, um, I really feel passionate about making the default vanilla out-of-the-box experience for Rails developers who want to
embrace this philosophy of going all in on simplicity, like trying to compress as much compli-complexity as possible, to make that experience seamless and easy. And there is still some work to be done there. So this whole problem and fix that we're talking about, this pull request is still open.
Aaron Francis
Mm-hmm.
Stephen Margheim
So that won't even be in 7.2. That's, like, coming for, for 8. Um, the other big problem, which we don't have the time to talk about much, but is around the difference between deferred transactions and immediate transactions.
Aaron Francis
Mm-hmm.
Stephen Margheim
You're also gonna get database locked errors by default using deferred transactions in a multi-threaded environment, uh, because the way that SQLite acquires the write lock or where, where it tries to do it, like you, you just need to use immediate transactions. If you wanna learn more, blog posts are on my site.
Aaron Francis
Mm-hmm.
Stephen Margheim
I go very deep into it. But, um, you know, that pull request has been merged. I'm not 100% certain if it's in the release candidate yet for 7.2. Might just be in 8. Uh, so there's still work to be done there. And then there's also work to be done on the deployment story. So-
Aaron Francis
Mm.
Stephen Margheim
Heroku has been the big force in deploying Rails applications for about a decade now, and you just can't use SQLite on Heroku, right? They have an ephemeral-
Aaron Francis
Right
Stephen Margheim
... file system, and it's just not gonna work. Don't even try it. There's, there's not workarounds. Don't waste your time trying to find workarounds. It just doesn't work. Don't use it. Um, so the alternative is you get a VPS, and you get it set up, and you manage it yourself. And that's, like, I'm a sicko, and I happen to kind of enjoy that experience and, like, learning those things and improving my, my skills in those
areas. But I recognize that for a lot of people, like, that's a, a lower value use of their time to, like, become, uh, system administrators.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, so I believe that the simplicity of this stack allows for a simplicity in deployment. And so some of my, like, my current work, um, that I'll be writing more about, um, in the coming weeks is around, like, okay, what d- what does it take to get one of these, um, hosting providers, um, just like a bare metal VPS, to get it set up to run a Rails application where you've just gone
all in on SQLite, and you can do everything on one machine, and you don't have to have-
Aaron Francis
Mm
Stephen Margheim
... 18 different services installed and properly running and properly configured to work with each other. Um, and then at, like, the longer lens, and part of me is thinking right now, like, I shouldn't even say this because I'll probably never solve this problem. But I will be honest about, like, I, I have honestly dreamt about this problem multiple times.
And, um-
Aaron Francis
That's a heck of a buildup. You gotta tell us now.
Stephen Margheim
At some point, somebody, maybe me, but so- like the world would be an amazing place if somebody cracked the nut of solid non-blocking zero downtime migrations in SQLite. This is a major problem. I, I love a lot of things about SQLite, but if you have a really big database-
Aaron Francis
Yep
Stephen Margheim
... uh, and you go to add an index, there's nothing you're g- You're-
Aaron Francis
Yep
Stephen Margheim
... you're gonna need to put up a banner and say, "Hey, guys. Let's just bring the system down from 3:00 AM to 4:00 AM on Tuesday."
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, that's gonna be your safest bet for sure. Um, so- And I, I really am deeply jealous of all the other databases and the infrastructure of open source tools that have sprung up around them with all the different ways to do zero downtime migrations of various kinds. And I can't help... It's like everything is possible, right?
Like-
Aaron Francis
Mm-hmm.
Stephen Margheim
These are just... We're talking over rocks with electricity in them. Like, everything is possible. Of course, it's possible. It's just hard. Um, so I would love to crack that nut.
Aaron Francis
That's what makes it so enticing.
Stephen Margheim
It does. And I've thought about it, and I've got some, I've got some wild ideas. Um, so that's like my white whale.
Aaron Francis
Mm-hmm.
Stephen Margheim
And if I ever solve that problem, like I can't even begin to guess what that elation will feel like.
Aaron Francis
Mm-hmm.
Stephen Margheim
Um, in terms of following along, um, I am Fractaled Mind everywhere. Um, I was actually thinking earlier that, hmm, I think that that is connected to my obsession with simplicity.
Aaron Francis
Mm-hmm.
Stephen Margheim
Uh, we can have that conversation later.
Aaron Francis
Mm-hmm.
Stephen Margheim
But, so my blog is fractaledmind.github.io. I'm Fractaled Mind on GitHub. I'm Fractaled Mind on Twitter. I'm Fractaled Mind on Yoto- YouTube. I have a, a few video series I've done there. And I really do... I make sure to say this at the end of, of any of these podcasts or interviews I have. Um, I benefited massively from people active in the community who were willing to give me some of their time and some of
their attention, and help me out and talk with me. And I'm fully aware of how necessary that was for my advancement and progression in my career, and just learning about things, and feeling excited and happy about what I'm doing with my life. And I absolutely want to pay that forward.
So-
Aaron Francis
Mm.
Stephen Margheim
I really do have an open door policy. Um, reach out, like if you have problems, if you have things you're interested in. Like, "I have this crazy idea. I wonder if we could use SQLite for this." Um, reach out. I'm happy to chat, hop on a, a Google Meet, hop on a Tuple, whatever. Um, I, I really sincerely mean that, and it's kind of a moral obligation for me.
I have to pay off all of these debts. Um, so you'd be doing me a favor as well.
Aaron Francis
Well, Steven, this has been a delight. You're fascinating. But beyond SQLite, you're a fascinating guy, and I really enjoyed finally getting to talk to you, uh, you know, in real life over the internet. Uh, but this, this has been, this has been delightful. So if y'all want to find more of Steven, uh, I highly encourage you follow him on Twitter and go and read all his, uh, past blog posts.
Um, even if you're not a Ruby person. I'm not a Ruby person, and I learned a ton about SQLite through just reading the stuff he's written. Um, so thank you for doing this. Thank you for what you're doing for your community, and if y'all want to, uh, if y'all wanna talk to him, please reach out. It sounds like, it sounds like he's got a lot of debts to pay off, so you'd be doing him a favor.
So please reach out to Steven. And Steven, thank you for, for doing this with me.
Stephen Margheim
Yes. Thank you.