Rails World 2026: Distributing Rails applications with ONCE (Kevin McConnell) Video: https://www.youtube.com/watch?v=eE4aVg15tbs Talk page: https://www.rubyevents.org/talks/distributing-rails-applications-with-once Unedited machine transcript, generated with Whisper. It contains recognition errors (names, technical terms). Check the video before quoting. ---- Hi there. So I hope everyone's having a nice conference so far. This talk is "Distributing Rails Applications with ONCE." And so it's really a talk about three things. It's about distributing applications, or specifically distributing applications to be run in a self-hosting style, as opposed to running them as SaaS. It's also a talk about Rails, because we'll look at some of the specific features in Rails and see how they fit into this self-hosting style. And it's a talk about ONCE. ONCE is a tool that we've built to try and make this whole process as easy as possible. The context for this talk really came from time that we spent at Thirstman Signals building and shipping a series of apps that we designed to be self-hosted in this way. We learned quite a lot from going through this process, both from building and shipping them and talking to customers who were using them. And that's what went on to inform the tooling that we built and the context to them that I'll be talking through in this talk. So to start with, I just wanted to look at a couple of different distribution models so we can kind of compare the details of them. And starting with SaaS, because I think that's the thing that most of us are kind of most familiar with. The way I think about SaaS is it really needs two things in order to work. You need some kind of infrastructure, which could be a rack of servers, a bunch of cloud resources or something like that. And then you need a bunch of customers. And then you need a bunch of customers. And so SaaS works by putting your software on that infrastructure and letting your customers access it. The self-hosting approach kind of spins everything around a bit. And so the way I think of this is that rather than starting with infrastructure, you start with a customer. You can give that customer a copy of your software, which they run on their own hardware. And then they can use that along with their kind of group of people, whoever they are. So that might be their company, their conference, their family, or whatever it is they need. When the next customer comes along. When the next customer comes along, they do the same thing. And now you have two installations out in the wild. More customers come along and you get more installations. And eventually you end up with a picture that looks a bit more like this, where you have tons and tons of these tiny little individual installations all over the place, each one running on a different bit of hardware and each one serving a different set of customers. When you put these models side by side and look at some of the characteristics of them, I think you have to find that something that's a strength for one to be a weakness for the other or vice versa. There's not necessarily a better way to do this. There's not necessarily a better way to do this. There's not necessarily a better way to do this. There's not necessarily a better way to do this. It's just two different models that serve different purposes. And the most obvious place to start, I think, is the question of who's running the infrastructure. So in SaaS, you run the infrastructure yourself that can have some advantages, like you have a lot of control over it. So if you do something like find a bug in your app and ship a fix for it, you know that all of your customers are getting that bug fixed right away, which can be nice. The less good part about running your infrastructure is the fact that you're the one who has to pay for it. So now you have to pay for it. So now you have to pay for it. So now you have to pay for it. So now you have to pay for it. So now you have to pay for it. So now you have to buy all the machines or rent the cloud resources. You have to pay the bandwidth bills. You probably have to devote somebody's time, your own time or someone else to look after this stuff. And that can get expensive, right? So for a big SaaS application, the running costs can be fairly considerable. Whereas in the self-hosted model, every customer brings their own hardware. You don't have to set that up. You don't have to pay for it or look after it. So there's a lot less that you have to do and kind of a lot less you have to pay for. But there's also a big difference. There's also a big difference in kind of the split of responsibility for data between these two models. So in the SaaS setup, all the data that gets gathered in your application when people are using it is going to land on your servers. And now you're responsible for kind of being like taking care of that data properly. So making sure that it's kept securely. It's not going to be hacked and stolen. Making sure you have backups so that if something goes wrong, you can recover people's data. But in the self-hosted style, that's another thing that the customer does the customer does the customer does for themselves. So now you don't have the responsibility that goes with that, which can be quite freeing. It can also be advantageous for the customer because they can, like from a privacy point of view, they can keep their data somewhere that no one else can get to. They can also keep their data physically near themselves so they'll have faster access to it. One thing that SaaS does really well, though, I think, is you get really good visibility into how your application's working. Since all the requests into your app are coming into your infrastructure, you can see a lot of them. You can see your app logs. You can see exception reports and so on. And so you can see in great detail what's working well in your app and what hasn't worked well in your app, which can be really valuable and it can help you kind of decide where you should be spending your time if you want to improve your product. On the self-hosting side, you don't really have that at all. People are going to be installing your software machines that not only do you have access to them, you won't even know where they are. And so the only way you could know how your software works there would be and you have some kind of telemetry. That's probably something you're not going to be that comfortable doing. It's probably also something that your customers would not be very comfortable with you doing. And so I find it's kind of better just to sort of stay away from that and just accept the fact that you don't have the visibility that you would have with SaaS. Scaling is something that can be quite complicated at times with SaaS, as I'm sure a lot of you have run into in your jobs. As your application kind of gets bigger and more popular, you may run into situations where you have to engineer your way around some kind of scaling problem. So you might have to do things like split things up into different services or shard big data sets into smaller portions that are more manageable, rethink some algorithms you're using or something like that. And as a programmer, that kind of stuff can be fun. But I think from a business point of view and a product point of view, it's not really ideal that you have to do that. One of the downsides is that it can end up with a lot of complexity leaking into your application. And it's complexity that only exists to get around these scaling problems problems. Something that's nice about the self-hosting environment, on the other hand, on the other hand, on the other hand, on the other hand, on the other hand, is that scaling problems kind of just go away because your application only needs to be able to work well enough to serve the biggest individual customer. And for sure, you'll have some customers that are bigger than others, but it's still likely that even your largest customer is still significantly smaller or orders of magnitude smaller than that sort of total kind of aggregate workload that you would have had if you were running it in SaaS. And we definitely saw this with Campfire. We definitely saw this with Campfire, for example. When we were working on Campfire, early on, we did some testing measurements to see what would it take to make sure it performed well in kind of the biggest installation that we thought someone would reasonably want to use that product for. And what we found was pretty much vanilla Rails application running SQLite on a modest single server can serve a surprising amount of traffic and was easily enough to serve that kind of what we imagined to be a very big installation. And so that's a lot of people that we have gone on before doing SaaS. Another thing that we have gone on before doing SaaS. Another thing that I think SaaS does much better or is much easier in the SaaS world is that it gives you much more control over who's accessing your software. So if you want to do something like charge a subscription or require some kind of conditions about sign up before people can use it, it's easy to do because, again, all those requests are coming into your infrastructure and you can gate that traffic on anything you want to gate that traffic on. In the self-hosted model, you have to get that traffic on anything you have to do so you have to do so you have to do so you have to give up quite a lot of control there. So you have to give up quite a lot of control there. So you can still charge money for software. You can still wire things up to work in a subscription model and so if you want to, but really once you've given someone like a working copy of your software, the ship has sailed a little bit in terms of how much you could then restrict what they would go on to do with it. You can try and fight it a bit, but I think really you have to just accept that there's less control in that model. And then the last point I wanted to mention here, but I think this for the purposes of this talk, it's the more interesting one is just the difference in customer effort that there is between these two models. So in SaaS, this is almost super easy for customers. They don't have to think about software and hardware. They just go to your product and sign up and start using it. This is pretty much what SaaS was invented for and it works really well. On the self-hosted side, it's kind of a bigger ask now because now you can give software to someone, but you're also telling them that they have to install it. They have to look after it and make sure it keeps working. It can get daunting for people who are not used to it. But I will say that what we found in our experience is that this is really more of a problem of perception than a practical problem. You can make it easy for them to do this. And as long as you sort of communicate to people what the steps are going to be involved, then most people won't run into problems. And so again, using Campfire as an example for this, we sold thousands of copies of Campfire to lots of different people and we offered support with that. So if anyone had trouble getting it running, they could contact us. And almost nobody did. The support requests that we saw come in were much more often just about product questions or maybe people who wanted to run it in an environment that it wasn't really designed to run or something like that and they wanted to talk through that. But almost nobody contacted us to just say that we're having a hard time running it, which I think is pretty encouraging. And that's not to say that I think it means that running software yourself is inherently easy. I think it shows that you can make it easy if you build the right tooling. And so that is a handy segue to the second part of this talk, which is to take a look at once. So once is the tooling that we built to try and simplify this process. We started looking into this way back when we were working on that first set of applications that I mentioned, because it seemed to us like we knew we were going to need to make this a really easy thing for customers to do. Otherwise, the whole premise of making self-hosting software just wasn't going to be viable. If it was daunting for people, we were going to need to do it. And the metaphor and model that we kept coming back to whenever we thought about software installation was on video game consoles, particularly the kinds where if you bought a new game that came on a physical cartridge and all you had to do to use that game was just stick that cartridge in the console and it would work. Super simple. And so we thought the closer we could get to that kind of feeling or like the spirit of that, the better. Of course, running web applications is probably never going to be quite as simple as that just because we have concerns to deal with based game didn't have. So things like network connectivity or managing the data that accrues over the time you're using it or something like that. But still, it was like a goal to aim for to see how close to that we could get. And I think the main thing we took from it was what you really want is to give someone a way to install your software that's going to one shot it. You want just like a single command that can run that command and they're done and everything's working. You don't want to give people like a series of steps to follow or things that they have to learn. We could almost get to that. But the one part that came up when we looked at it was when you're installing web applications, it turns out you pretty much always want to know what host name or domain name someone's trying to run that software on. Because usually they're going to run these applications so they're accessible over the internet. So we'll need to know things like what name to put on their TLS certificate. If someone's running multiple applications on the same server, then requests are going to come in for all those applications. We need to know how to route them. So again, based on their names. So we started two choices. So we started two choices. You can either have the thing that nearly one shots the installation, but then leave something that's not properly configured because we don't know the name. Or we can have that process just ask them the question at the start. And we found that the second one is much easier for people because then you get prompted for the one bit of information that needs and everything else just gets taken care of automatically. So the format we landed on for this Comangica one shot is just the good old something into a shell construction. I think these are a nice way to install software on servers, particularly because it asks so little of the server you're installing onto. You just need to have some kind of terminal access. You can do it over in SSH. You can use the web console in DigitalOcean or whatever. But if you can get to a terminal, you can paste with these in, and you can kind of start there. And so this is the format we started with. This version will be enough to install the ONCE tool. So if you paste this into a terminal now, it'll install the ONCE tool. It'll sort out any dependencies it needs to and run the tool so that you could then go on to install some apps. But that in itself isn't really this whole one-shotting the process we wanted to get to because it doesn't leave you yet with the app running. And to get around that, we have this form of the install where you can specify the app that you want. So the ONCE tool knows a few apps by their kind of short name or alias. It knows what Campfire is. It knows what Fizzy is and so on. But it's not just for those apps. So for other applications. So for other applications. So for other applications, you can just put in the path to a Docker image, and it will install the application that lives in that Docker image. And this is the thing that, as I say, other than asking the question about the host name, it can kind of one-shot everything from there. I'll make it a bit clearer if I could show you an example. So here's a video of me doing a complete installation of Fizzy from start to finish just by pasting in the Fizzy version of this link. So in a moment, I'll paste in the link that's going to go and when I run that. When I run that, it's going to go and fetch the ONCE tool and boot that up. Here's where I get to enter the host name. I'm just going to use a local host domain in this example because I was just trying to run it locally on this machine. But then when you hit install, it will do all the remaining steps. It will go fetch the container, be the container, configure it, and you're left with something that's working. And that's the whole install complete. So at this point, Fizzy's running on this machine. You can open up in the browser. If I was doing this for real, I could go into that sign up section and create my first Fizzy account and so on. So that's what the process looks like. So that's what the process looks like to use it. When the installation is finished, you end up back on this screen. So this is like ONCE's main view whenever you have any apps installed. And this has two jobs to do, really. One is that it acts as a dashboard for what's going on on the machine. So you can see all the apps you have installed. Here we just have one. You can see some stats about how they're behaving. So like how much traffic they're serving, how many visitors you've had in the last 24 hours and seven days. A little bit of a little bit of a little bit of a little bit of a little bit of a little bit of a little bit of a little bit of a little bit of what their resource usage looks like, so much memory they're using. So that sort of dashboard overview is one of the purposes of this. Its other purpose is it's kind of the jumping off point to do anything you need to do to manage these applications going forward. So there's a bunch of different menus and screens that you can get to either with the mouse or keyboard shortcuts. And it's designed to be kind of something you can explore. And the hope here is it makes it a lot less daunting for people who are new to running their own apps because instead of having that question of where do I start, where do I start, just open this and you can probably find the thing that you're looking for and do it from inside this. Under the hood, all that's really happening with this is that we're running the apps as Docker containers. So once we'll make a Docker network for each app that you install, it'll boot a container for the app. It'll attach a persistent volume so the application can keep its data in that volume and we can keep it safe between the source and the source of the source and the source. If one's takes a backup of an application, it's backing up the contents of that volume. We run the apps behind a little web proxy so that way it can handle the TLS termination and it can also route requests to the correct app if you have multiple apps running on the same machine. So knowing how these applications run and what environment they're in, that also helps us start to think about what is required of your application to work in this environment and what you'd have to do if you're building a Rails app that needs to fit into it. So that's our second handy segue that takes us into the third part of the third part of the talk. So that's our second handy segue that takes us into the third part of the talk. So if you want to build an application in Rails that you can distribute this way and install with once, there's really four requirements that once makes on the application. So it needs to be packaged as a Docker image because that's what we're using to launch the app. It needs to serve web traffic on the default port 80 because that's where the proxy was going to sell that traffic to. So it needs to keep all of its data inside a particular path. It needs to keep all of its data inside a particular path because that's the path that we'll have mounted that persistent volume onto. So anything that your application wants to keep needs to go in there. And it needs to have a health check endpoint so that the tooling knows when your application is kind of ready and healthy and can start sending traffic to it. If you're starting a new Rails app that you might want to distribute this way, then the sort of short, pithy answer for what you need to do to make it adhere to these principles is to run Rails new these are all just defaults that Rails already does out of the box anyway. So a new Rails app, it already comes kind of packaged for Docker because there'll be a Docker file generated when you create the application. The configuration of that Docker file will serve web traffic over port 80 because it'll have thruster installed, which will handle the traffic of that port. That also means you get some of the thruster nice things like faster asset serving and response caching and so on. By default, a Rails app will store its data in the right place, but this works due to like a bit of trickery that it once is doing. So we didn't want the once tool to be too real specific, which is why it uses this path of slash storage. But Rails already has a convention that locally stored data like SQLite database and so on will be under this path slash Rails slash storage. And we didn't want to fight that convention either. So once gets around this by just mounting the same volume in both places. So it doesn't matter which of these paths you use, you're still right into the same place and either one will work. So it doesn't matter which of these paths you use. So it doesn't matter which of these paths you use, but it doesn't matter. So one of those applications uses the entry point to run DB prepare whenever you boot the application container. So that has the effect that if there's any pending migrations that need to be applied in that environment, then when you try to boot the app, they'll get applied before the app starts. This works really well in that self-hosted environment because if you imagine you've shipped version one of your app, you might have thousands of people who have it installed and have existing data and their databases are in a particular state. If you then add some more features that require some migrations to run and ship the newer version of your Docker image. So it's in an app container. So it's in an app container. It can store it in SQLite. It can store it in SQLite. It can store it in SQLite. The default settings that you get there just kind of fit. Same with solid cache. Same with solid cache. And same with active storage. Like all of these can just store the data they need to locally on desk, which would be in that volume. And so they tend to work quite well. So what all that means is that if you wanted to build a new app and distribute it this way, then the steps you have to follow to go from your new app to someone being able to very easily one-shot that install that we showed earlier, and it's really just a few steps. It's really just a few steps. It's really just a few steps. It's really just a few steps. You have to make a few steps. You have to make a few steps. You have to make a few steps. You have to make a few steps. You have to make a few steps. You have to make your new app, build your features, and then just build and push the Docker image for your app to somewhere that's going to be accessible to people, and then make one of these install commands of that format that references your Docker image in it. You can just then share that command with anybody, and if they paste it into the server, they'll have your app running in kind of no time at all in that sort of semi-managed environment that we looked at. So that really covers most of these requirements from your app. So that really covers most of the requirements from your app. I just also wanted to mention a few things to start working in the other direction, which is things that one does that your app might be able to take advantage of just to kind of smooth out some parts of the process of working in this way. And there's really two categories of these. There's some environment variables that one sets up for you, and there are also hooks that you can use to make the backup process work better. So to start with, I'm going to go through a few environment variables. And the first thing is just to know that once. And the first thing is just to know that once is going to take care of the question of assigning a secret key base for you. Obviously, in every installation you do of our Rails app, you need to have a unique secret key base so that secrets that are generated by that application are unique to that environment and can't be reused. So people can't do something like take a signed cookie from one installation of the app and use it somewhere else. So in this hosted environment where you have potentially thousands of these installations, they all have to have unique secrets. So if you want to do this for you, it'll just make a random secret key base for each installation that someone does and we'll pass it through the container, this environment variable. Rails already looks here for that. So you don't actually have to do anything about this. I mentioned it just so that you know that it's sort of, it's been handled. It's not something you have to think about in your app itself. Similarly, once we'll create a key pair for use with web push. So if your application needs to send push notifications, you can do that using the web push standard, but you'll need some unique credentials. Again, unique for each installation. And so in case that's something you're going to do, one sort of proactively makes a key pair for you and passes it through in the environment. If you don't use web push, you can just ignore them. But if you do use web push, then it's already sort of there and configured and it makes it easier to do. If your app has to send email, this one can be a bit fiddly set up. So you're not going to be able to have your app just send email from whatever random server someone's going to install it on, or at least you could. But chances are all that email just get flagged as spam and not actually get delivered. What's likely to work better is to refer people to use some kind of third party service to send email through. So like a mail gun, send grid, postmark, one of those kind of folks. And all of those will give people some credentials that they can then use to configure their app to send through. So to make that process a little bit easier to set up, once has a setting screen called email settings, where someone can just open that up and fill in the form with the details that they got back to their email. If they got back to their email, they got back to their email. And if they got back to their email provider. And if they do that, those will also get passed into your app as a set of environment variables like this. We have Fizzy set up in this way. So if you have an app that needs to send email, you can go and look at the Fizzy repo. And in our production RB, you'll see the block where we deal with these environment variables. You can just kind of copy that bit into your app and make that work. And then the last environment variable way to mention is this numCPUs. So once also has a screen where if someone wants to, they can restrict the amount of resources that the amount of resources that each app gets to use on a server. So you can limit how much memory it can access and you can limit the total number of CPU cores that it was allowed to use. If someone does that, then once enforces it through Docker, but it will also pass through that numCPUs as the number of cores that your app is supposed to be using into your app so that if you do something like spawn background worker processes or something like that, like job workers, you might want to choose the number that you spawn there according to you spawn there according to how many cores you have access to. So that you're not trying to run far too many processes for the environment. So we just pass that number through the environment variable so it's easier for you to set that up. And then the other category of things that I want to talk about was just these hook scripts. So I think I mentioned earlier that once has the ability to automatically do backups for you because that's something that most people are going to want to set up. It'll take a backup once per day, build an archive that has all the application and it's data, along with it. And it keeps a month's data along with it. And it keeps a month's worth of those backup archives for you. Mostly we can do this automatically, but there's one wrinkle to it, which is that it's not necessarily safe to just copy data that's being written to you and once doesn't really know what's going on in your application. An example of where this could come up is if you're using SQLite, like a SQLite database is a file, but if you're actively writing to that file, you can't just sort of naively copy that file and expect to get to get a valid copy because depending on where this page changes landed. As the copy was going on, you might end up with a copy that's corrupt. So once there's two strategies for dealing with this, the first and the simplest is just that we pause the container briefly while we do that copying step. So that way, no writes are landing in the file system. We know that what we get is an atomic copy and it's always something that we're going to be able to safely restore from later. And often that's fine. One of the, or one of the, or one of the, or one of the nice things about these self-hosted environments is that they tend to be small. Like the amount of data there's usually smaller. The number of people accessing it is fewer. And so if you have a brief pause that happens once a day, there's a good chance this will just kind of fly under the radar and no one will notice it. So in many cases you can get away with it, but it obviously won't fit every situation and it doesn't scale well if you have the kind of app that generates a lot of data where that copy step might actually take a while. So to improve that, it also supports these application hooks. These application hooks that you can use these application hooks that you can use to sort of work alongside the backup process and make it run a lot more smoothly. The way this works is that when once it's about to take a backup, it will first try and, right before it does the copy, it'll try and invoke a pre-backup script in your container if there is one. If it finds there's one there and it executes and it returns success, then once assumes that your application is taking care of whatever details need to be taken care of such that you have a live copy in the file system is fine. And so in that situation, it doesn't do the pause, it just continues to let your app run while it copies all the data into its archives. So the way we implement it in our apps, and you can look at something like Campfire for an example of this, is that our pre-backup is just a little Ruby script that connects to the SQLite database and uses an API SQLite has called the online backup API that lets you take a transactionally safe copy of the database into a separate file. So you have to do that and you have to do that while the database is changing. So our pre-backups, our pre-backup just does that and makes that safe copy onto the file system so that that exists in the backup. If you layer restore a backup, once we'll sort of unpack the data into a new volume, it'll get everything set up ready to run your app. Then right before it boots your app, it'll try and call this post-restore. So in our example, since we made that safe copy of the database, our post-restore script just moves that file back into place where the application database would normally be. So it's running the safe copy. So that's pretty much everything I wanted to cover. So that's pretty much everything I wanted to cover. I just wanted to quickly recap to say that maybe self-hosting is something you'd be interested in trying. Maybe you have an idea for an app that you think would work good in this way. If so, please know that we can make the whole process pretty easy. Ones can deploy most Rails app just as they are, so it's a very simple thing to try. If you're interested, then I'd love for you to give it a shot and let us know how you get on. Thanks. Thank you.