Hello, and welcome to another episode of The Bike Chat, a weekly podcast from your friends at ThoughtBot about developing great software. I'm Stephanie Min, and today I'm joined by a very special guest, Trevor Turk. Hello. Thanks for having me. Yeah, happy to have you. Trevor, would you mind giving a little bit of an intro to our listeners? Yeah, so I've been a long time, mostly Ruby on Rails consultant. I think I got started in 2008 doing freelancing stuff. Mostly I've been a freelancer, except I had about a four-year stay at 3ZenSignals, aka Basecamp, at the time I was there. And lately I've been working on...
a side project that's become more of a side business, which is a weather app for iPhone Android called Hello Weather. And we also have a spinoff product, which is basically a weather data API proxy that we call the weather machine that powers all of our apps. I love the name weather machine. I think it's very smart. Yes. We try to have some personality in our marketing stuff. I do all this work with my main partner is named Jonas Downey. He is a designer that I met at Basecamp. We work together a lot. It was his idea to make a weather app. The joke is how hard can it be? And it turns out five, six years later, we're still working on it.
It's been a journey. Yeah, I bet. So I think one of the things I was really kind of curious about or impressed by when I was poking around Hello Weather and just kind of all that you've written about and building this product is kind of just what you said, where it's like, it's a weather app, but you have been able to, I think, take it into a place where it is, you have a pretty small subset of features, but they're all very powerful and they are handling that really well. Does that feel like that's true to you? Yeah, I think that it's kind of like maybe in early days with iPhone development, everybody made like a Twitter app or something like that.
I think weather apps are often something that developers, especially if you're new to a platform, might get drawn to because it's sort of a constrained domain. You're going to show the weather and a chance of rain and all that kind of stuff. But it still gives you a lot of room to be flexible and creative. And something that we really work on is this stuff is really complicated. Dealing with all these different APIs and the scalability and inevitable errors aren't really your fault. So we work really hard to sort of push all that stuff down and make it not only not a problem for our users, but also for ourselves because I'm the developer.
this admin and everything. So I don't know if we're doing now something like 500 requests per minute. I really don't want to get paged ever. I'd like to take bike rides and not have my computer with me if I'm like going out to dinner or something like that. And basically we've been able to do that. And now one of the things that I want to talk to you about is in the Ruby world, there's this new-ish async and fiber stuff, which is sort of bubbled up throughout the Ruby ecosystem. And now you can even use it pretty well in Rails, starting in Rails 7.2. It's pretty much a clear shot.
to just start using all this async stuff, which has been incredibly good for us because it made it so that I can still program in Ruby and even still use Rails for something I think generally you might have to drop into Node or Elixir in Phoenix or something like that. Now these things I think can be done in Ruby, which is really nice for me because I love working in Ruby. Yeah, same. And I think that's true for a lot of our listeners too. It's funny because when you said it seemed like a weather app, right? It's like, how complicated could it really be? And then you're talking about like, oh, actually you have to deal with all these APIs. You're making so many requests. I think that is just true for a lot of web apps these days. That complexity ends up being like, you don't really think too much about it, but you're like, oh, part of what is valuable about the software we build is how it interacts and connects with all these other things. And yeah, I almost think that it's not as uncommon of a problem or kind of like...
technical challenge, as you might think. So I'm really excited about what you said in terms of a moment where you're like, oh, like this thing is scaling and I now need to make a decision about how to keep this sustainable for me and what are my options, right? And you were saying like, you know, you're trying out some different languages and frameworks. What did that look like for you? When did you realize that you needed to do that? And how did you then arrive at Async Group? Because like you said, it is pretty new. And I'm curious, it sounds like you were kind of going through that right at the right time almost. Yeah. So I guess...
If we go back to sort of the start of our Hello Weather app, Jonas and I really just knew how to build Rails apps because that's pretty much what Basing app does, right? And we built a web app that you can use as a home screen icon or whatever you call those. And then people liked it enough that we wrapped it up into like a WebView wrapper app. And then people liked that enough where it was kind of like slow. This was sort of my first dipping my toe into iPhone development where we tried to parallelize the fetching of your current location and then the fetching of the data and then the reverse geolocating so you can show the person's address, which is already, I know that sounds like...
It's already crazy, right? So we're dealing with all this stuff where it's like, it's working on the website fine, but we want it to work on the phone. That's, you know, making its way into the product. And then one of our sort of like early features where we decided to start making it like a paid app was you can use it for free, but you can pay if you want to unlock all these different data sources. So if you want to use AccuWeather or the weather channel or whatever, you can pay and then you can like choose which data source you want. And with these data sources, they're all very different. And one of the big differences versus Dark Sky, which Apple recently acquired and they shut down their old API and moved it into like an Apple weather kit kind of service, which has been great.
But when it was Dark Sky originally, it couldn't have been easier to use. It was one call, you got all the data, and that was very straightforward. As you add more sources, a lot of them, you have to do multiple requests at the same time and then compile that stuff into a sensible full weather app screen. And I don't know, AccuWeather does six different requests or something. So pretty quickly, I realized I had to figure out a way to make those requests run in parallel. You don't want them to run serially, then it's going to take forever. So at that time, we were using Rails and Puma and a gem called Typhius, which lets you issue a bunch of requests at once and then wait.
for them all to come back before you proceed. And that was going fairly well. But I would have some times where if one of the APIs got really slow for some reason, who knows if they're doing maintenance or I don't know what goes on, it would get slow for everybody. And I was like, well, it's turning from a side project into a side business. Let me invest some more time in this. I'll rebuild what we have and maybe switch to Node or maybe switch to Elixir. I'm interested in the Phoenix framework. Maybe I'll look into that. I had done some work a long time ago when Devize was just being built.
on the documentation to try to further the adoption of device because I thought it was really nice. And so I was kind of friendly with Jose Valim, who was the one who started Elixir and is involved in the Phoenix framework. And I believe he basically switched over because he was tired of all this multi-threaded world and all these thread bugs and stuff that can happen in Ruby and realized that this Erlang stuff was really purpose-built for this. Anyways, I had a call with him and he pointed out, which I hadn't seen, the new stuff going on with fibers and async in Ruby. So that let me sort of come back to working in Ruby, which is a language I'm much more comfortable in. I could reuse a bunch of my code.
And basically, I switched out Puma for Falcon, which is the fiber-based server that pairs best with async stuff. And instead of TypeScript, I switched to async HTTP, which basically did the same job for me. But it's more because of the kind of application that I have here, where I'm mostly waiting on external API clients that sometimes go incredibly slowly. This is a better fit. Whereas normally you might use Node or some other framework that's like an invented system. This let me keep using Ruby. And in the last, even honestly, the last few months, there's been so much work done in Rails to make...
Rails more compatible with Fibers, that now it's a pretty new thing that it's possible to use Async and Fibers and Falcon with out-of-the-box Rails, which has been incredible for me because I wanted to keep working in Ruby. It's funny that you mentioned like, oh yeah, once you needed to integrate with a different data source, you're like, I need to reevaluate some things. But it's cool that, I don't know, I think it just reminds me of you aren't going to need it until you do. And it seemed like this was that kind of turning point almost, right? Could you share a little bit about...
how using fibers is different than threads in Ruby for folks who haven't heard too much about it yet? So I'm not great at this kind of stuff, but I'll do my best. The way that I think about it is normally, if you have like a Rails app running Puma or something, you are, I think an easy way to think about it is you're sort of like, memory constrained. You're going to need more dynos on Heroku or whatever. You need more servers because you need more memory because you can only support so many incoming requests at a time based on how many threads you have and how many workers you have and all that kind of thing. And that works really well. I think for most use cases, when you're talking to your own database, you have the right database indexes, everything you're doing is fast. If you want to send an email, you drop into a background job. And I love the stuff that's going on with Rails now where they're building a bunch of the caching and background job and stuff in the database. I think that's going to be really nice for newcomers and just have like...
less things you have to worry about configuring and building. Because you really do very, you know, you were mentioning this earlier, like nowadays, you almost always are talking to some external API or at least sending emails or talking to some OAuth provider or Auth0 or something like that, right? So all that kind of stuff, as long as it's going fast or you drop into a background job, you're doing well, you're constrained more by your memory. In my case, where I'm basically like a proxy, I'm taking a request in and it's saying, hey, I'd like the weather data for this location from AccuWeather. Then I need to fire off like five HTTP requests. I need to wait for them all to come back. And I rearrange the JSON according to whatever the iPhone app.
person's options are and send back a json blob or in some cases in the older apps a whole rendered web view you know web page in that case the vast majority of the time spent in my app is waiting on io And that doesn't mean I sort of, I want to be always careful when I'm talking about Falcon and Async versus like Puma or whatever. From my understanding, again, I'm not really a super DevOps kind of person, but my understanding is that the vast majority of Rails applications are still better off sticking with the threaded world and Puma and, you know, using Sidekick or whatever. But if you're building something that's like a proxy and you're spending a lot of time just waiting on IO, waiting on API requests or databases you don't control or something like that, then this sort of evented system changes you from being memory bound.
to more like being CPU bound. So it looks more like a node app. Where do you spend your time? What are you constrained by? Is like parsing JSON and building JSON responses and stuff like that, which can be pretty CPU intensive. So when I'm scaling my app now, I'm basically, instead of worrying about the RAM, as long as I stay on the like small dyno RAM ceiling, great. And then I just watch my dyno load on Heroku, make sure that I'm at like 80% utilization or better than that. That's pretty much it. So long story short, I think for some cases, I guess the way to think about this is...
I think maybe a simplified way to think about it is threads are like memory bound and fibers are like CPU bound. At least that's been my experience with it. Yeah. Thank you. I know that you and I kind of just, when we were talking about how we were going to have this conversation about async, you know, you had mentioned to me that like.
For the most part, like you mentioned, your standard Rails tools are enough and you want to be careful about recognizing that async isn't just a magic wand that'll make your app faster. There's a right use case for it. I'm curious because I think that you didn't just be like, okay, time to use this thing. Let's see if it works in production. I think you did some exploration and tried some different iterations of it and introduced it to the weather machine in different ways. So I'm wondering if you could talk about how would someone go about that and evaluating whether this would work for them.
Yeah, so I guess one really simple way to go about things is if you're using something like Typius, or if you're issuing API requests or HTTP calls to various external services, you could just use async HTTP. So that'll come along with the async gem, which has all these hooks into the fiber stuff that's in Ruby core. And that would be a good way to get started. There's a caveat there where I know a lot of larger, older Rails app, like stuff that I've worked on with my clients or whatever. You end up with 10 different HTTP clients. So I hate to say, maybe add another one. But that's a very good way to get started. Another thing to look at is if you're using GraphQL at all, the GraphQL Ruby gem is a lovely gem if you like GraphQL. And they have been adding an async data loader, which uses async in the background.
GraphQL is a really good example of something that might be better off in this async fiber world, where if you're using GraphQL as a proxy or a central hub to talk to your Elasticsearch or talk to your old monolith app over here or whatever, if you're talking to different sources, then you could use this async data loader that would basically let you be significantly more efficient if you're doing what I'm doing with the weather data calls where you're waiting on unknown API requests, who knows how long they're going to take. But yeah, so for getting started, I would say if you're doing any kind of external work, even if you don't switch servers or anything like that. you could just drop into async HTTP and so forth. There's also gems for like Redis. But at this point, even with Rails, you're fine to use Active Record and Postgres has the fiber hooks built in. So even if you don't realize it, you're probably already using fibers. So that's something to keep in mind too, is like the author of async, Samuel Williams, is a very smart and kind person. And I think he chose to work on this very difficult problem and to sort of like gradually introduce fibers and async into the Ruby ecosystem in such a way that it's like...
not disruptive and it's easy to use if you'd like. I've been finding it. I'm dealing with iPhone app with Swift and the new Swift concurrency stuff is much more like the async await world. And when I come back to Ruby and get to use the async tools as they are, it's just a real pleasure. Another thing you could do is right now, this is brand new. I might be the only person using it in production at this point, but Samuel's been working on something called async job and also an active job adapter. And I've used that now to...
I have active jobs and I was using Sidekick and now I'm using async job. It was very easy for me to switch. And because of the way I'm doing it just as like inline jobs, I don't need a job server anymore. So if you're using Falcon, Rails, active job and async job. That's really neat. It's kind of crazy. So it's like, instead of running a job in a background queue, you just like drop into a fiber. The only downside with that is if you have jobs that are like super critical, you wouldn't want to do that because if a server dies, your job dies with it. In my case, it's like, it's very, mostly I'm doing like metrics and usage tracking and stuff. So it's not really a big deal.
Very cool. Yeah, one of the things that I noticed when I was kind of exploring it on my own was just that a lot of the async gems are kind of plug and play, that you don't have to bring in everything. And then I think I have a ThoughtBot colleague, Matthias Richard, who has shared a lot of neat things about async over on our ThoughtBot blog. And one of the things that he has, there's a blog post called Async Ruby on Rails that he wrote and kind of talks about a couple of different ways to start introducing it and playing around with it if you're on Rails 7, the low async active record. query method seemed really cool for just those kind of controller actions where you're trying to load a lot of different queries. I definitely hear you about like, oh, like...
introducing another way of doing things. But I almost think that this does feel like a place where experiments are kind of the way to go and kind of validating. Does this actually help with the goals of what I'm trying to accomplish? And, you know, it's one more tool, right? And it's one other thing to try or reach for. Yeah, I guess I would think about it mostly in terms of, I think the sweet spot for me, at least, is if you were tempted to like run some kind of node server, if you think, oh, geez, I probably can't use Rails or Ruby for this. At this point, I think you probably can use Ruby and async. It's like another tool in Ruby's toolbox.
And, you know, that along with all the wonderful work that, you know, Ruby core and all the people from Shopify and work on YGIT and all that kind of stuff is like, not that Ruby was ever too slow, but now I think it's really, really solid and you can use it for a lot more than you think. A funny story about just to think about where we are in this sort of like async journey, especially with Rails. With Rails 7.2, the reason that I would say you don't really want to use before Rails 7.2 with async stuff, before Rails 7.2, if you have a request coming in, it goes through Action Controller and it does any database query. That's going to open up a connection or take a connection from your connection pool for talking to the database. And it's going to hold that connection until it returns the response, no matter what. And it's sort of been a long journey and a whole lot of work, especially by Jean Bossier with the Shopify team, to change Rails and ActiveRecord behavior so that it checks in and out that database connection every time it issues a query. Because basically, I was trying to stay on the cutting edge, for example, with Rails 7.1. And I was introducing some database stuff to my very hot code pass.
completely crashed my database trying to open thousands of connections at once because I didn't realize that it was keeping the connection open for that long. So there's a lot of little stuff like that where I think apparently the people who work on Rails were willing to work on this even though async is a niche case because that is, even though it's a lot of work to change, more efficient for everyone. So I think part of what we're seeing here is that the Ruby ecosystem is just getting better for everyone. And it's also really cool that it's enabling these new use cases too. Yeah, that's really neat. So one of the things I was kind of picking up on is that because you are using...
these new features or developments in async and how it is integrating with Rails. Maybe it's not all perfect, right? Or totally free of hiccups. I'm curious, like... As you try things out, how do you engage with being involved with how they get improved in upstream in Rails or as a user of it? Where are you giving feedback on how it's going? Yeah, so I was thinking back to your conversation a couple weeks ago with Steve Polito talking about working in open source. I do some open source stuff. I've done some contributions to Rails back in the day. One of my jobs at Basecamp was, I forget what version it was, but I think I was maybe...
moving our main app from Rails 4 to 5 or something like that? And what bugs do I run into? Because I've done some actual open source work, but I think that's something that's often overlooked is, especially if you're the kind of person who's an early adopter or you're technically capable, you can really help open source projects by helping with documentation. That's something that I've just done pretty much every time I get involved using a new gem. For example, I sent that story earlier about device. One of the first things I did as I was using it was just like...
completely rewrite the readme, put a bunch of examples in the wiki. And Jose was like so happy that I was doing that. Now, as Samuel's working on the async job stuff and the active record tie-ins, he doesn't work that much in Rails. I do. So I'm like, I made, I don't know, poor guy made like 10 issues when I first was using it. Like, it should work like this. It should work like that. And he works with them all now. Would he prefer that I actually do that work and make the changes? Yeah, probably. But like, I'm being nice. I'm giving like a nice issue report.
and a suggestion, you know, that kind of thing. And part of, I think also what I try to do is just in my various, you know, Slack rooms or whatever, if I'm talking to other Ruby programmers or even just being on the show, I like to try to sort of evangelize the stuff that I think is cool and, you know, encourage other people to use it. And if I'm around helping with like those little paper cuts for people, or actually, I think Samuel said for a while, at least HelloWeather was the biggest async Ruby app in the world. That was cool. I think it made him feel better that it was like actually being successful. And another thing that I've done too, I'm like a self-taught person.
I have a philosophy degree, not a computer science degree. And a lot of times I do run into things where I'm not a good enough programmer to figure something out. And I'll try to reach out to an expert. So in the case of starting to use async, I was lucky enough to catch Samuel when he was in between clients or something. And he had like two weeks. So I hired him as a coach. He helped me finish up the work that I was doing and then babysat everything as I was deploying it. There were some weird bugs that he found in the Heroku router, which was written in Erlang, I think. And he helped the Heroku fix those problems.
I never would have been able to do that, but I was lucky enough to be able to work with him. And that's something that I've done a few times is when I'm, for example, now with the Swift concurrency stuff, which is sort of the Swift world's async version, they use like async await stuff. It's really tough and tricky. And as I'm reading all these articles about it and trying to learn how this other system works, I've been lucky enough to hire a couple people who have coached me and tutored me along the way, which I think is I'm spending my own money doing it, but it's such a shortcut to like leveling up your skills and way cheaper than like going to school. So that's everything that I've kind of done. Yeah.
Just to go back to what you're saying about kind of how you engage with open source, I definitely think it takes a village. You know, it's not a single open source maintainer can only get so far. Like, you know, they need users as much as you kind of need the tool, right? And that's how it gets better. And that's how it gets to a place where, like you said, things are better for everyone. And yeah, it's like documentation, even just like talking about it, like, hey, I'm using this thing and I'm loving it. It's really helping me out for this kind of use case.
I think that's really cool. And just kind of sharing that experience, giving other people more options. I think that's always great. I actually remembered kind of what I was trying to recall earlier about something you said, which was people's assumptions about what you can't or can do with Ruby and how that I think is changing. And yeah, I think kind of part of.
this work and sharing how things are going for you is important to that. I mean, you know, there are some constraints that are very real, but like this doesn't have to be one of them or that assumption doesn't have to be one of them, right? Yeah. And I think there's like the benefits, at least to me, I like working with Ruby so much more than other languages. Like I would just, I would take a major performance hit and probably am taking a performance hit by staying with Ruby for this kind of stuff. But you know, at this point, the performance is so good. I mean, for my use case, for the amount of traffic we're doing, waiting on these API endpoints that can take one and a half seconds to return anything.
I've got, I think I was like 500 requests a minute running on three Heroku dynos. That's including all the background job processing on the lowest level dino. You can go a really long way. And I haven't even tried to optimize things terribly well. I'm sure I could do even better. I think that nowadays, especially with the new fiber hooks that are making their way into Rails and various gems like the Postgres gem, I think it's like way better than it was even a few months ago. And I've been using it now for like two years, but I think now it's like basically ready for everyday use, if that makes sense.
Yeah, that is awesome. I think kind of what you're saying about that, maybe you're taking a performance hit, but it's kind of like, I feel like that can only get better for you, right? Because things become part of the convention and more people are like looking at it and thinking about it and figuring out how it can just improve the ecosystem in general. And yeah, sometimes that's, I think could be a better trade-off than being like, okay, it's time to switch to another language and whatever other kind of hidden consequences that comes with, right? I suspect that that's something not a lot of people.
fully consider before kind of switching to a different tech that's quote unquote faster, or just kind of when you are just looking at it in a very generalized way. So yeah, I think that's, I feel really excited about trying some of these things out. I'm not an early adopter. I'm a, other people tell me what's good and then I'll think about it. And then once it's easy, like I definitely struggle with when things are lacking documentation or like I have to figure out how to use a thing. I really appreciate what it's kind of like someone has put in the effort to make it smooth for me. So yeah. Yes. And that's, that's exactly what I'm hoping to encourage more people to do. Even if you
think, oh, I should probably make a whole pull request and really try and fix this problem myself. A really nice bug report or an update to the documentation or opening an issue to describe what your workaround was. One of the first contributions, quote unquote, contributions that I made to async was putting up in the Falcon discussion board in GitHub. I think it was just putting my Heroku Falcon config, which... wasn't documented anywhere. And it took me a long time to figure out how to do it. And the fact that Heroku's router was, at least at the time, only HTTP1, there's a few config lines that you have to do that weren't in there. And I know that that was helpful for other people because it eventually made it into their official guides and everything. Yeah. Another thing actually occurs to me as if you're interested in this sort of thing and sort of a call for help, one of the last areas in Rails that's not...
really like fiber ready is the action cable stuff. There's an open issue in Rails about active cable adapterization or something. And basically it's like trying to make it so that action cable... So you can do like WebSockets and streaming and stuff like that will work better in Rails. And I think that the Rails core team, as it stands, doesn't really use Action Cable all that much, especially in combination with fibers. So if you're interested in this stuff, you have something that's like doing streaming or WebSockets, that would be a really good place to start because I think they're literally just looking for like early adopter testers right now to prove out that this actually works. So, you know, there's an opportunity to contribute just by trying things. Yeah, that's cool. I think just kind of one last thought that I had about what you're saying is that...
It almost seems like it's easier to engage with open source that's new to you that you might not totally understand, but just ask about it, right? And just to kind of have conversations, you said that you reached out to Sam Williams just because you're like, I want to use this, I need help. And yeah, they are, I feel like it can be just a really mutually beneficial kind of interaction, right? Where they get to see, if you're like the users, the customers or whatever of these tools, right? They get to see what people want out of it and kind of the hiccups and you get to learn about how it works and that.
I don't know. To me, it sounds like a really good way for a healthy open source ecosystem to develop. Yeah, for sure. Especially when it's new stuff. It's like you do all this work and then nobody's using it. That's no point. So if you're a friendly person who's trying to use the new stuff and you're excited about it, but you have some questions or you want to share what you've found along the way, then that's super helpful and I think really encouraging. I hope it's more encouraging and annoying.
Cool. Well, Trevor, I really appreciate you sharing just like strategies for making, you know, quote unquote, cutting edge, open source and Rails more accessible to folks who are curious about it or kind of like lurkers, if you will, on how to better understand it, kind of help also play a role in shaping the direction of that ecosystem. So I'm, again, as a late adopter, I'm sure I will benefit from all of this in the future. So yeah, thanks for coming on the show. And I had a really great time learning about Async and maybe there'll be something that's new in my world in the future.
Cool. All right. Thanks for having me. Show notes for this episode can be found at bikeshed.thoughtbot.com. If you enjoyed listening, one really easy way to support the show is to leave us a quick rating or even a review in iTunes. It really helps other folks find the show. If you have any feedback or questions, you can reach us at hosts at bikeshed.fm via email. Thank you so much for listening to The Bike Shed, and we'll see you next week. Bye!