Rails Business
Brendan Buckingham and Ryan Frisch talk about developing with Ruby on Rails and how to leverage it to build a business.
Rails Business
Nate Berkopec
Use Left/Right to seek, Home/End to jump to start or end. Hold shift to jump forward or backward.
In this episode, Nate Berkopec, the proprietor of Speedshop, a Ruby on Rails performance consultancy, author of several books including the Complete Guide to Rails Performance, and a maintainer of Puma, joins the podcast. Nate discusses his journey into Rails performance optimization, starting from his early career at startups and his breakthrough talk at the Gotham Ruby Conference. He outlines his 'Three S' framework—Speed, Scalability, and Stability—for tackling performance issues. The discussion includes practical advice for Heroku users on optimizing 'dynos' for better request queuing and performance, as well as insights on using tools like JudoScale and PG Analyze for better Sidekiq job management and database optimization. Nate also touches on the importance of setting proper performance thresholds to balance system efficiency and operational costs.
00:00 Introduction to Nate Berkopec and Speedshop
00:45 Nate's Early Career and Interest in Performance
03:36 The Impact of Nate's Conference Talk and Book
04:40 Challenges in Diagnosing Performance Issues
10:22 Framework for Tackling Performance Problems
17:27 Understanding Heroku's Load Balancing
27:39 Understanding TCP Open Failures
28:10 Optimizing Dynos on Heroku
29:16 Setting Performance Thresholds
31:12 The Importance of Monitoring and Automation
34:25 Challenges with Sidekiq and Data Processing
36:24 Best Practices for Sidekiq Queues
44:48 Ensuring Job Idempotency and Efficiency
49:26 Rapid Fire Tips for Indies
52:41 Conclusion and Final Thoughts
LINKS
- The Complete Guide to Rails Performance Book
- High Availabillity Wikipedia Article
Questions or comments, email us at railsbusinesspod@gmail.com
Today we are joined by Nate Berkopec, proprietor of Speedshop, which is a Ruby on Rails performance consultancy and also contributor to plenty of open source things including Puma and Rails itself, I believe, if I'm not mistaken there. Welcome to the, the podcast.
Nate:Hey, thanks. Yeah, not extensively to Rails. I probably got like a dozen commits over the 15 years that I've worked with this wonderful framework. But, uh, yeah, uh, I appreciate it. Thank you.
Ryan:Well, thank you for your, your contributions. I know that we're, we're not alone in saying that. Um, maybe to start out, just, you know, tell us a a little bit about Speed Shop and how you got, sort of started with that and, and why Performance. I.
Nate:Well, I started off my career as like a kind of I, well, I worked at a startup that was using Rails and. Two startups actually. And then I freelanced her all after that. And at the second startup, it was a, like one of these, it was uh, a coffee subscription box. Those were like really popular in the, you know, 2010s was like, everybody's gotta launch a subscription box and, uh. I remember having a lot of fun working on optimizing the checkout of that site and making it faster because I had read stuff online that was like, you know, performance and the checkout flow was really important and I knew it wasn't that good. So like I, I remember getting a big kick out of that and then like two or three years later I gave a conference talk at, what was the name of the conference at that time? I think they just called it Gotham Ruby. Um. And that was the New York City Ruby Conference at the time. And like at the last minute, Mike Dalessio, maintainer of Nokogiri, currently works at uh, 37 Signals. He like emailed me and was like, we had somebody pull out, can you give a talk at the last minute? And I was like, yeah, dude, sure. And I, I very clearly remember, um. I think I was on like right after, or right before. Uh, Eileen Uchitelle, who was giving her first conference talk ever? I think, um, and. Uh, I was really interested in the time in Turbo, and I had basically seen Twitter spats online between DHH and Yehuda Yehuda at the time was pushing, uh, Ember. Um, and I was like, I don't like. Writing JavaScript, I'd like to write less JavaScripts. And this turbo thing really appeals to me, but like, how fast is it really? And so I, I redid what was really popular at the time. It was the, um, do you guys remember the to-do list? Uh, and what do they call it? It was like a to-do list app that like everybody,
Brendan:I, I remember that thing. There were all
Nate:Do it in all the different frameworks. Yeah. So I did it in Turbo Links and then compared the code and the performance to Ember. And what I found was, is there wasn't really a significant difference in the performance. Um, and, uh. That conference talk and I think subsequent blog post got like retweeted by DHH, got this huge takeup and I was like, oh, people are really interested in this performance thing. So this is all like 2015 and then like I took six months to write, six to nine months I think to write my complete guide to RA's performance and then launch that in March. And then. Sales of that took off and then I kind of never looked back and, uh, had been doing rails performance stuff, uh, ever since 10 years now.
Brendan:So, so the book, it sounds like, came before the consulting, or is that not quite right?
Nate:I mean, I was like, like, uh, what would you call it? Like warm body freelancing before that. Like I was just
Brendan:Oh,
Nate:you know, just another rails guy you could hire.
Brendan:mm-hmm.
Nate:Then after the book came out, I was quite literally the guy that wrote the book on Rails performance and all my work. My rate basically doubled immediately, and then I only was getting work for performance.
Brendan:Got it.
Ryan:All right. Well, so, so pre-show you said you like specifics and I've now read your book several times. The challenge that I often face and you know, you can, you can insert any a PM tool you want, right? But I get into the a PM tool. And, and let's say our, our app is having some sort of performance problem where queue time has spiked.
Nate:Mm-hmm.
Ryan:And I go into the a PM thinking, all right, let's try to fix this. And I get completely lost. I don't know what to look for it. It feels like I am searching for a needle in the haystack. Right. Um, and I don't, you know, I usually spend an afternoon then tuning, you know, I'm on our apps on Heroku, so I start playing the game of, right, am I scaling properly? Do I have the right, you know, number of Dynos, the right number of threads? And sometimes I, I get lucky, I think, and it fixes the problem. But most times I feel like I'm a, a fish outer water. Right. So my first question to you is, is that a flawed. Process like it, it feels like queue time, you know, even though that is maybe the right metric to look at, is hard to diagnose.
Nate:It certainly is. Um, I think it is the right metric to look at most of the time. Um, I think it's interesting, you know, that this, that that whole process you just described, it couldn't, it might not necessarily be queue time, but maybe some other metric that you see on a dashboard that like worries you, that that has not. Gotten better or easier to deal with in the last 10 years. Like the overall quality of like a PM tools has improved in 10 years, but like they still don't close the loop for you there. Um, and just displaying the numbers kind of as far as it goes and. Even now, I find, um, you know, LLMs are pretty decent tools for auto completing a good idea that you already have. They're pretty good at teaching you and being a tutor. But they're not very good at analysis and making decisions, and part of the reason for that is because they are overconfident and they're always gonna give you an answer, even if they don't really have a, even if they, there's, there's no, they don't really know, and they also don't know what you don't know, so they. They give you an answer based on the context provided, but they don't know how to stop like a good consultant does and say, okay, well what about X, Y, Z things that you know are related but you didn't consider what? What is the data that's missing here in order for me to give you an accurate. Uh, recommendation, right? No LLM will, will stop and ask for that additional context. There's some ways to like prompt it to give you that, but even then, the questions they ask are not that great. So I think it's interesting that this workflow has, uh, persisted for so long as needed to persist. And so. On a, on a, unfortunately, you know, on a practical level, I don't think that there is been, there, there is unfortunately no shortcut to knowing your stuff at this point. Like there's, I, I don't have like a off the shelf like this thing is. Turns your, your rails app into like a self-healing performance, you know, uh, juggernaut, like, and
Ryan:what I'd be asking is like, what is the, what is the framework to tackle that type of, of problem? Right? Because like, the alternative is, I, I can go into an a PM and I can see, all right, here are, here are the slow endpoints, here are the slow actions. Here are the n plus ones. And yes, obviously cleaning those up is gonna be helpful over time. But it doesn't solve the fact that my app is unavailable at the moment.
Nate:Yeah. Framework is a good, let
Ryan:I could, I could spend months cleaning all those things up and still have the same problem. So I, I feel like I don't have the tooling or even the, the framework to, maybe it's a tooling problem. I don't know. Maybe it's a framework problem. I, I interrupted you, so I apologize if maybe you were gonna answer that.
Nate:As, as an, as an aside, I think so many people that I come to me with consulting aren't even tracking request queue time. So like, and, and of course the tool is, none of these tools are opinionated enough to say, Hey, there's no request queue time here. You're missing this enormous. Enormously important metric, you need to do X, y, Z to track that. So like probably 50% of the people that come to me are missing this metric, which we can talk about like the specifics and the whole history of why this is important. But like, none of these tools are even opinionated enough to say, Hey, this number isn't missing. You gotta, you gotta do X, Y, Z to install it. So even the fact that you have this measured, like you're one step ahead, um, um. As far as a framework, I do have a framework that I use with all my, all my work. Um, and I've, it's a, it's a mnemonic that I call the three. It's the three S's, so it's speed. Scalability and stability. So speed is performance work that makes the site feel faster. It is about the human experience of latency. Literally the human computer interaction of I pushed an input and I'm waiting for the state to change so that I can do the next thing. Um. It could be the, you know, the most dominant one is I clicked something on your site and I'm waiting for the navigation to finish. That could be a full page nav. It could be a turbo powered, you know, um, nav. It could be an SPA React route change. Uh, then there's also things that involve your background. Jobs, like I clicked reset password and I'm waiting for an email. Like that involves a whole chain of things that, that involves your Sidekiq or or background job install. So all that stuff I, I file under speed, under the, it's the human experience of latency. Then there's what I call scalability, and to me, scalability is about resource efficiency because you could scale. Any service to the moon if it only would use the minimum amount of resources for a single request. Really, when we're talking about scalability, we're talking about how many resources does this one request? The filling one request use and minimizing that. And the, so it's like the it, the, the numerator. Is, uh, requests or, or transactions, and the denominator is the amount of resources that we are using. So put another way, this is also the cost efficiency of your setup. So how much are you spending on Heroku, whatever to deploy this? Because as businesses, we have the ability to also just increase that number you can solve. If any problem that can be solved by increasing this number is a scalability problem. So that's my second bucket. Third bucket stability is about responding quickly to, uh, fast changes in load. So that would be like the, uh, the derivative, the, the gradient of change is like from less than five minutes, less than something you can auto scale, uh, to, so like from. Minute, zero to minute one. You go from one request per minute to 3000 because you dropped some, uh, we're recording this before Black Friday, cyber Monday. Great example. Like e-commerce business, black Friday, cyber Monday. That's a scalability, uh, that's a stability problem. Uh, you wanna make sure that when your site goes live, it doesn't get slower. Go down, brown out, whatever. Um. These, this can also be self-inflicted, particularly with background jobs. It's a very easy way to self-inflict a stability problem on yourself. Background jobs are like a, a fire hose that you drink out of. Um, you can easily take yourself down by going from, you know, zero to by. A 2000 inserts per second on your, uh, naively implemented background job. So, um, those are my three buckets and I, I put everything that I do into one of those three. Um, and what is important or what is, uh, useful for each business is gonna be. Some combination of those and, and like it depends on the business, right? Like if you're an e-comm or consumer business, latency matters to you a lot more. If you are a B2B SaaS. Uh, a lot of them care about the scalability part more. They want to know, how do I serve? I'm about to sign up Pizza Hut. Uh, how do I, how do I deal with that increase in load? And then a lot of the more consumer people also have a stability problem because they have these big changes in, uh, in their load coming through. So the request queue issue is, it can be one of all three, but it depends on and how you address it. Depends on which of those three things is most important to you. If it's a speed issue, then we're gonna look at it from the lens of, well, how much time does this add to the average or, or P 95 request? And do I want to pay money to make that number lower? Uh, because that's what you're basically doing, is you're adding more capacity to make that number lower. If it's a scalability problem, you're looking at it from the lens of, okay, uh. Can I run fewer servers to increase my cost efficiency? What will that do to my request queue time? And as a stability problem, you're looking at it from the lens of how does my request queue time change when I drop, you know, my Black Friday Cyber Monday sale, and, uh. My, my traffic changes from zero to a hundred over the course of 60 seconds, which is way faster than my auto stealing can react. So there's a way to look at this problem from three different lenses that will all depend on what's most important to the business.
Ryan:So, a, as you're talking, I'm, I'm thinking about, you know, situations I've found myself in, in the not too distant past. Right. And I feel like it is a little bit of all three, but maybe that's just a, a naive viewpoint and, and the situation usually occurs. You know, it's the third Friday of the month, which is the month that Google decides to scrape our, you know, public websites. And we're getting crushed. You know, there, there's a drastic increase in throughput, right? We are doing auto-scaling, but maybe it's just not fast enough. We've tried to throw more. You know, in Heroku terms, dynos at it, but it had no effect. Um, which makes me think that's not, you know, I, I think I usually, we were running around, you know, somewhere between six and 20 of the 2X dynos. I tried throwing 80 at it. It did nothing, right? Like. Just so it's like I, it, it's almost like I don't know where to start. Right. That's my, my challenge. And, you know, maybe those aren't enough details for you to even comment on, but that, that's the level of performance acumen I've got right now. Right?
Nate:so you're on, you're on Heroku. You just used that term, two x dyno, right? So it's, you're on. Okay. And so let's, we can get into the specifics of request queuing now. So. Probably the first thing I'd tell you to do is you should be running on profile Dynos, uh, for web. And the reason for that is based on the queuing theory here as like how your, uh, setup works. So Heroku, um, along with most other, uh, services, uses a round robin. Routing algorithm at the load balancer level. So, uh, they, if you have, or actually hku might be random. I mean, in, in in large numbers there's no difference
Ryan:It's supposed to be random and reality. It's not. That's.
Nate:There it, at large numbers, there's no difference between round robin and, and, uh, random. It's the same thing. And smaller numbers, you might see a difference. But like the, the point here is that the. Load balancer does not communicate with a, like, does not do like any kind of back and forth with a dyno before it decides whether or not it's gonna route the request to a particular dyno. It just throws it out to one. Right. With no regard to what that dyno's current load status is. There's like a thing where we can mark a dyno as dead if it doesn't respond to, I think the whole algorithm is published online. It's like. Basically if it like five hundreds or doesn't respond a bunch of times within five seconds, like it'll get marked as dead. But that almost never happens, and it won't happen in the case of like queuing, which we're talking about. So this load balancer is just spraying load to each dyno with no regard to how loaded up it is. Okay? But in the dyno itself, you've got. One, two n number of ruby web application processes. Um, so Puma, Unicorn, pitchfork, falcon, whatever. all gonna use a process-based forking design. So they all work the same way they. We'll start a process and then call fork, create a new child process. That child process, all the child processes are listening on the same socket, the socket that the load balancer is throwing these requests out to. And each of these processes is when it's decides to, is gonna call, uh, accept on the socket and say, I'm ready to take the next request off of this socket. So when you push a request down to a dyno, the load balancing such as it is here is very different than what just happened at the load balancer level because you will not have a request on the dyno be sent. It will not be given to any. I'm gonna, I'm gonna keep, I'm gonna start saying Puma because that's like what I'm used to. It will not be given to any Puma process that. Isn't ready to accept it. Uh, because the process has to be calling, like has to be, we have to, the process has to be going through its code and then literally call accept. And in Puma there's some kind of complicated rules for when we do that. But you could basically say it's whenever we're not actively, you know, running Ruby Code in your app, we're gonna call accept and be ready for another. Uh, request. So literally like when we're idling, right? It could be idling with no load, it could be idling, um, uh, waiting for, uh, io and a different thread to complete. But literally, we're not doing anything at this moment. That's the only time we're gonna call accept, and the only time we're gonna get a new request. So that load balancing is very different and it's much more efficient and will result in less. Queuing, then the load balancing that's happening at the, uh, load balancer level, because that's just throwing stuff everywhere. The comparison here is to like a grocery store. So if you imagine going to the grocery store and you, uh, the, there's two different setups that you can get at a grocery store. They can have. 12 checkouts and a separate line for every checkout. And everyone's been in the situation where they get behind grandma at the checkout and it takes forever. She's gotta write a check, you know? And the, the alternative to that setup that you see it like a Trader Joe's, is there's 12 checkout counters and one queue. And the reason why a bunch of management consultants figured this out is because the queuing theory works out that it's better. And in the. In the one queue setup, grandma only takes down that one checkout calendar, and the other 11 are still pulling from the queue. So the average person's waiting time is lower despite there being the same amount of resources involved. So to sum this up, as long as you can make the. As long as, you know, you're spending$500 a month on Heroku anyway. Um, on, on Web you should be doing it with a perel, dyno and running, uh, eight uh, processes per dyno. Um, because you will get on average lower Q times than if you had run eight two x Dynos. Um. I guess four. I, I don't know, eight one x tinos, I guess is the comparison. Um,
Ryan:Yeah. Yeah. Because it's not exactly one-to-one, because once you go from standard to perf, you get multiple cores. Right? So that's one of the main differences. But is, is the, is the argument. So. I've heard you say, or maybe it was in your book or I don't know where I heard it, but I'm pretty sure it was you, you said you, you always want at least two Dynos running in case one crashes
Nate:I didn't say
Ryan:you.
Nate:mean, I think, I think some people think that way. I've never had a single instance of, I mean, what's your, what's your threat model? You know, like is the threat model here that. Something random on the Heroku platform is going to take down just my one dyno. And then even if that's true, what happens? Like, do they, won't they just restart the dyno and you're back up two minutes later? So like. Especially as an indie, you should not just be blindly copying the best practice of, you know, somebody who is on the hook for five nines of downtime. So. You don't need to be running an additional$500 a month of web capacity to go from three nines to five nines in a month. That's just not your threat model. So if Heroku goes down, you're going down, whether you got. One dyno or 20, right? So, um, yeah, I think when people talk about stuff like that, they really need to walk through what the reality is of their uptime expectation, um, and, and gain that out a bit because I think if you just think about it for 60 seconds, especially for you guys, it just doesn't work out. Um, so a a, a Wikipedia article that I'm constantly looking at and citing by the way, is the five nines article. So if you search five nines in Wikipedia, you'll see this table that is what it means to be X number of nines of uptime for over various time periods. And it's extremely helpful for getting bearings. Um, so like. For example, uh, three nines of uptime. So 99.9 over a year is about eight and a half hours of downtime over the year. Uh, and then they have like a whole chart that does all this for you, uh, to, to look at. So that, I use that a lot. I.
Ryan:Yep. Um, okay. Uh, we'll link that in the show notes. I got it up here on my screen. I, I think the reason I thought the two Dynos thing were, was important on Heroku. I think this is related to performance, which is the only reason I bring it up is if the dyno hits the 32nd timeout mark for any reason, Heroku kills it. Right? And yes
Nate:we, we,
Ryan:does restart, but you know, our, it takes two minutes, right? So like is that two minutes of downtime acceptable to your point? I'll have to think about that more, but I think that was my reasoning at one point.
Nate:Yeah, I'd have to go look at the documentation again. But my understanding for what causes Heroku to market dyno as unhealthy is if the dyno does not, um, respond to an open, uh, A TCP open. And there's two different things happening here. Um, so when the load balancer gives a request to a dyno, uh. We, the, what it's doing is, is it opens it TCP connection to the dyno and that TCP open is handled by the operating system, not puma, so that TCP Open will succeed kind of regardless of how busy, if all your Puma processes are, are, are. Not doing anything or as if they are doing anything, or if they're CPUs at a hundred percent that TCP open should still succeed. So it doesn't actually have anything to do with your, your 32nd timeout. Um, and I actually don't think that's how that works. I don't think that a certain number of, uh, what is it? I'm sorry. H 12 is the timeout error. Um, I don't think it has anything to do with that. I think it's. If it doesn't respond to a TCP open, if A TCP open fails a certain number of times, then it starts to get marked as unhealthy. So I've, I've certainly,
Ryan:Digging.
Nate:I've certainly seen, uh, a lot of setups that are doing tons of H twelves and the Dynos are not constantly restarting, so I don't think it's that. Um, so no, I don't think that would be a reason to run two Dynos.
Ryan:Okay. Um, you mentioned pre-show that when we had our, uh, we had an episode recently that, uh, we talked a lot about performance. You were yelling at the screen. Um. Was there anything in particular that stuck in your head that you wanted to, to dive into? I don't wanna monopolize the entire conversation on, uh, you know, optimizing Dynos on Heroku.
Nate:Um, yeah, I mean, I think for, well, I think this whole discussion about request queuing has been like helpful because I think what I want indies to do more of is to like to. Take the, like frameworks of the advice that I try to push and not necessarily the, like, I don't, uh, think that, you know, you guys should be spending$10,000 a month on Datadog despite the fact that I love that tool and, uh, love it when I have a client that's spending $10,000 a month on Datadog. That's not the appropriate choice for you. You know what I mean?
Brendan:Yep.
Nate:It is appropriate for you to think through these problems in the way that I just thought through the request queue time problem with you and to, you know, when I talked about, um, gaming out, should I run another Heroku, dyno things like, um, well what is my uptime expectation? What, what is the amount of latency on my site that I think is acceptable? Um, you. The thing I like to say is that all these requirements exist. You just haven't written them down
Ryan:Mm-hmm.
Nate:so you know. That your site has to be up. You know that your site has to respond in a particular amount of time because the requirement is not infinity. It's, it's not, okay. If your site takes 120 seconds on every click, no one would use your thing. So it's not that, right? So we know the number for what's acceptable for Ryan's site is somewhere between zero and 120 seconds. So let's just, let's just throw a number up and try to measure ourselves against that standard. Um, and same thing with uptime. Like, you know, you. You have to be up for people to pay you money. So the number is somewhere between zero and you know, nine nines of uptime or something. Right? Um, what I encourage people like you guys to do is to. Measure it and to put a line in the sand and say, listen, this is what I think I'm gonna get to this threshold and I can reevaluate it then and say, oh, actually this feels fine. So I'm, I'm not, I'm gonna lower my threshold here, but I'm gonna put this line in the sand and I'm gonna put up some monitors so that when I cross that line, I'm gonna know. And, uh, I think any business can. Benefit from that, uh, from that approach because I think particularly for indies, performance is not your value prop. It is not the thing that people are paying you for. It is a necessary condition for you to deliver your service. So I understand that you. Don't wanna spend a lot of time on this and you only wanna spend time on this such that it unblocks, you know, the value delivery for the people that are paying you for the thing that the software is actually supposed to do. So my second kind of point here is that my whole approach to performance for any size company is to try to build like a red green stoplight and say. These numbers are acceptable. They're below the thresholds or above, you know, whatever's green, such as we have said it versus red, and this needs some intervention. From a human on some kind of timeline. And that can be, that can be a timeline of like, well, we, we gotta put some tickets in the sprint this quarter, or this, this, this, this two weeks. Um, or it can be someone needs to be woken up right now, like this is an incident. Um, and most comp, almost every company I work with is failing to set those thresholds down in an automated. Uh, I just wanna say like, programmable way. Um, and instead are kind of working on vibes. And vibes will eventually lead to customers complaining. Uh, they will, they will eventually be your, uh, your backstop, uh, observability platform. So rather than let it get to that point, try to measure all this stuff and say what is acceptable? And then. Don't work on it unless it tells you it's unacceptable. I think a lot of what I'm trying to do here is like, let you sleep at night so that you're not constantly feeling like, uh, oh, I should be working on performance stuff that I, oh man, I haven't worked on performance in, in three months. Like you, I do not want you to have. A performance day, like the same way that you do your, your marketing day or whatever, right? Marketing is an infinite return activity. If you work on marketing, you are trying to grow the business, which theoretically has an infinite ceiling. If you work on performance, you're only gonna make it like, all you can do is kind of remove some cost, remove some barriers to delivering more value. So it's a much. Particularly if you're, you know, if you're in a good spot, it's not a very high return activity. So I'm trying to get you to build thresholds that say this is not acceptable or this is acceptable. And when it is acceptable, don't think about it. Like, go do the thing that you're actually trying to do with this business. Um, so I think if that's what I'm trying to get people to do,
Brendan:No, I think that's really helpful and, you know, for, for me, we've got a lot of things going on in our app, and honestly, Sidekiq, which I'd love to dive into that a little bit too, is. Maybe our biggest problem because like we have so much data coming in that we're trying to process, and it's like, you know, you get to a certain threshold of the data either in the database or being put into the database, and it's like, okay, now this worker is no longer working, or the database is now timing out when we're running this, you know, query because we've got so much data in the database or, or whatever the, the different variables are. And I, I know you also wrote a book on Sidekiq, um. That I, I've hadn't read part of
Nate:Yeah.
Brendan:May, maybe not as much as I should have, but may. Maybe just to like switch the, switch gears a little bit into Sidekiq though. Like what, what are your thoughts on, on that? You mentioned earlier that, you know, you could queue things up and take your whole site down because Sidekiq is, you know, running rampant and, you know, taking the database down I think is what your example was there. Um, I'm trying to think of like a specific question to make it a little easier for you to answer, but just. Maybe at the moment, like, do you have, do you have like some guides or framework around Sidekiq even to, to kind of point people to?
Nate:I do, and I had a very funny experience, um, just yesterday in, I was going just like scrolling through Instagram reels and I was watching this reel and it was talking about. If you have a queuing system, you should name your cues after the SLO of that queue. And I was like, wow, this is really weird and familiar. And then I looked in the comments and someone had said, oh, I wanna read more about this. What should I, what should I look at? And the guy said, there's this cool book called Sidekiq in Practice. So it was my first ever experience of like an idea that I had put out, like being fed. Back to me, like through the, through the culture, which was really weird. It is really cool. Um,
Brendan:Yeah.
Nate:so, uh, the approach that I talk about in more detail in that book is that most peop, and this gets back to what I was saying about Red Green and, and uh, deciding when to work on stuff is most people really don't have any idea. When their Sidekiq queue is in a good state versus a bad state, and they basically just sit and look at their Sidekiq dashboard all day, hitting refresh and saying, does this feel good or bad? Um, so basically if I, most of the indies I meet are spending way too much time looking at slash Sidekiq on their app, rather than going and doing something right, put it said in his hands, uh, doing something actually meaningful. So, um. What you need to be doing is a couple of things. One is take all of your cues and rename them. With a latency that is acceptable for that queue. So rather than have a queue named high or low, or even using the default queue, every queue should have in its name what is acceptable queue latency for this queue. So it might just be. The common buckets that I use are within zero seconds, so like ASAP, real time, whatever, like basically the, the queue latency for this queue should be the same as a web request, basically zero within five minutes. Within one hour. And then sometimes you have even longer queues, like within 12 hours, or even queue like a backfill queue, which actually has no SLO and it's just gonna be done when it's done.
Brendan:Yep.
Nate:So I am. Telling everybody I work with to add these SLOs to the name of the queue. And generally, I don't use any subject specific queue. So like if you have like a, I don't use like a mailer's queue because to me all jobs are, they're cattle, they're all the same. And the only thing that's important about these cattle are how quickly they're allowed to wait around or how long they're allowed to wait around.
Brendan:Yep.
Nate:Usually, most places I work with, they have too many queues. Nobody knows what the SLOs of these queues really is, and particularly as they get more and more like there's no one's paying attention to how long each of these Q latencies is, is sitting around. So the second part of this then is you need measurement of how long those what, what the queue latency of these queues is. Over time
Brendan:Sure.
Nate:for indies, I think this is tough. There's not like a great off the shelf. Solution for this. Um, I think probably the best thing available for indies right now and what you should be doing anyway is Judoscale. Because Judoscale will have this number for the last like three days or something. I think they retained. Um, it's not like as formal, like my bigger clients I will like put into place an SLO and Datadog blah, blah, blah, but like. At least in in Judoscale, even if you don't pay for Sidekiq Pro or Enterprise, they have a metric that's being tracked that you can look at for the last, I don't remember, seven days or something, and see what's actually happening with your, your Q latencies. And then of course it's an autoscaling product, so like you also get autoscaling along with it and, um. They've done a good job of supporting basically anywhere. I think anybody deploys Rails apps nowadays. So, um, I basically tell everybody you should either use Judoscale or re-implement it yourself. So like, which one would you like to do?
Brendan:Right, right. Yep. Well, Adam, who's been on a guest before, you know, he, he'll, he'll be excited to hear that recommendation. Uh, I do have a, a couple follow ups there, so let's go back to the Q names and stuff really quick. So. Our app, you know, we've got basically the, the job that we wanna run in the queue is the name. So if I, if I take what you were recommending there, and it sounds like maybe like four or five queues, potentially maybe a little bit more. But
Nate:Mm-hmm.
Brendan:if I, let's just take an example of, say I've got, uh, a job that I wanna run every hour, basically,
Nate:Mm-hmm.
Brendan:and then I've got, and it, it takes quite a while to do, let's say,
Nate:that's being queued via like Sidekiq scheduler or something. Right? Or, maybe like you're talking about something that's being queued every hour.
Brendan:correct? Yeah. Yep. And I, I want to get it done every hour so then I can trigger it again the next hour, right? And I say, let's just say I've got multiple of those and the only SLO is just get it done within that hour. But, so I guess maybe my question is, my thinking originally when I queued these up, was that. With different names and not, you know, based on the time. Is that this way I could say, okay, well this one is higher priority than the other one. But by your, what you're saying there, maybe I'm, maybe you can help me understand. If I'm putting all of the, the same jobs in the same queue, that is one within one hour.
Nate:Mm-hmm.
Brendan:I guess maybe my question becomes how do I guarantee they all still get done and within one hour if they all have the same priority, I think is what I'm thinking.
Nate:Well, you have an autoscaler. So, um, let's say for example, you have, uh, uh, for whatever, just to make the numbers simple. Like you have one dyno with one Sidekiq thread, like, you know, one process, one process concurrency. One, there's only one thing going on in this, in this dyno. Um. And you, it's starting from zero, nothing going on. You put your hourly scheduled job onto this queue, okay? Um, it gets immediately picked up and starts being processed by our one dyno. This dyno was now a hundred percent utilized. It cannot do anything else while it's working on your job.
Brendan:Yep.
Nate:Let then you put another job into this queue and. If you're auto is.
Brendan:I.
Nate:Every, let's say 30 seconds. Judoscale looks at Redis and asks, what is the oldest job in this queue, and how old is it? So what you're gonna see is 30 seconds later, the queue latency of this queue is gonna say 30 seconds. 30 seconds later, it's gonna say 60. 30 seconds later is gonna say 90. And then your autoscaler is gonna kick in at whatever threshold you set for what? For within one hour queue. I would usually set it at half of the SLO, so like 30 minutes. So that job is gonna sit in the queue for 30 minutes, and then after 30 minutes, Heroku gets the signal to start a dyno, and five minutes later you have capacity. And then. Uh, you start the job 35 minutes later after, uh, the initial scheduled job, it kicked off. So that's like the workflow. And so the actual queue latency here was about 35 minutes for this second job. Uh, and. That's like the approach that I, and then, so Judoscale will keep increasing the dyno count until queue latency goes back below 30. So if you add more jobs to the queue after that, it's gonna keep increasing the dyno count until the queue latency gets back under 30 minutes. So that's how that actually works in practice. Now what this means. You know, the actual latency of any work is the queue time, plus how long it actually takes to do, which I call a service time. And you just described a job there that sounded like it had a very long service time. Was that, am I correct about that? So that is, that is. Not something that I tend to do because, uh, it's kind of antithetical to this approach of we're gonna control the queue time. Because if the service time can also be like a random number between zero and infinity, then we don't really have any control over the total latency. So
Brendan:Yep.
Nate:for a number of reasons, but that is a big one. I'm always pushing people to keep job execution times below 30 seconds. There's two more benefits to doing that. One is item potency, Sidekiq and Heroku, when you scale down, gives your Sidekiq process a 25 second timeout. So if this big job, if something for whatever rolls this dyno over, uh, it gets 25 seconds and then it hard kills it. So if that happens in the middle of your 30 minute job. What happens, like
Brendan:you're starting all over.
Nate:Sidekiq will rein cue it. Yeah. But do you end up doing all the work again? Do you end up uh uh, missing some work? That job needs to be safe and item potent, otherwise it's not gonna work. And most people aren't very good at ensuring item potency in their jobs. So instead what I do is I tell them. Make your job fast enough that it will almost never be, you know, timed out. Um, and that's a, that's a great way of like ensuring that in potency is just like never be interrupted. So
Brendan:Yep.
Nate:the second half of this is we can do, like, Sidekiq is a distributed system, so lean into that. The idea of a single thread churning along and work for 60 Minutes is just like antithetical to the design of Sidekiq like. Almost any time you're doing that, you can redesign it so that you've got a parent job and then child jobs that you fan out to. So almost certainly you could find a way to take that job and re-architect it so that there's one job that starts and says, here's the 10,000 records we have to, uh, do this work over. I'm gonna in queue 10,000 jobs onto the within one hour cube. And then that allows the. Whole system to scale up as much as needed to finish this work in, you know, theoretically 30 seconds if you had enough Dynos, 'cause you would just have scaled up to a million Dynos. Right? So, um, finally, just one more thing on the tactical side here. Um, if you do have a job which you think must take that long amount of time, iterating over a large collection of records. Use the new Sidekiq Iterable module. This was added in Sidekiq seven. It basically allows the job to checkpoint itself, um, so that you do get much better item potency for a job if it is interrupted. So it'll save, you know, I'm on record, 5,550 out of 10,000, and then when you restart it, it'll start again at that point. So that tool is now available for you in Sidekiq as well.
Brendan:Is that just in Sidekiq or, or sorry? Is that a Sidekiq Pro feature or is that standard Sidekiq.
Nate:is standard and it's also just been added to Active Job. I don't remember what Rails version, but it's also an Active Job now.
Brendan:Well that, that's good to know about.'cause I run into that problem a lot, so I will definitely be checking into that. Excuse me,
Ryan:Yeah,
Brendan:Ryan, you were gonna say
Ryan:well you, you mentioned item potency, right? And this idea of fanning out jobs. Um.
Nate:Yep.
Ryan:And this has always confused me, which is, let's say I have a, uh, I've got a, I've got a CRM with contacts and I have an importer, right? So a spreadsheet of email addresses and names that I need to import into the CRM, right? So I, I get the parent job, I fan out, essentially one job per row, right? But how do I guarantee. idempotency across like, you know, when jobs are running in parallel, right? Like if, if there are two rows with the same email, close enough together, I often hit this race condition where one is in the process of being created and then the other one, you know, we gotta get an Active Record error saying you can't update a new record because it's like they're on top of each other.
Nate:Yeah. Yeah. You should, uh. Be using database locks. So, uh, you, you've, you've just described there's a primary key here or there's a key here that is unique and you should wrap the, the operation in that key, locking that key for as long as that job is using that is trying to work on that key.
Ryan:I, I
Nate:So, go.
Ryan:something fundamental.'cause I, I felt like when I was looking at this before that, that adding row locks defeats the purpose of parallel jobs, right? Because I.
Nate:Well, only if they were all working on the same thing at the same time. Like if you had 50,000 jobs and they were all working on the same key, then yes, it would be defeating it. But they're usually not.
Ryan:Yeah, no, I, I, I think I just overthought that and you just cleared that up in one second, so Yeah. Okay.
Nate:So, um, we're almost outta time here, but I wanna like, can I wrap? I wanna rapid fire some stuff
Ryan:Yep. Go.
Brendan:Sounds good. Go for it.
Nate:So this is, this is geared at people like you and your audience. So, um, real quick right there on what we just talked about, go read the docs on Active Record for optimistic and pessimistic locking, and just put that in your toolkit because I think that's underutilized. Two, go read the docs on Active Record batches also underutilized in Sidekiq jobs. Uh, most people I know in indies have problems with memory in their background jobs, and a big reason is that they're loading too many records in their memory at once. If you know how to use active record batches, that'll be much less of a problem for you.
Ryan:Okay.
Nate:Next, uh, everybody should have some kind of autoscaling, I kind of already said this, you should just use Judoscale, particularly if you're like an indie, this isn't, you know, something you can spend two weeks implementing yourself. Um, I think the cost benefit is there. Finally, I wanna say, I think something that I've seen a couple of times in the last few weeks that makes me think more people should be getting on this train is I've seen a lot, like one of the big worries that an indie's gonna have is what happens with my database. And there's a lot of worry around like uptime and like all my database went down. A lot of your problems are gonna happen at that database level. A couple of things there. I think PlanetScale has started to put together some crazy good offerings here that not enough people are starting to adopt. I think we're kind of just used to buying stuff from Amazon, but PlanetScale's, performance and then like, you know, kind of like. Almost Heroku Diner, like, you know, slider for like, just scale me up, bro. Like is kind of insane. Um, second thing there is if you do wanna stay on Amazon, I've recently seen some Aurora serverless installs that are crazy. So like I saw a database go from zero to a hundred thousand reads and 2000 writes per second. Just instantly and like nobody was prepared for this and it just worked. Um, it was probably not cheap, but like nothing went down. So that's a really crazy product that I'd never seen in action before. Um, and then third thing is if you do use Postgres, I think probably everybody should be paying for PG Analyze. So,
Brendan:Yeah, we, we do. But
Nate:Incredibly good product and uh, I am very impressed by how useful it is. If you don't wanna really understand how indexes work and instead just want something to tell you what indexes to use, that's a great tool for it. Um, it's also very good at helping you manage, um, like vacuums. So in these, a lot of time I see have massive databases and not that much load, uh, that will potentially sting you, uh, if you don't have your vacuum stuff together. So, um,
Ryan:You, you've just hit on all the things that are top of mind right now and that
Nate:There you go. Uh, that was my, all right. I've given everybody their to-do list. Go be, be fruitful and optimize.
Brendan:That's right. Well, thanks Nate. I know we gotta jump off here. Um, anywhere you wanna point people to before we jump off.
Nate:Um, I mostly am posting on Twitter in my newsletter these days, so I'm Nate Berkopec on Twitter, and uh, if you go to speedshop.co you'll find my newsletter
Ryan:For Line Friday. Let's
Nate:stuff there four line Fridays every week. So that's kind of the main things that you can find for me.
Ryan:well
Brendan:been enjoying those by the way, so thanks.
Ryan:Thanks so much for joining us. Uh, enjoy the rest of your week.
Nate:All right. Thanks guys.
People on this episode
Podcasts we love
Check out these other fine podcasts recommended by us, not an algorithm.
Code with Jason
Jason Swett
Standing in the Fire
Very Good Software
Remote Ruby
Chris Oliver, Andrew Mason, David Hill
IndieRails
Jess Brown & Jeremy Smith
Startups For the Rest of Us
Rob Walling
Vibing with AI Code
Brendan Buckingham