Rails World 2026: Ractors, Rails, and Robots: Automating the Path to Ractor-safety (Andrew Novoselac) Video: https://www.youtube.com/watch?v=MVKUx-B_wHs Talk page: https://www.rubyevents.org/talks/ractors-rails-and-robots-automating-the-path-to-ractor-safety Unedited machine transcript, generated with Whisper. It contains recognition errors (names, technical terms). Check the video before quoting. ---- Hello, everyone. My name is Andrew. I'm here to talk about Ractors, Rails, and Robots, automating the path to Ractor safety. Yeah, this is a talk about making Rails Ractor safe. So a little bit about me. I'm a developer at Shopify on the Ruby and Rails infrastructure team. I live in Hamilton, Ontario. I've been building with Rails since 2018, and today I'm a first-time conference speaker. So, yes, please be kind. Please wish me luck. Okay, so as I said, this talk is about Ractor safety and Rails. A little bit of what we're going to talk about today. We'll start by talking about the dream of a Ractor-based web server where I'll try to motivate why we're interested in doing this work. Then we'll talk about why the work is actually quite challenging and what that looks like. mapping the problem with the help of an AI agent loop. And last, I'll talk about the work we've actually been doing to upstream this stuff to Rails, where we're at today, and what's coming up in the future. So let's get started. First, we're going to talk about the dream of a Raptor web server. To do that, we have to talk about what Raptors are. So, Raptors are concurrency primitives that enable parallel execution within a single process. What does that mean? We can contrast Raptors with the other concurrency primitives in Ruby, like processes and threads. So we have this handy chart. Let's start with processes. All Ruby programs run in a process, and processes are fully parallel. That means they can run Ruby code at the same time. Imagine you open two terminal tabs, you open two Rails consoles, and you type some commands. They can both run Ruby in parallel. However, they don't share memory. Processes are totally isolated. They load their own code, they have their own object space, et cetera. On the other hand, we have threads. threads running within a process, but they do not run in parallel. Only one thread has something called the global VM lock at a time, and only it can be running Ruby code. The rest of the threads are all blocked and waiting. However, threads do share memory, so they have access to the same objects. You only have to load code one time, and all the threads have access to it. That's where Ractors come in. Ractors actually have both of these good properties, so Ractors can run code in parallel. There can be many Ractors within one process, running Ruby code at the same time. No GVL, or actually they have their own GVL. And they do share memory. So each reactor can be talking to the same code, the same objects, et cetera. So to get an idea of what that looks like, suppose we have fast Fibonacci with threads. And fast Fibonacci does this calculation in 100 milliseconds. You can decide for yourself if that's really fast or not, but just for the sake of demonstration. Let's say we want to do this calculation three times, so we fan out to three threads. For the first 100 milliseconds, one will have the GVL. The others are blocked. Then thread two will have the GVL. The others are blocked. And finally, thread three will have the GVL. So though we did 100 milliseconds of calculation on three threads, the program still takes 300 milliseconds to run because only one thread can be executing Ruby code at a time. But if we introduce Ractors, everything changes. Each Ractor has its own GVL, so they can each be doing that calculation in parallel. done three times on three Raptors, executes in only 100 milliseconds. So a bit of the life story of Raptors. They've been an experimental feature since Ruby 3.0 in 2020. Thanks to some of my colleagues at Shopify and others in the community, there was massive improvements in Ruby 4.0, and there's even more improvements coming later this year in Ruby 4.1. And we're believing now that Raptors are ready to really be used in production. And so at Shopify, we're exploring many ways. potentially use Ractors in production. And one of those ways is we want to experiment with a Ractor-based web server. So what would that look like? A traditional multi-threaded web server has one process with many threads, each serving requests. And that means each thread is taking turns running Ruby code while the others are blocked. What that means is if we want to serve more concurrent requests, we need more threads. And thus, we introduce more latency in our system because we have more blocking as we introduce more threads. traditionally solve this problem at Shopify and in the community is using a forking web server. Oh, sorry. A forking web server boots one process and then forks many additional process. And so now instead of having one thread responding to a request, we have one process responding to a request. And they can all respond to requests in parallel. However, each process has its own memory. Now I put an asterisk because there's something called copy on write. At the OS level, the process may share memory, but the longer they run, that will diverge. will be invalidated. They are truly isolated. And what this means is if we want to have more concurrent requests, we need more processes. And that means we're going to use more memory in our system. And I want to highlight that. That's really the problem that we are interested in solving at Shopify. Today, if we want more concurrency, we use more memory. Now, I don't know if you follow the news, but memory is getting very expensive these days. probably you as well, to try to use your machines-- oh, sorry. OK, we can watch it again. Memory is getting really expensive today. So it's really important to us at Shopify, and probably to you as well, that we use the machines we pay the big bucks for as efficiently as possible. We want to serve as many requests as we can using as little memory as we can. And we think Raptors might be a way to help us do that. So a Raptor-based web server would be one process with many Raptors serving requests in parallel. would be sharing memory. So we have this theory that a Raptor-based web server could let us increase the concurrency of our system without increasing memory or latency. And that's the dream that we have. Now, to test that, we actually used Basecamp's open-source WriteBook app. Using some of the techniques I'm going to talk about later in the talk, we made a small part of this application, Raptor Safe, and we hooked it up to a Raptor-based web server to benchmark it against Puma. And the results were quite eye-popping. be looking at is the bottom line here. For a PNG cluster with nine processes versus Keno, which is the experimental web server we used with one process with nine Ractors, we saw about a seven times memory improvement. So that's quite enticing. How do we do on latency? Well, there is actually a 25% increase in latency when we're using Ractors. And why that is, I'll talk about later in the talk. Once we understand some of the techniques we use to make this work, why there's more latency using Raptors, and what we could do about that in the future. But for now, I'll leave it as a bit of an exercise to the viewer to think about why that might be. So this is pretty enticing. We can get all these memory savings just by using Raptors. Now, if you're a skeptical developer like me, you're probably thinking, sorry, before I get there, there's a blog post that we wrote. You can check it out. It has all the details about this experiment. So now let's get back to the skepticism that you're probably having. and you're probably thinking, what's the catch? How can this be true? What makes this hard? So let's talk about that. Why it's hard to make Rails Raptors safe. So I've kind of buried the lead here. I've been using this term, Raptors safe, but I haven't defined it yet. Let's talk about what it is. Now, while it's true that Raptors share memory, there's actually a bit of a stronger constraint than that. Any object that's shared between Raptors has to be deeply frozen. So what does that mean? If I'm a Ruby object, for me to be deeply frozen, not only do I have to be frozen, but all my instance variables, all of their instance variables, so on and so forth, all the way down the object tree, all need to be frozen. That's what Ractor safe means. So here's an example. Suppose we have my application with a config that's a hash. We open or create a new Ractor. We try to access that hash. We'll get this Ractor isolation error because the hash is mutable. So we can't access it off of the Ractor it was created on, what we call the main reactor. So to get around this, we have to freeze the hash. Now we can access it off of the main reactor. But things need to be deeply frozen. So if our config actually contains a mutable value, such as this array that contains bar, it's not enough just to freeze the hash. If we do that and try to access it off the main reactor, we'll actually get this error again, because the array within the hash is still mutable. So we have to freeze both the array within the hash, and the hash itself. And Ruby actually provides a nice utility to do that called Ractor Makes Shareable. If you want to learn more about the building blocks of using Ractors, how to make things shareable, et cetera, Aaron talked about it quite a bit in his keynote last year. So I highly recommend you watch that portion. I mean, watch all of the keynote if you haven't seen it, but watch that portion to learn more about all this shareability stuff. So what does this mean for Rails? to make Rails Ractor safe means we want to boot the application, and then we want to call Ractor make shareable on our Rails application. That's the thing actually we use to respond to requests. And then in a new Ractor, we want to call Rails application call and give it some request information. So that's the goal. How hard could it be? So instead of trying to tell you why it's hard, I thought, why don't we just walk through what happens if we actually do this? So I'm not going to do a live demo. I was advised strongly not to do it by many people, but let's just pretend I'm doing a live demo. So I've created my new Rails app. I'm like, ah, I'm going to open up the console and I'm going to call Raptor make shareable. What's going to happen? I'll get this error. Prox self is not shareable from Rails initializable RB line 44. Okay, let's check it out. So what is this? Well, Rails applications and Rails engines can define initializers. They all go in this collection. As you can see, we have some hashes there and they have prox that when we ask the hash for a key that it doesn't know about yet, it puts an empty key. Okay. So what do we do? The error is actually about the proc. So this proc, for it to be reactor safe, the thing it refers to itself, I guess you could say, also needs to be reactor safe. So that's the issue. But if we think about this, we can actually sidestep this. Once we boot the Rails app, we've loaded and run all the initializers. We're never gonna add new initializers during a request. At least I hope not. I mean, I've seen some pretty wild things in Rails applications, we're not creating new initializers during requests and so we don't actually have to worry too much about making this Ractor safe we'll just drop the default proc we'll freeze all this stuff and we'll call it a day okay first one we got passed let's call it again Ractor make shareable rails application now we get this error undefined method freeze for concurrent map so concurrent maps are not freezable and thus they're not Ractor safe and now if you're looking at this error message you're probably thinking where does this current current map come from what line of code is this error and one thing about ruby 4.0 is the error messages for some reactor stuff still leave a little bit to desire to be desired so one thing my team did is actually open some patches upstream to ruby to improve the situation so after applying those patches we'll now get an error message like this this actually names where the concurrent map lives and also it names what instance variable which object it lives in and then what instance variable what object that lives in all the way up the object hierarchy so we can kind of walk back and see where this current map is really coming from. And those patches are here if you would like to check them out. We're hoping they'll be released as part of Ruby 4.1. So if you look back at the last slide, sorry, let me go back. This is actually coming from Solid Cache. Now this is tricky because Solid Cache isn't part of Rails. Solid Cache is like its own gem. Depends on Active Record probably. So how do we make it reactor safe without Rails being reactor safe? It's not exactly clear. So for now, let's just skip it. We'll just bypass that one. Yeah. We'll call RactorMakeshareable again. Okay, we get another error. This time it's from Action Table. So I won't belabor this, but it's kind of clear this is going to take a while. Now, why is this hard? So for one thing, this is not parallelizable. You only get one error at a time, and you have to solve that error or just kind of hack around it to even see what you're going to get next. Because of that, you can't see the finish line. I can't tell you if there's going to be 10 of these errors, 100 of these errors, 1,000 of these errors. I just don't know. And additionally, you have to do constant context switching. So you saw we started in kind of like rail ties, rail boot process stuff, then we're looking at solid cache, and then we would have to look at action cable. So for one developer who can't parallelize the work, it is going to require you to basically look at the entire Rails framework in some sort of random order. And not to mention, we didn't get to any actual hard stuff yet. So once we freeze the application, we're going to actually respond to a request, and then the interesting engineering problems are going to happen. So how do we do database access? How do we render views? What about logging? What about caches? So we actually did this for a while at Shopify manually, or I know DHH and his talk was talking about what do we call the old era of programming. I've been calling it classic programming. So we did some classic programming manually trying to figure this out. And it was like very slow going. And eventually we started thinking to ourselves, there's gotta be a better way. And this is when AI agents were starting to get really good. we can use an AI agent to help us with this problem. Now, I like to think the benefits of AI and AI agents are they only care about money and they never get tired. If I just keep feeding this thing tokens, maybe unlike me, it's never going to have to take a lunch break. It's never going to get bored or want to do something else. It'll just keep working on the problem and I can let it turn and turn forever. So that was kind of the idea. So how do we actually set that up? We call this the harness in the repo. One repo that contains everything the agent will need to solve this reactor safety problem. Contains vendor dependencies of Ruby, Rack, Rails, a few other things that are needed. And then it has a dummy app that's wired up to these vendor dependencies. Has some agent skills. So it knows how to, for example, sorry, my clicker is unresponsive. It knows how to run tests. It knows how to rebuild Ruby. If it's made changes to Ruby, it can build a new dev version for itself, et cetera. And then most importantly, It has the plan. So what is the plan? We told the agent, we want you to iterate until you can make a Rails application shareable. Then we want you to keep iterating until you can serve a 200 okay. And then the most interesting part, start adding new functionality to your app that doesn't work with Ractors, and then find a way to make it work. And just keep doing this forever until we tell you to stop. So that's what we left the agent with, and a bunch of details about how to work with Ractors. of course. So what did it do? It created this nice report for us, actually. Sorry, it's quite bright. And the report says, my app now has 169 routes. They all work within Raptors. And that covers about 67% of the Rails code base. And as you can see, it's broken down by area of the code base. So yeah, this is like really fantastic results. It did very well. Some stats for nerds. This was about $2,000 worth of Opus 4.7 credits, which was the best model the time uh it took about one week of human time running this loop and we performed some additional loops to try to iterate on some of the solution and also as like a hallucination uh guard to make sure like what it did once it could actually do again um yeah so there you go uh and there you have it folks uh that's it for me we got raptor safe rails before gt6 logo that we call the unofficial logo of Ractors at Shopify. So not so fast. Now, I know this is rapidly shifting. This work happened three or four months ago. But you may be part of the I Still Read Code Club. And you might be wondering, did you actually read any of that agent-generated code? Yes, I did read all of it. So it was one week to generate and two weeks to review. The final prototype that I reviewed had about 200 files change in the Rails framework, mostly in kind of the guts of the framework, action view, active record, action pack. I tried to perform like a vibes-based categorization of how difficult the change looked to me and try to see some common problems and patterns that the agent came up with. And the results of that were this. About 50% of the changes the agent made are trivial, looked great, were pretty much ready to upstream right away. direction, but would just need some human attention, either prompting an AI or just cleaning up the code, and then we could hope to upstream them. And 10% needed a totally focused design effort with humans and agents to figure out what we would do in Rails. So I think that's pretty amazing. It did also make me think a little bit about what the drawbacks of AI agents were, which are they only care about money and they never get tired. So the agent really had no qualms about making 5,000 line methods, or doing the same pattern all over the Rails code base instead of like trying to find a nice API for something that we care about a lot in Rails. And it also was willing to do rescue raptor isolation error in some places just to get around problems. So not perfect, but we weren't looking for perfect code. What we care about is that the agent gave us a map. This was formerly like a very intractable problem that we thought would take us, you know, potentially a year to just get our heads around everything we would need to do to make Rails Raptor safe. And then in a week or two, we have this massive prototype with so much of the ground kind of mapped out for us. And that's really, really amazing. Okay, so let's talk about what we actually did to get this stuff upstreamed. Now, we have this massive prototype. The app does tons of stuff. I didn't actually read every line of code of the app, so I don't even know everything that it does. We started thinking, now it seems like this problem is really tractable, but what are we actually going to start with in Rails? What are we going to upstream? We landed on the first pass of what we call experimental Raptor support. And it's just this. You can do Rails new, make a new app. You can generate some scaffolded routes. You can make the application shareable. And then for each route, those routes will work as a Raptor. That's the scope that we wanted to start with. This is like a very nice and attainable and easy to describe scope, but it actually covers some really important stuff. So first of all, it covers making the application shareable. It covers database access, logging, view rendering, sort of the guts of any web application. Well, at the same time, it lets us skip some stuff that's going to be complex and that is better served for us to do later once we've got our feet under us with some of these reactor safety patterns. So for now, we didn't touch action cable, jobs, dependencies like solid cache. We also didn't touch development mode, mainly because of lazy loading. So now we're ready to start actually shipping some code, but we are faced with a problem. our smaller scope and know what we actually need to ship upstream and so i thought about this a lot we had sort of an inclination to say just one more agent loop bro let's run the ai again on the new scope and it can cross-reference that with the old prototype and tell us which changes are related to what but i started to feel that we're just drowning in ai code and every new loop we ran was like more cognitive burden actually to read the code and understand uh what it was related to so we wanted something a little bit more static we could run and get some deterministic results from. And that's when we came up with the idea of Ractor check isolation. So this is a new method that we created. It's a special Ractor. So it doesn't actually have the parallelism properties of real Ractors because instead of raising errors, it emits warnings. This is really handy. It allows us to collect all the errors that would happen during a request. Here's what it looks like. So if I have the foo constant and I try to shovel it in my Ractor, I'll immediately get an isolation error. and my program will end, just like we saw when we were kind of going through making Rails Reactor safe. But with check isolation, now I actually get three warnings, one for each time I access foo, and then my program would actually do what it's supposed to do. It prints, hello world, perfect. So this is really, really useful. And this is actually open upstream now in the Ruby bug and feature tracker. There's some debate happening about the best implementation for this, but we're hoping that this will be a part of Ruby 4.1. So what we did is wired it up to a nightly build. This build pulls the latest commit of Rails, generates an app, generates the routes, and then runs all the routes through a scaffold. We started with about 200 unique routes. This is a screenshot from a week ago as we're getting ready to close up this project and knocking off the last few errors. Here's what it looks like. The errors are itemized by where in the codebase they happen, the backtrace, et cetera. So this was really, really handy. start working on this project. We have the AI written prototype that shows us some of the patterns that are needed to solve various things. We're tracking these errors live every day. So as code is getting shipped to Rails, we're seeing what the latest errors we would see are for this scope that we want to hit. We have everything we need. And then PRs really started flowing. So over the summer months, my team shipped over 100 PRs to Rails working on Raptor safety. So earlier I said there was some different levels of goodness, I guess you could say, from the trivial to the less trivial to the really hard. So let's look at an example of each of those. Example one, freezing constants. So Rails has many, many constants containing mutable arrays or hashes. They really have no reason to be mutable. They're just set when the code is loaded and never changed again. So if you access them on another Ractor, you will get an error. The easiest thing to do is just freeze them. We did this all over the Rails code base. There's actually a RuboCop that does this for you. If you're working on making anything Ractor safe, I highly recommend you start just by enabling this cop. I think that cut down about 30% of the errors right there. Okay, something a little bit trickier. Rails has a lot of global application states that live in class attributes. Those contain mutable values, and you access them off of the main Ractor, you will get an error. Here we have active Rector models have a read-only attributes array. Now, here's where we diverged a little bit from what the agent came up with first. 5,000 line method in Rails that found every place in the framework that does this and then froze them all just before freezing the application. We didn't like that for a few reasons. For one thing, it's a lot of bookkeeping, but also it creates this state where the application may or may not be reactor safe and we have to remember to call this method. We want to try to avoid that as much as possible. So we landed on what we call the read, copy, update pattern. I mean, we didn't make that up, but we call it this pattern. When we add a new element to the array, we actually duplicate it, we add the new element, and then we freeze it again and reassign it. This is really nice because now all these places in the framework are always rocked or safe at any point in the program's lifecycle. If you want to learn more about that pattern and quite a few others, my colleague Hartley published a blog post last week where he goes through quite a few of the different patterns we used over the framework. Okay, last, let's do a tricky one. Database access. This one is tricky. So IOs are inherently Ractor unsafe. They cannot pass a Ractor boundary. And additionally, connection pools are kind of like an inherently mutable thing. They track the state of a bunch of different connections and need to know who they're leased out to. So it's not immediately clear how we'd make something like that Ractor safe. What we landed on is using this gem called Ractor Dispatch. Sorry, I forgot the QR code, but I can give the link later. We use this gem called Ractor Dispatch, which lets us password to another Ractor. For example, we create a new Ractor. We do some CPU-intensive stuff. Then we need to do something that's Ractor unsafe. So we actually call back to the main Ractor to execute our Ractor unsafe code. We get our results, and then we do more CPU-intensive stuff with the result. We can split this code up by which Ractor it runs on, and it would look like this. So we create the Ractor on the main Ractor. The new Ractor, we do the CPU-intensive stuff. We're back on the main Ractor, et cetera. So this is the pattern we tried to use for database access. everything we can on the request serving reactor, but when we need to actually talk to the database and do that IO and get the connection, etc., we're going to go back to the main reactor. Now let's take a look at what it looks like. This is a very simplified walkthrough, but imagine we have git post one. So on the request reactor, we, sorry, ah, it's behind my back, it doesn't work. We execute middlewares, we call the post controller show, we build SQL, and now we're actually to execute our SQL. So we call back to the main Ractor. We execute our SQL there. Now we get our result back on the request Ractor. We hydrate our post model, and then we render some HTML. Perfect. Now the astute viewer will notice we've just added some blocking to our web application. Where would that be happening? Well, let's imagine there's two requests happening, both for Git Post 1. They both execute their middlewares. Oh, a lot of clicking, sorry. where they want to execute SQL. And now we're back on the main reactor. But the main reactor can only do one thing at a time. So first it executes the SQL for request one. And then it executes the SQL for request two while we get the result back on the request reactor, request reactor one, that is. And then we start hydrating the post model. Just now we're getting the result on request reactor two. And then we render a show HTML. But what you'll notice is that request reactor two actually had some latency compared to request reactor one. And so now we can answer the question, why introducing Ractors to write book increased latency? One of the reasons is that talking to the database and sending that back to the main Ractor actually introduced some blocking and thus some latency. So Ractor Dispatch is a really helpful tool, but our goal long-term is to have 100% of code being executed on the request Ractor. But Ractor Dispatch gives us an estate patch to use until that's possible. I think long-term for database access, we're hoping that connections will live on their own Ractor, and the request Ractor can check out that connection and kind of own it. But that's just a bigger factoring in Rails. But hopefully we'll get there one day. Other examples are lazy state. So Active Record has some lazy state on models that's only hydrated the first time we talk to the database. And that needs to be set on the main Ractor. We can't set that on a request Ractor. And there's also still lots of Ractor unsafe gems. Notably, Nopeagiri is one example. So we have to go back to the main Ractor to use Nopeagiri for now. Additionally, communicating between Ractors is not free. So we also want to be kind of judicious when we choose to use communication between Raptors during a request. This is a really interesting PR. I've really simplified it, but I highly recommend if you want to know more, read through the code. It's really, really fascinating engineering work. So there you go. Okay, so how do you try it out at home? We introduced this new method called Rails Application Raptorize. There is some ceremony that you have to do before you can freeze the application, unfortunately. in this method. So if you call it, you'll get this warning. Ractor support in Rails is experimental and subject to change. Even I would say the method name is subject to change. So don't get used to it. But after that, your application will be shareable. There's a couple of Ractor-based web servers you could check out. Cougar is very lightweight, very simple. You can probably read all the code very quickly. Keno is a little more feature-rich, but it's quite interesting. So check both those out. And then we actually created a template you create a Rails application exactly with the right setup for using Ractors, like skipping solid gems, enabling a few other things that you need to make Ractor support work. You can run this script, follow the QR code to get it. It has the proper link to the template, which is quite long. But when you do this, it'll boot the server at the end. And if you hit that server with some real requests, they will be being served to you off the main Ractor, which is very cool. And please read the template. before you run this on your machine. It was written lovingly by my colleagues and I, but it's arbitrary being run on your machine. So please do your due diligence and read it before you just execute something you saw on screen at a conference. That's it. What's next? We want to continue expanding Ractor support in Rails. We want to make the ecosystem more Ractor ready, like Gems. And we want to work on making real applications Ractor safe. We'll be working on all these things at Shopify over the next year, But the exciting thing is you and the community can be doing all these things as well. So create a new app using our template, add some new functionality to it. Chances are it will raise a Ractor isolation error somewhere, and you have your next contribution to Rails lined up for you. If you contribute to or own any gems, work on making them Ractor ready. And if you have a side project or a small app, explore making Ractor safe. Agents are really good at finding and fixing these things, and you can probably make progress really quickly. I really would like next year myself or one of my teammates or one of you on stage giving a talk about how they're using our actors in production. That would be really, really cool. And that's all I have today. I want to thank all my collaborators who both wrote code and had lots of interesting discussions and feedback that made this work possible. And thank you to all of you for listening.