Latest / Talk Python To Me / #143: Tuning Python Web App Performance
Transcript
- 0:00Do you run a web application or web service? You probably do a couple things to optimize the
- 0:04performance of your site. You make sure that the database responds quickly and more. But did you
- 0:09know a well of performance improvements actually lives inside your web servers themselves?
- 0:13Join Ben Kane and me to discuss how to optimize your Python web application as well as
- 0:19uWSGI and Nginx. This is Talk Python To Me, episode 143, recorded December 11, 2017.
- 0:26Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the
- 0:45ecosystem, and the personalities. This is your host, Michael Kennedy. Follow me on Twitter where
- 0:50I'm at mkennedy. Keep up with the show and listen to past episodes at talkpython.fm and follow the
- 0:56show on Twitter via at talkpython. This episode has been sponsored by Rollbar and GoCD. Thank them
- 1:03both for supporting the podcast by checking out what they're offering during their segments.
- 1:08Hey everyone, before we get to the interview, I want to share a quick update about our Python
- 1:12courses with you. Do you work on a software team that needs training and could really use
- 1:16a chance to level up their Python? Maybe your entire company is looking to become more proficient.
- 1:21We have special offers that make our courses here at Talk Python the best option for everyone
- 1:26you work with. Our courses don't require an ongoing subscription like so many corporate
- 1:30training options do. And they're roughly priced about the same as a book. We're here to help
- 1:36you succeed. Send us a note at sales at talkpython.fm to start a conversation. Now let's get to the
- 1:42interview. Ben, welcome to Talk Python.
- 1:44Hey, thanks, Michael. Thanks for inviting me.
- 1:46It's great to have you here. I love I'm just a sucker for a good performance talk. So I'm really
- 1:51excited to talk about all the different layers of Python web apps and performance tuning, mostly
- 1:57outside of the Python code itself, right? Yeah, well, I would say one of my my keys is you have to tune
- 2:04pretty much everything, the entire stack. So Python code, absolutely. But it's not just that. And
- 2:10that's, that's a real important thing to remember is, it's everything, because everything matters.
- 2:15Yeah, absolutely. So it's gonna be great to dig into that. But before we do, let's get to your story.
- 2:20How'd you get into programming in Python?
- 2:21In the 90s, when I was a teenager, I was making anime fan sites, which is fun, just for fun, you know,
- 2:28using HTML, CSS, and I wanted to really kick them up a notch. So I learned PHP. And that was,
- 2:35you know, kind of the web framework at the time. And I did a little bit of that for fun, mostly some for
- 2:43money. But it's funny, in my adult life, I didn't actually make a career out of it, I went into like
- 2:50retail. And then it wasn't until a friend said, dude, what are you doing? You can make these awesome
- 2:56sites. Why don't you make a career out of this? And I kind of thought about it. I was like, Oh, he's
- 3:00right. I should make some money out of this.
- 3:03This is way more fun than what I'm doing.
- 3:04Exactly, exactly. So fast forward, and here I am.
- 3:08Yeah, that's awesome. Cool. And what are you doing day to day these days?
- 3:12I'm a staff engineer at American Express. That is, is that's essentially the payment network
- 3:17for American Express. So the way I like to summarize it is it's it's really the yes,
- 3:23no machines. So when you swipe your card, that transaction has to get somewhere and that's
- 3:29somewhere to say yes and no all those systems that it goes through in order to get to that yes and no.
- 3:34That's essentially what I work on every day.
- 3:36Wow. So a lot of services, a lot of low latency demands, right? The person is standing there with
- 3:41their card, like, yeah, you know, oddly, uncomfortably looking at the cashier who's also
- 3:47uncomfortably trying not to look at them, just waiting for your systems, right?
- 3:51Yeah, exactly. And a whole bunch of them too, right? Because it's not just that one person.
- 3:55It's millions of people around the world.
- 3:58Yeah. When you think of the number of people are shopping and swiping cards and just using
- 4:02payment at any given moment, it's huge, right?
- 4:04Yeah. And then when you add things like Black Friday, Cyber Monday, all those big shopping
- 4:09holidays, it's it's huge, right? You have to...
- 4:12Does that make a big dent for you guys?
- 4:13Oh, yeah. The more people are shopping, the more they're swiping the card,
- 4:16the more our systems have to be there to say the yes and the no.
- 4:19A lot more yes, no questions, huh?
- 4:23Exactly.
- 4:23Yeah, nice. So when you talked about the PHP stuff, I kind of distracted you from the Python
- 4:31part of it.
- 4:31With Python, I was actually working at a web hosting company as a sysadmin in about 2005.
- 4:38Back then, Perl was really the language of choice for sysadmins. And my buddy, Marcel,
- 4:44introduced me to this new language called Python. And from then, it was like love at first sight.
- 4:49Like I just, I loved it. I love the syntax of it. I loved how easy it was to work with.
- 4:55But then, of course, I moved to a Perl only sysadmin shop. So I was the only one who knew
- 5:02Python. Everyone else knew Perl. So I was kind of like, all right, well, I guess I'll just write
- 5:06stuff in Perl if I have to. I was really a big fan of Python at the time. So I really didn't do much
- 5:12with it then. And then about 2013, 2014, I realized, I was at Amex at that time, I realized that the
- 5:21traditional sysadmin role was kind of dying off. And I would say at this point, it's pretty much dead.
- 5:27It's just not everyone knows it yet. Yeah, I think it's that much of a change lately.
- 5:32Yeah. So like DevOps and stuff like that, it's kind of replacing it Ansible and SaltStack.
- 5:37Oh, yeah, exactly. And if you want to stay relevant, you know, I figured you got to learn
- 5:42how to program. And that's when I really buckled down and learned how to program again, above like
- 5:49scripts, right? I've been writing scripts forever, but more than just a script. I mean, it's a big
- 5:54difference between the two. And of course, Python was my language of choice to do it.
- 5:58That's really cool. So how did you get interested in performance tuning and that kind of stuff?
- 6:04It's really part of my job. With things being the authorizations and that yes and no, like you said,
- 6:10people standing at a terminal waiting for an answer. You know, we get a lot of requests,
- 6:15and these requests have to be very, very fast. So one of the things that we're very acutely aware of
- 6:22is performance. And we actually hold ourselves to certain performance benchmarks. And we constantly
- 6:30test, do performance testing during the development cycle, just to make sure that we're meeting those
- 6:35benchmarks.
- 6:35Do you have like performance requirements or measures in say an automated build or anything like that?
- 6:42Yeah, but that's more for developer satisfaction, right? But for us, it's more about how fast those
- 6:49transactions get processed. When I think performance tuning, that's my immediate thing.
- 6:54Of course, everything else that goes along with it is important. But to me, like that's the thing that
- 7:00shines so bright in that. The cool thing is you can take what you learn from that and apply it to all
- 7:07sorts of different tools.
- 7:08Yeah, absolutely. And as a sysadmin DevOps person, you see these as the whole system,
- 7:14right? Like Linux plus the server servers and services plus the app. How does that thing work?
- 7:21Right. Or maybe even across, right? Like, how does the front end web servers plus the load balancer plus
- 7:27the back end services? How does that thing perform? It's really more what you might want to
- 7:31have like the final measure be, right?
- 7:33It all matters, right? So you have to really look at the whole picture in order to see how things
- 7:39interact or how things change the performance of a system.
- 7:43Absolutely. So one thing I kind of wanted to touch on just a little bit at the beginning here is there's
- 7:48a little bit of a difference between scalability and straight performance, right?
- 7:53If you kind of think about scalability and performance, a lot of times they're really two
- 8:00separate problems, but I do think they're very closely related. With scalability or performance,
- 8:06you might say like one request versus a million requests. And with performance, it's not just
- 8:13necessarily one request, right? You might also have a whole bunch of requests. Maybe it's not a million,
- 8:18maybe it is in one instance of an application. And you need to be able to see how many concurrent
- 8:25requests can I handle? Because that impacts performance overall.
- 8:28Yeah. One of the things I think is kind of funny to think about is you could have an app that responds
- 8:33like it takes 10 seconds to process a request, which sounds like performance sucks. But maybe if you throw
- 8:39a million requests at it and it only goes up to 11 seconds per request, that's a pretty scalable app.
- 8:44It just doesn't perform very well, right?
- 8:46Yeah, exactly. You got to make that thing faster, right? But scalability is also,
- 8:51you can talk about scale up and scale out too, right? You can scale out the number of instances. So,
- 8:57you know, if let's say your application can only handle a couple of thousand requests at a time,
- 9:04you know, by adding additional instances, you can add that many number of requests at a time. But then
- 9:10there's other trade-offs with that too. You add complexity to the application, which that's a
- 9:14trade-off not only in performance and scalability, but also with availability. And that gets a little
- 9:21tricky as well.
- 9:21Yeah. And some people can get away with like very small amounts of lack of uptime and availability.
- 9:28I suspect you guys not so much.
- 9:30Yeah, not at all.
- 9:31It's frowned upon to have the yes, no machine down.
- 9:33I don't know why. I mean, yeah, no, it's highly frowned upon to have that down. We go through
- 9:39a lot of effort just to make sure that it's up all the time.
- 9:42Yeah, I'm sure. But it totally adds more complexity, which we'll talk about. So,
- 9:47I guess one of the things to think about is the difference between performance testing and
- 9:53performance tuning. Can you maybe compare those for us?
- 9:56Performance testing, at least in my mind, although I think many would agree with me,
- 9:59performance testing is really just the execution of tests that measure performance.
- 10:05Performance tuning is more of a concerted effort to improve performance. So, you know,
- 10:12to give an example, in our development process, we have automated performance tests that run
- 10:17all the time. We know whether that benchmark is being met or not. But really, performance tuning
- 10:24is adjusting that benchmark. Is that benchmark now a higher benchmark? Does it need to necessarily go
- 10:32lower? Although generally, we never go backwards, we always go forwards. You only go backwards if you
- 10:37really have to.
- 10:37Right. Maybe you add some major feature and it's worth it, but it doesn't actually,
- 10:42it does make it go slower because it's doing more or something.
- 10:44Yeah. All of these things are about trade-offs. You gain in one area, but you trade in another
- 10:48area. So, yeah, it's very important to kind of know the difference. Really, the concerted effort
- 10:55with performance tuning, that's important. If your goal is to really squeeze every microsecond out of
- 11:01an application, it's important to have that concerted effort. And it's not just about what's the measurement
- 11:07tool telling us.
- 11:08And if you have graphs or some other kind of reporting, it's really nice to actually go,
- 11:12wait a minute, when we deployed that new version yesterday, what's the response time now than what
- 11:17it was before or memory usage now or whatever?
- 11:19Yeah. Measurement from production is huge, right? Because you can run all the tests you want in
- 11:26kind of pre-production environments, but production is where things get crazy. And if you start seeing
- 11:32differences in performance there, it really gives you an indication of where you need to start looking
- 11:38as well. And it's a really good idea to just measure both. Measure your pre-production
- 11:44environments, compare that with your production environments, and see where the differences lie
- 11:48and why they are different.
- 11:50Yeah, absolutely. Production, that's where reality lives, right?
- 11:53Exactly. That's what really matters at the end of the day.
- 11:56Exactly. You're building this stuff to run it for millions of people in production. It doesn't matter
- 12:01what your tests say. Like that's the final arbiter of how it is.
- 12:04Yeah.
- 12:04Yeah. So before we get farther into it, let's maybe take a moment and just talk about what a typical
- 12:10web stack in Python looks like.
- 12:13Really, if you kind of start at the top, you have something like Ngenix, which is a web server,
- 12:19and I'm actually skipping a whole bunch of layers, but we'll kind of talk from the web server down.
- 12:25In one web server, maybe we're not talking about the scaled out architecture with all the machines
- 12:29working together, right?
- 12:30Exactly. Exactly. So in your typical one stack kind of approach, you have your web server,
- 12:37you have things like Ngenix, Apache, there's several others. Ngenix, one being known for being very
- 12:44performant out of the box. And really what those do is they serve those HTTPS requests,
- 12:49they serve kind of the static content. And you can even do some like caching with them as well,
- 12:55which is interesting. And then you go to like your application server, you have UWSGI,
- 13:01Gunicorn, things like that. And those are really there for being the worker processes for your
- 13:08running application. So they'll start the application, they'll manage the application,
- 13:12make sure it's running, and really make sure there's enough workers of that application to handle
- 13:19those requests. And then, of course, you have your app framework as well, your web app framework. So
- 13:24Flask, Web2Py, and then Pyramid, Django, there's a whole bunch of those. All of them are kind of a
- 13:31little bit different. Some have different areas of expertise, and some have more features, some have
- 13:36less features. One of the interesting things with performance, if performance is a big factor for you,
- 13:43one of the kind of caveats, or one of the more golden rules I have is, the less features, the more
- 13:49performant it's going to be. That's not always true, but it's a good general rule of thumb, at least.
- 13:53Yeah, the less you're doing, the more you can do of it. Yeah, that's for sure.
- 13:57And then, you also have the database, and that's another whole factor to think about, and whether
- 14:02it's SQL, no SQL. Not every web app is going to have that, but a good chunk of them will.
- 14:08Yeah, most of them will have some kind of data store, and usually the choice of the database. You
- 14:13actually have some really interesting things to say about that. We'll talk about that. One thing I do
- 14:18want to maybe take a step back and talk about, because I think it's important to understand for
- 14:22people who don't live in web deployments all the time, is you talked about two web servers. You
- 14:30talked about Nginx, and you talked about uWSGI or UWSGI, and why do we need two?
- 14:35That's actually kind of important, and I think I will simplify it with Nginx is really good at what
- 14:45it does. It's really good handling some proxying. Let's take an example of HTTPS. In order to do that
- 14:54SSL handling, right, the SSL handshakes, the decryption, all of that, leveraging Nginx for that
- 15:02is very fast. It's good at that. It does that very well, and it does that very fast, and it's tuned
- 15:09specifically for that type of task. It also is really good for serving static content. So if you
- 15:17take an application server like UWSGI, that's running your Python web app. Well, if you have static content
- 15:25with that, offload that kind of workload to Nginx. Let Nginx do the static content, because that's very
- 15:31static. It doesn't need to talk to your Python app. If there's no need, then don't do it. But
- 15:36UWSGI is more for executing the requests across your Python application as well, and that's really
- 15:45kind of your worker process. Now, it all kind of, there's many ways to set up this type of stuff. You can
- 15:52do things in many different ways. Some things work better. Some setups work better for certain
- 15:57environments, but your typical deployment is going to have kind of all three, web server, the application
- 16:02server, and then that web app framework as well. Yeah. And one of the things, I don't know how much
- 16:08uWSGI suffers from this, but certainly the Python web servers themselves, the pure Python ones,
- 16:15can suffer from the fact that you only have the global interpreter log, the GIL. So you can really only do so
- 16:22much serving on any one thread at a time. And if you're busy serving up like a large image file,
- 16:30you know, you're not processing requests, right? Like, so putting something in there to like offload
- 16:37everything except for the true application requests like Nginx is, I find this pretty awesome.
- 16:43That's what Nginx is good at. So let it do its job. Yeah. And it can be like a load balancer or proxy
- 16:49server. It's really quite advanced what you can do with Nginx. You know, you mentioned run it all in
- 16:54kind of one server, one kind of instance, but that's exactly right. You can put Nginx up one level and
- 17:02have it do the load balancing across multiple backend applications. And that's really powerful as well.
- 17:08Yeah, that's awesome. Another thing that I do at the Nginx level, at least on my site,
- 17:13is that's where all the SSL exchange happens, right? Beyond that, like the uWSGI stuff,
- 17:20it doesn't even know that it's encrypted. Well, I guess because it's not, but it's in the data center,
- 17:24right? Exactly. Although even today that sometimes you're starting to see even that good, that layer
- 17:31get encrypted as well. You know, things change over time. Yeah, I can see in the more machines you
- 17:36involve, the more encrypted it is. I guess in my setup, I have uWSGI and Nginx on the same
- 17:41machine. So it's like a loop back. So encryption doesn't make as much sense.
- 17:45This portion of Talk Python To Me has been brought to you by Rollbar. One of the frustrating things
- 17:51about being a developer is dealing with errors, relying on users to report errors, digging through
- 17:57log files, trying to debug issues, or getting millions of alerts just flooding your inbox and
- 18:02ruining your day. With Rollbar's full stack error monitoring, you get the context, insight and control
- 18:07you need to find and fix bugs faster. Adding Rollbar to your Python app is as easy as pip install Rollbar.
- 18:13You can start tracking production errors and deployments in eight minutes or less.
- 18:17Are you considering self hosting tools for security or compliance reasons? Then you should really check
- 18:23out Rollbar's compliant SaaS option. Get advanced security features and meet compliance without the
- 18:28hassle of self hosting, including HIPAA, ISO 27001, Privacy Shield and more. They'd love to give you a demo.
- 18:36Give Rollbar a try today. Go to talkpython.fm/Rollbar and check them out.
- 18:41If you look at like UWSGI, that one in particular is good chunks of it are written in C, right? And that can
- 18:50also help with performance because, you know, it's C. And C is very fast. C is pre-compiled. It's got performance
- 18:58in its nature, right? So being able to leverage that is also very useful. And Ngenix is also
- 19:04written in C. And there's kind of that, you know, you use your image example. That's a really good
- 19:09example, right? That's where you can leverage that aspect of Ngenix to really get that boost
- 19:17of performance.
- 19:18Right. The threading and parallelism, that's all just, all runs over there in C. And I'm,
- 19:22you know, I'm glad I don't have to maintain that.
- 19:24Yeah, I would agree with you on that one.
- 19:26Let's start by thinking about how you might approach performance tuning. Like, I've got
- 19:30an app. It's kind of working pretty well, but certainly it could be better maybe under times
- 19:36of load. It's like too slow. Or I'm just thinking, you know, 300 millisecond response time is fine,
- 19:41but could we do 25 instead? But how do you think about this tuning problem?
- 19:46I like to think of performance tuning as if it's a science experiment. So first step is put
- 19:52on a lab coat. And then kind of after you got your lab coat established, really, you know,
- 19:58kind of start with the observation. Now, I would say one of the keys here and kind of the next step,
- 20:04which is creating questions, you know, with your observations and your questions, it's really good
- 20:09to have as many perspectives as possible. With you mentioned kind of the Linux stack, you have web
- 20:15servers, you have application servers, you have the actual Python code itself. Many times in many
- 20:21areas, you know, some of these things are managed by different people and bringing those people in to
- 20:27kind of add their input into observations and what kind of questions can be asked, you know,
- 20:33what kind of knobs can be turned, you really start to get multiple perspectives. And that's where things
- 20:39get very interesting. Now, with the same thing with science experiments is you only want to change one
- 20:46thing. And then kind of that's important as well. So as you're testing and you're validating,
- 20:52and you're kind of adjusting as necessary, only making one change at a time is very important,
- 20:59because otherwise, if you make too many changes at a time, and this is a real common mistake, I see,
- 21:04if you make too many changes at a time, you get a difference, but you don't know which thing caused
- 21:09that difference. And sometimes that leads you down like a rabbit hole, right? You start chasing something
- 21:14that you thought made a big difference. But in reality, it was something completely different.
- 21:18And that's really important.
- 21:21Yeah, or one change made it faster and one change made it slower, but you did them at the same time.
- 21:25So it looked like it had no effect.
- 21:26And another, another key piece is really establishing your baseline. And that's one thing I really talk a
- 21:34lot when I'm telling people about performance tuning is the first thing you do, the first thing you do
- 21:39before you make any changes is establish a baseline. And then you also establish a base between changes.
- 21:47So usually when you have big performance tuning effort, you're not just change one thing, and then
- 21:51everyone goes about their day, you want to change multiple things, you want to have some fun with it,
- 21:55you just want to see all the little knobs you can turn to make this thing go faster.
- 22:00So being able to stop and baseline between each kind of iteration is important. And also being able to go back to a previous state, it's important to test things individually and together in kind of separate tests. And it can take longer. And that's complicated. And people want to tend to want to rush through when they're first kind of getting started with performance tuning.
- 22:27And you just got to take your time. And it's key to really measure it very well.
- 22:31Well, and with these deployment stacks, or what do you want to call them, you know, you've got
- 22:36Nginx, and you can tune Nginx, you've got
- 22:38like with year, or Gunicorn, or whatever, you can performance tune that. And you've got your Python code. And so measuring them
- 22:46separately, I think can be a little bit challenging. While you're talking, it occurred to me that
- 22:50it's pretty easy to measure Nginx directly, maybe against a static file. If you have your app running in micro
- 22:58whiskey, you could just start hitting it, you showed for both of those scenarios, you have a B Apache benchmark, right?
- 23:03An Apache benchmark is actually comes with like the Apache to utils package. And it's a very common benchmarking tool, I would say it's, it's got its own problems, you know, there's, there's some things it does really well, some things it doesn't do, but it's a good general purpose tool. Another one I'm a big fan of is a go bench. It's written in go. It's a little bit different. It approaches benchmarking, web requests a little bit different. In some cases, I've seen it faster. And that's actually an
- 23:33interesting problem. It's an interesting problem too, is these benchmarking tools are applications in themselves, and they're running in environments themselves. So often, you can run into situations when your application is tuned so well that your benchmarking tool is actually where your bottlenecks are. And that gets like into a real interesting problem.
- 23:56Yeah, so do you do you recommend running the benchmarking tools on a separate machine with like a super low latency connection, like in the same data center or something?
- 24:04Yeah, sometimes we've just daisy chain servers to get that, that low latency connection. But yeah, absolutely. If you can run it on a different machine. That's awesome. That's great. You should do that. Sometimes, though, in order to really cut out network latency, we've had to either run it on the same machine or like I said, daisy chain some servers so that they don't go through any switches on the way.
- 24:27Yeah, definitely. It's a complicated problem, right?
- 24:57That's a lot of things that happens when a request goes through your infrastructure. Because it's easy to go, yeah, this is the config file and I put it up and then it works. But, you know, knowing more about the actual steps before it hits your code is actually pretty interesting.
- 25:10Yeah, and that's important for 3am calls as well when you have a problem, right? So the more you know kind of about your application, some of the benefits to really performance tune your application is, you know that this is why it works. And this is how it works. So finding problems is a lot easier. But that also really plays into getting even more performance out there. The more you know about your application, the more you come up with ideas on what to experiment with.
- 25:39And where to make those changes and how they would affect it.
- 26:09that we didn't have an idea of one thing. And we're like, oh yeah, this one thing is going to make our application just scream. And it had the complete opposite effect.
- 26:18Yeah, yeah. So that's a really good point. Thinking about, you know, how good our intuition is what around performance is. And so one thing I wanted to sort of wrap that up with those you have a B and you have a go bench, and you can test your server level things. But if you actually want to test your app in isolation, you know, you maybe don't even want to use the development server, you want to just call it directly. And so you could do things like time, say a unit test, or profile a unit test.
- 26:48Or something where it's literally just your code running.
- 27:18are occurring, and how long they take to occur. Now, an interesting thing is, when you do profiling, sometimes that also affects changes in performance as well. So you have to take some things with a grain of salt, but it is a really good way to kind of look at the execution of what's happening underneath the covers of all that code. And it helps you to really kind of isolate where within the actual application itself, you might make some performance improvements.
- 27:48Right, you do have to be a little bit where I'm cognizant of the sort of observer effect, sort of quantum mechanics style, right? Like, it was doing one thing until I observed it, and I did another thing. Darn it.
- 28:00Yeah, and the same is true with monitoring and production as well. I've seen several times where maybe you make this method of monitoring performance statistics, but in doing so, you actually create a load on the system, and that load then starts potentially affecting performance in itself.
- 28:20So it's all about balance. That's kind of like a key thing. And, you know, you have to look at these things. Sometimes it's worth it, and sometimes it's not worth it. And you really kind of have to take a look at your application, what you're running, and what those trade-offs are. And there's no real hard-line way to say this is worth it, or this is not worth it. It's all very situational. It depends on the application. It depends on the environment.
- 28:46And what's actually happening.
- 28:48For sure. One final thing on this profiling bit that I wanted to throw out there is, I'm pretty sure some of the other frameworks have something very, very similar. But in Pyramid, it has this thing called the debug toolbar, which lets you analyze the request and see what's happening. And it has a performance tab. And you can check a box, and it'll actually collect the C profile performance data as you click around the site on a page-by-page basis. And that's really nice to just drop in and see, okay, this page is slow because what?
- 29:15Just go request it, and then flip tabs over to the other thing.
- 29:19That sounds pretty cool. I'm a big fan of Flask, so I haven't really given Pyramid a try, but that sounds very interesting. I'll have to check that out.
- 29:26Yeah, it is pretty cool. I'm a fan of Flask as well. And I think Flask has some kind of debug toolbar, but I don't know if it has the profiling built in, because I just haven't done enough with it.
- 29:34Yeah, I haven't looked at that.
- 29:36Yeah, another thing that I feel like is often, maybe this is my perception from the outside, and it's just like, I'm looking at this like, I know the database for the site sucks. I know it. That's why it's taking five seconds to load this page. I just do. And so I feel like a lot of people skip the real optimization around the database stuff.
- 29:54Like, indexes, indexes, like, if you have a query that doesn't use an index, you need a really good justification for that, for example, in my mind.
- 30:01Yeah, absolutely. And when you talk about your traditional SQL databases, although some know SQL databases have indexes as well, you know, when you talk about your traditional SQL database, indexes are incredibly important. And think about what they do, right? And this is kind of goes down to knowing what is underneath the covers of every little piece.
- 30:23If you think about what an index is, is a database is an application itself, right? It sounds like this ominous thing, but at the end of the day, it's just an application.
- 30:33And really, indexes are a way for the database application to know where on disk is, am I most likely to find this data?
- 30:44It's a very fast way to find one little piece of data that then leads you to get all of the different data you need.
- 30:53And, you know, in SQL talk, it's really, you know, where's this index key? Let me find that key. And then boom, here's all my row of data.
- 31:02It really helps with performance. I would say indexes are very important as well. But sometimes it's also queries. Sometimes queries can be very, very complicated. Indexes are no indexes. And simplifying some of your queries, simplifying some of your database structure can really help out with performance as well.
- 31:23Right. And maybe your queries are terrible because your models are not quite right in your database. You know, I mean, there's like all sorts. We can't go too far down here, but it definitely, I think optimizing the database is something to consider, right?
- 31:35Yeah, exactly. And one thing to remember is, you know, the database was modeled at the beginning of this application. But in reality is most applications grow over time and the usage grows over time.
- 31:46So sometimes those queries get the way they get because, well, we wanted to change and have this ability to pull this data over here, but we didn't want to make major changes to the database model. And sometimes it's just necessary.
- 31:59Yeah, for sure.
- 32:00So you wrote a really interesting article called Eliminate the DBA. I don't want to go too deeply into it, but you had some really interesting ideas. I definitely want to point people at it. Maybe give us a flyover on that.
- 32:12Really, it's Eliminate the DBA for higher availability. And it's a bit of a trolling title, to be honest. A lot of DBAs internally did not like me for that post.
- 32:24But really what my point is, is when you're creating a highly available application and highly performant application, the goal is to minimize complexity because complexity leads to problems.
- 32:41The more complex an application is, the harder it is to troubleshoot. And not just an application, but an environment in total.
- 32:48The harder it is to troubleshoot, the more opportunities for failure are there.
- 32:53If you just have an application and there's no database, a database going down doesn't affect that application.
- 32:59But if both are there, you have two failure points versus one.
- 33:04And that's, you know, really kind of what the article is all about is kind of calling out that if you're going to use a database, make it worthwhile.
- 33:14Don't just use a database just to use a database because it's easier.
- 33:17That is often kind of really what the design pattern is all about is only use a database if it's absolutely necessary.
- 33:26And that's really only when you're talking about super high availability environments.
- 33:31Some environments, it doesn't really matter.
- 33:34If it's easier to use a database, then use it.
- 33:36You've got that web app.
- 33:38You've got the database.
- 33:38They talk to each other.
- 33:39Maybe they're even on the same machine.
- 33:41Maybe.
- 33:42Exactly.
- 33:42Exactly.
- 33:43And sometimes it's fine, but other times it isn't.
- 33:47And really what that article was all about is knowing when to think about should I or shouldn't I include a database in it.
- 33:56Yeah, I liked it because it made me think like at first, like, no, that's not possible.
- 33:59And then I'm like, all right, so how is it possible if I think the answer is that you can't do it?
- 34:04Do you know what I mean?
- 34:05Yeah, it's pretty cool.
- 34:07And it all depends on use case.
- 34:08Some applications, it's completely not possible.
- 34:11In other applications, it is.
- 34:13And I'll give you a really good common example, not even like card related, is, you know, my personal blog is actually a statically generated HTML.
- 34:24Now, that doesn't mean I write in HTML.
- 34:27I write my blog in Markdown, and then I use Python to take that Markdown and generate HTML.
- 34:33Some blogs, like if you look at like WordPress, for example, that's got a database backend.
- 34:39Now, my stack HTML is going to definitely be a lot less complicated to run than a whole web stack just to write a blog.
- 34:48Right.
- 34:48You might not even need a server.
- 34:49You could potentially drop it on like S3 or something.
- 34:52Yeah, exactly.
- 34:53Exactly.
- 34:53You can get real interesting once it's static pages.
- 34:55Right.
- 34:56Absolutely.
- 34:57All right.
- 34:57So there's a lot of stuff we can do at the architectural level, you know, caching, queuing, asyncio, changing the runtime to say PyPy or Cython or something.
- 35:06But I want to make sure we touch on all the stacks at a pretty good level.
- 35:09So maybe let's move up one level into MicroWSGI and say, this is the thing that runs your Python code.
- 35:18What are the knobs and levers that we can turn here?
- 35:20There's quite a few.
- 35:22One of the simplest ones is actually enabling more threads.
- 35:27So threads are interesting because you can actually go, you can go too far and have too many threads and or also go too few as well.
- 35:38And really what that is, is if you look at that configuration, it's processes equals a number.
- 35:46So one of the things that you kind of want to look at is how many CPUs does my actual machine that I'm running this on have?
- 35:55And that's your production machine, not your development machine, because those are two different things.
- 36:00And sometimes you have to also adjust for the environment as well.
- 36:05And that's something to kind of think about when you're thinking about performance tuning things is what's it run on my laptop is going to be very different than how things run in production.
- 36:14In production, you might have a machine with a whole ton of CPUs available.
- 36:19And on your laptop, you only have, you know, maybe four or eight, right?
- 36:24Depending on your machine.
- 36:25And then another thing.
- 36:27So kind of the golden rule there is try not to exceed the number of processes for a CPU.
- 36:34But I have found in some cases and some workloads, you can actually go up to twice of it and still get a performance increase.
- 36:43It's kind of interesting.
- 36:45It's really one of those things where you've got to adjust the number and slowly adjust it as you go.
- 36:51Start from the lowest and work your way up or potentially work your way down if you've already got something deployed and it's starting to hit some interesting areas.
- 37:01Yeah.
- 37:01It gets really interesting, too, because basically the parallelism of that is tied to the parallelism of Python, which has its own interesting mixes.
- 37:10Right.
- 37:10And so if you're doing something that's computational, that takes really long.
- 37:14Right.
- 37:15Like you're I mean, it doesn't have to be science.
- 37:17It could be generating a really large RSS feed.
- 37:19For example, some of us has experience with that.
- 37:21And, you know, the RSS feed for Doc Python is like 700K.
- 37:27And it's quite a bit to generate it at this point.
- 37:29At some point, I may have to do something about it, but it's hanging in there just fine.
- 37:32But, you know, that that kind of stuff that kind of locks that process up even with the threads.
- 37:37Right.
- 37:38But if what you're doing is like you're basically I come in, I process requests, I call a database, I wait, I call a web service, I wait and I give it back.
- 37:44That one can keep flying because those network I.O. things kind of break it free.
- 37:48Right.
- 37:48They release the kill.
- 37:49And so it gets really it's I think it also depends on how your app is working.
- 37:54What kind of app are you running there?
- 37:55You're exactly right.
- 37:56One thing I actually want to call out just to circle back a little bit, sorry, is there's kind of two adjustments you can make.
- 38:04You know, by default, UWSGI processes all have the same CPU affinity.
- 38:10So if you do have like a two CPU machine, for example, just changing the processes to four will actually lock all four of those to the same CPU.
- 38:20But if you enable threads equals two or enable threads equals true, what that'll actually do is I'll actually split the processes across multiple CPUs.
- 38:30And that's actually a very common problem that people run into when doing multithreading is they run into like, oh, well, I'll just add some threads and we're good to go.
- 38:42But how it actually lays out in the stack is a little bit different.
- 38:46Linux tries to get things running on the same CPU as much as possible to really leverage things like L2 cache.
- 38:53But there are ways to split it out to multiple CPUs.
- 38:58And sometimes that can really be a big benefit.
- 39:00But yeah, depending on what the application does, the reverse can be true as well, where running on that same CPU can give you that performance benefit as well.
- 39:10And like your RSS example, I would say, because you're kind of looking all at the same data and generating it, you might even get a benefit running that on one CPU versus two, right?
- 39:21Yeah, for sure.
- 39:23This portion of Talk Python To Me was brought to you by GoCD.
- 39:26GoCD is an on-premise, open-source, continuous delivery tool to help you get better visibility into and control of your team's deployments.
- 39:35With GoCD's comprehensive pipeline modeling, you can model complex workflows for multiple teams with ease.
- 39:41And GoCD's value stream map lets you track changes from commit to deploy at a glance.
- 39:47Say goodbye to deployment panic and hello to consistent, predictable deliveries.
- 39:51We all know that continuous integration is super important to the code quality of your applications.
- 39:55Choose the open-source, local CI server, GoCD.
- 39:59Learn more at talkpython.fm/gocd.
- 40:03That's talkpython.fm/gocd.
- 40:06You have an example in this, there's an article that you wrote about optimizing Mike Grovitzki or UWSGI, and you have one for Nginx that we'll talk about as well.
- 40:14And you start out as your baseline in this one at 347 requests, and just that change knocked it up quite a bit to 1068, which is quite the improvement.
- 40:24Yeah, it is.
- 40:26And that's as simple as going from one CPU to two.
- 40:28Yeah, exactly.
- 40:31It seems like, you know, math would say, well, if I have two CPU and I'm getting 347, shouldn't I get around 6800?
- 40:37So, you know, 6800 range.
- 40:41You know, sometimes you can even go a little bit higher, right?
- 40:44With you have less contention.
- 40:46Another thing to kind of think about with Linux is there's a task scheduler, right?
- 40:53And this task scheduler is figuring out what processes should I give priority to CPU time.
- 41:01And when you're all running on a single CPU, you also have other processes that are running against that single CPU.
- 41:09So you're going to have conflicts in CPU time that the task scheduler's job is to figure all that out.
- 41:15So having two kind of allows you to reduce some of those task scheduler conflicts as well.
- 41:21Yeah, it's pretty interesting.
- 41:22So the two other major things, one of them I think is somewhat obvious.
- 41:28One of them is sort of counterintuitive.
- 41:30One, you say, is to disable logging, which you may or may not want to do that based on you might want to have logs for certain reasons.
- 41:37But if you can, disabling logging actually has a pretty significant performance change.
- 41:42Yeah, because that's disk IO, essentially.
- 41:45So every log message, you know, and there's many ways to solve this problem.
- 41:53In my article, I kind of took the easy approach by just disabling it because it was an article and it was easy.
- 41:58But really what the root of that is, is by disabling logging, I'm telling, you know, uWSGI to stop writing to disk, essentially for every request.
- 42:12So for every request by default, it's just going to write to disk details about that.
- 42:17And whether it's asynchronous or synchronous, and those do matter a lot.
- 42:22You know, some platforms will default to kind of synchronous logging.
- 42:26And what that is, is it makes sure that that data is written to disk before kind of going to the next step.
- 42:33And asynchronous is more like, well, let's kick off a, let's throw this in a buffer.
- 42:37Let's kick off a thread to write these to disk and let things kind of continue.
- 42:41Those can be huge.
- 42:44Just going from synchronous to asynchronous can be a big performance increase, but nothing will give you better performance than just disabling it.
- 42:52But there's some trade-offs with that, like you said.
- 42:54It's hard to optimize faster than doing nothing.
- 42:57Yeah.
- 42:57One little tip I tend to like is actually using syslog with UDP for logging instead of going to disk.
- 43:05So syslog is a very well-established protocol.
- 43:11When you're using UDP, you don't have to worry too much about like TCP handshakes.
- 43:15It's kind of fire and forget.
- 43:17So pushing that to a network place versus disk, which, you know, disk is traditionally slow, although solid state drives have made it a lot faster.
- 43:27It's still slower than, you know, memory and going to kind of that network stack can make a big difference.
- 43:33And that's kind of an interesting trick.
- 43:36You don't necessarily lose your log data, but you also, you know, don't have to go to disk.
- 43:41But another kind of key one there is make sure you're not writing too many log entries.
- 43:49Kind of finding the right balance of how much logging is there is really important.
- 43:53Now, when you're using a framework, the framework is going to do what it's going to do.
- 43:56But even within your application, the less logging, the better.
- 44:00But at the same time, you have to have this kind of minimum amount of logging in order to support it.
- 44:04Yeah, absolutely.
- 44:06So the last one that I said was non-intuitive is you can tell the worker process to live only a certain amount of time.
- 44:15And after that time to just be killed off and basically start fresh again, which there's some, you know, startup costs.
- 44:21And there's like some cost to having this new process come up and it reads like your template files potentially or whatever.
- 44:27So it seems like that would be slow, but you actually flipped it to something like restart the worker process every 30 seconds.
- 44:33And it was quite fast.
- 44:35That really depends on the application.
- 44:37If your application is going to hold a lot of data in memory, that could be good or bad, right?
- 44:44Depending on how things start up.
- 44:45If you have to kind of load things in memory before you can really start serving requests, then that startup time really, that's a hit, right?
- 44:53But if your worker process is really just executing something very fast, restarting it, as long as it kind of starts up very quick and you have a very low startup time within the actual application itself, you can get a big benefit.
- 45:09The example I had was a very simple, simple application.
- 45:12There wasn't a whole lot of kind of data stored in memory or anything like that that you had to kind of build up.
- 45:17So the start time was really small and that's where I kind of got that benefit.
- 45:21But again, it all depends on the application.
- 45:24The benefit can be the lack of memory management or simple allocation because the memory is not fragmented.
- 45:30You know, one of the things that Instagram, pretty sure it was Instagram, did, that was a super counterintuitive for performance.
- 45:36But at its levels, they turned off the Python GC.
- 45:39They left only the reference counting bit, but that doesn't handle cycles and stuff.
- 45:44So there's definitely memory leaks when you do that.
- 45:46Yeah.
- 45:46But if you restart enough.
- 45:47Exactly.
- 45:48You just go, it's our workload, our data.
- 45:50That means we can run for six hours before we run out of memory.
- 45:53So let's just restart every hour.
- 45:55Something like that.
- 45:56And they got like 12% improvement or something.
- 45:58I mean, it was really significant.
- 45:59That goes into kind of the overall architecture, right?
- 46:02It's sometimes it's okay to kill off an application as long as you're doing it gracefully and as long as you have others to take its place.
- 46:11Right.
- 46:12And that's kind of where that whole microservices approach really lends a hand.
- 46:17Because if you break things down really small, then you can run, it's a lot easier to run multiple of them.
- 46:23So you can actually handle the distribution of load to other processes when these ones, you want to start taking them down.
- 46:32Right.
- 46:33And that's where having like Nginx up front doing some of that load balancing really plays into a big hand in that.
- 46:39For sure.
- 46:39So let's talk about Nginx.
- 46:41We've sort of said what it is.
- 46:43But just like before you had this baseline analysis, in this case, you had a little under 3000 requests per second, which this is to like a static HTML file or something like that.
- 46:53Right.
- 46:53Exactly.
- 46:54Yeah.
- 46:54So it gets all the other stuff out of the way.
- 46:56It's not to the app.
- 46:57It's just to serve up a thing.
- 46:58And so the first thing that you said that you might want to look at is worker threads.
- 47:03Again, that's just like with uWSGI, right?
- 47:07That's really the number of processes on the system.
- 47:11So a really interesting thing about Nginx is by default, it's actually at auto, which tells Nginx to create one worker thread for every CPU available to the system.
- 47:25Now, what I actually did was I changed it to two.
- 47:30So actually, no, I changed it to four.
- 47:32I had two CPUs on the system, which is basically two worker threads per CPU and two processes.
- 47:38And I actually got a pretty good boost out of that.
- 47:41And it wasn't too bad.
- 47:42But then if you changed it to eight, when I kind of was tinkering, and this goes into experimentation, right?
- 47:50Measure, measure, measure.
- 47:51When I changed it to eight, performance dropped quite a bit.
- 47:54So back it goes.
- 47:56Yeah.
- 47:56So it's a very close balance.
- 47:59It's walking a very thin line.
- 48:00You know, you can optimize things to work really well in these situations.
- 48:05But once you go a little too far, then you start hitting other contention, right?
- 48:10And that really breaks down to like that CPU task scheduler.
- 48:14And that's actually why I did that in that article is to kind of show sometimes things don't always work out when you just add more numbers.
- 48:20Yeah.
- 48:21So by messing with the worker threads, you were able to get it to go from a little under 3,000.
- 48:26You added another 2,250.
- 48:29So not quite doubling, but still quite good.
- 48:32And then the other thing you said is like, maybe some of these connections are going to last for a long time or they're sort of backed up.
- 48:39Like, what if we let it accept more connections?
- 48:41And this is important for requests, but it's super important for like really long, large files, I would imagine, or lots of them.
- 48:47People downloaded them or even like WebSockets, these persistent type of things.
- 48:52Yeah, exactly.
- 48:53And that's exactly it, right?
- 48:55Sometimes, and actually in this case, it was, I believe it was a simple REST API.
- 49:00So there wasn't really a whole lot of like static connections, but you're right.
- 49:05That is a big performance increase when you have those long live connections.
- 49:09And sometimes, you know, that's letting Ingenix do some of the work, let Ingenix kind of handle that connectivity.
- 49:16By increasing the number of connections per worker, if they're very fast requests to the downstream application,
- 49:24you can actually kind of leverage Ingenix handling connectivity with the end client and through the process very quick.
- 49:32So by making some changes, I think in that case, like I changed it to like 1024, for example, it went up to like 6,000 requests per second, which was a huge improvement.
- 49:42It's really cool.
- 49:43Now, another thing in that kind of worker space is to look at the number of open files, which is a very common Linux limitation that people run into.
- 49:55In Ingenix, as it's serving static content, for example, or even just the fact that it has logging enabled as well, it's going to have open file handles.
- 50:05And by default in Linux, there's a limitation.
- 50:08I want to say these days it's 4096, but actually, I think the Ingenix default limitation is much smaller.
- 50:16I forget what it is exactly.
- 50:17I mean, I think in that example, all I really did was just upped it to 4096, which allowed it to have even more files open, which gave a little bit of a boost, but not too much.
- 50:29Yeah, it was still really nice.
- 50:31I think it gave it like 300 more or something about it.
- 50:35Yeah, which is cool.
- 50:36But the other thing you can do is most web workloads are hitting a number of files, 10, 50, 100, or 1,000.
- 50:44But after that, how many unique static files do you have in most situations?
- 50:49I know there are some that there's tons, but most sites, they've got their CSS and their images and whatever, and it's mostly shared, right?
- 50:55So you can also tell it to cache that stuff, right?
- 50:58Yeah, you can.
- 50:59And I would say one caveat, though, is just because you only have a certain amount of files doesn't mean that that process isn't opening other files.
- 51:08Sometimes there's things like shared libraries that it opens, and all of those count as well.
- 51:12And even like sockets and things like that, they all kind of count towards the limitations in the OS.
- 51:19But in regards to caching, that's actually pretty cool.
- 51:23There's some options with NGENX to do like an open file cache, which allows you to increase the default amount of cache for open file handles.
- 51:35So NGENX will open up those CSS files and those HTML files, like you were saying, and it will actually load them in memory.
- 51:42So that way, when you get a request, even though it's a file on the file system, since NGENX has it open and it's cached in memory, it doesn't have to go to disk for access to that, which makes it a lot faster.
- 51:56Yeah, just serve it straight back out of memory.
- 51:58That's awesome.
- 51:58So in the end, you were able to tune it NGENX just on its own bit from 2,900 up to 6,900.
- 52:08That's a serious bit of change by just tweaking a few config settings.
- 52:14And then you did the same thing, you know, something similar on the UWSGI level.
- 52:18And then, of course, you could, you know, tweak your architecture as well.
- 52:21But just making the stuff that contains your app go that much faster, that's pretty awesome.
- 52:26There's another article that I wrote about kind of benchmarking Postgres.
- 52:30And I had a similar experience there is all I really did in that article was just adjust a shared buffers configuration.
- 52:38And what that is, is that's essentially a query cache for Postgres.
- 52:42And that change alone for the example I was given had a big, had a big performance increase.
- 52:49So, you know, sometimes I kind of call these a little bit of low-hanging fruit because they're just little knobs you can change in existing systems.
- 52:58And sometimes those low-hanging fruit can be a really good first step.
- 53:02None of those seem super scary, right?
- 53:04You just change some config files.
- 53:05I mean, if you mess up your config, you will take your website down.
- 53:08But, you know, you just put it back.
- 53:10It's not super complicated, right?
- 53:12The key is test before you put it in production, right?
- 53:15So as long as you're testing before production, then, you know, if you make a change and it doesn't work, then, oh, well, who cares, right?
- 53:23But really, that's kind of getting into the measuring, establishing your baseline and measuring each little change and how they interact.
- 53:32That's real important.
- 53:34So, yeah, that way, when you go to production, you know exactly what you're changing.
- 53:38Yeah, that sounds good.
- 53:39So we're just about out of time for our conversation here.
- 53:43But I did want to just ask you, like, how do things like Docker and Kubernetes change this?
- 53:48Do they just make it more complicated?
- 53:49Do they simplify it?
- 53:51Like, what do you think containers mean around this conversation?
- 53:53They simplify some things.
- 53:55Like, when we're talking like scale-out type approach, it makes it real easy to spin up a new one.
- 54:00I was kind of thinking when you asked that question, I think of some of the challenges I've ran into with those.
- 54:07One thing that I've run into with, like, Docker is if you just pull down a service and use that service out of the box without changing it, you're going to get kind of a default performance.
- 54:19So by using a Docker package, a lot of times you kind of forget about all those little knobs that you have to turn in order to get it fast and performant.
- 54:30And then another area that that kind of goes into is Docker is it has some services as well that things run through.
- 54:40And Kubernetes is a big example of this.
- 54:43So with Kubernetes, you have a software-defined network, right?
- 54:46So if you have, like, a cluster, and let's just give kind of a scenario.
- 54:52For whatever reason, you had, you know, half your cluster on one side and half your cluster on another, and there's a network latency between getting to that other half.
- 55:03With Kubernetes and services, you would have your traffic land on any host within that cluster.
- 55:10And then that software-defined networking is responsible for moving that request to the appropriate hosts that might be running that service.
- 55:17So if there's some latency in there, you can actually start seeing that.
- 55:21And that's actually very obscured away once you start getting into that area.
- 55:26It's hard to kind of pin that down because that's so far removed from what you're doing in your application that that can actually be pretty tricky.
- 55:34Plus the fact that, you know, you have to go through that software-defined networking means you're taking some penalties there.
- 55:40But again, it's probably worth it.
- 55:43Yeah, probably.
- 55:44Very, very interesting.
- 55:46All right, before I get to the final two questions for you, you said that you, we were talking before we hit record about an open source project that you're working on as well.
- 55:54You want to give a quick elevator pitch for what that is so people know about it?
- 55:57Yeah, absolutely.
- 55:58So Automatron is the open source tool that I've created.
- 56:03It's kind of a second version of something called Runbook.
- 56:07I launched an open source project called Runbook, and things happened where I was like, I need to redo this and start fresh.
- 56:15And that became Automatron.
- 56:17And really what it is, is it's kind of like if Nagios met IFTTT.
- 56:22So where you have these health checks and they monitor, you know, whatever you tell them to monitor, the health checks are really just executables.
- 56:31What it will do is it will SSH to the remote server, to the monitored system, run that health check.
- 56:39And based on kind of the Nagios return codes, it's either good or bad.
- 56:44Right.
- 56:44Like SSH in and ask for the free memory or something like that.
- 56:47Yeah, exactly.
- 56:48Exactly.
- 56:48And if it's bad, you know, if you beyond a threshold.
- 56:51If it's zero.
- 56:53Yeah, yeah, exactly.
- 56:54Then really the exit code would indicate a failure.
- 56:57And that exit code will actually trigger an action to take place, which is, again, SSHing out to a system and then executing either a command or a script or something like that.
- 57:09And this is all built in Python.
- 57:11And it actually uses Fabric very heavily, which is a really cool SSH command execution wrapper.
- 57:18It's very cool.
- 57:19I'm a big fan of it.
- 57:20And I used it very heavily there.
- 57:22And it's kind of a cool little side project.
- 57:25And really what it's there for is I hate on-call and I really wish it would go away.
- 57:31And this is one of my ways to hopefully help people make it go away.
- 57:37Could a machine just go restart the web server process and just not call me?
- 57:41Exactly.
- 57:42Or, you know, in some cases when you've kind of designed your environment well enough, you could just maybe reboot the box and who cares?
- 57:49Yeah, true.
- 57:50At a certain scale, is it worth finding the root cause of a single issue or is it more worth finding the root cause of continuous issues?
- 57:59And that's kind of one of the real philosophy changes that that kind of project brings is, in my opinion at least,
- 58:06it's worth fighting, you know, more frequent and reoccurring problems.
- 58:12And just one-off problems are not worth it.
- 58:15Just restart the thing.
- 58:16Yep.
- 58:17Sounds awesome.
- 58:18All right, cool.
- 58:18So people check that out.
- 58:19We'll put it in the show notes.
- 58:20All right, last two questions before we go.
- 58:22If you're going to write some Python code, what editor do you use?
- 58:25Right now, it's Vim screen and syntax highlighting.
- 58:28That's it.
- 58:29I'm kind of weird, I believe.
- 58:31So, but that's where I feel comfortable.
- 58:33And I think that's all those years and kind of operations has led me to that.
- 58:37Yeah, cool.
- 58:38And then notable PyPI package.
- 58:40You already called out Fabric, right?
- 58:42That's pretty awesome.
- 58:43Yeah, Fabric is an awesome one.
- 58:45I actually wanted to call out, since we're talking performance tuning, Frozen Flask.
- 58:49Now, it's not one that I've used personally quite a bit, but the whole concept is it allows
- 58:56you to pre-generate static pages from a Flask application.
- 58:59So it's really cool.
- 59:01And the real awesome thing that you can do is you can kind of combine that with certain
- 59:07like Ngenix rules to where it'll pre-generate the static HTML.
- 59:11And then based on, you know, regular expressions in the paths and things like that, you can have
- 59:17Ngenix serve that static HTML without having to go down further in the stack.
- 59:22And that's real cool because you can use Frozen Flask to still kind of keep that all within
- 59:27your Python application.
- 59:29And it's really just at runtime, it'll generate that static HTML.
- 59:32It'll freeze it.
- 59:34And just Ngenix configuration from there takes it away.
- 59:38Yeah, that's pretty awesome.
- 59:39So you get the dynamic sort of data-driven bit, but then you could just freeze it, if you
- 59:44will, like turn it to static files and then serve it through that way.
- 59:47That's cool.
- 59:47Most web applications, you know, you have static pages and dynamic pages and really using it
- 59:53to establish those static pages and pre-generate them can be a big benefit in kind of production
- 1:00:01workloads, not just from a performance perspective, but cost of what it takes to run it as well.
- 1:00:06For sure.
- 1:00:06That's really interesting.
- 1:00:07And sometimes those sort of landing pages and main catalog pages, that's where like really
- 1:00:12the busy traffic is anyway.
- 1:00:14That's pretty much my use case that I've used things like that.
- 1:00:17I haven't used that one in particular, but I've done some things like that where I just
- 1:00:21pre-fetch pages and save the HTML for, yeah, it was hacky, but you know what?
- 1:00:27It really helped keep things slim.
- 1:00:29So it works.
- 1:00:31That's awesome.
- 1:00:31Cool.
- 1:00:32All right, Ben, final call to action.
- 1:00:34People are excited.
- 1:00:35They realize like there's a few knobs that make their code much faster.
- 1:00:38What do you think?
- 1:00:39They should start by reading your two articles about optimization?
- 1:00:42And really just don't be afraid to just jump right into it.
- 1:00:46Even if you don't know how something works, you know, sometimes just turning that knob and
- 1:00:50then figuring out why it works is a real benefit.
- 1:00:52But for kind of self-promotion, for sure, check out those blog posts.
- 1:00:56You know, you can kind of follow me on Twitter.
- 1:00:58Mad Flojo is my handle.
- 1:01:00I have quite a few posts out there and lots of different stuff and lots of different kind
- 1:01:05of areas.
- 1:01:06But that's definitely a good start.
- 1:01:08Yeah.
- 1:01:08People should check out bencane.com slash archive.html because you have a ton of awesome
- 1:01:13articles there.
- 1:01:13We just chose a few to speak about.
- 1:01:15Awesome.
- 1:01:16Thanks.
- 1:01:16Yeah.
- 1:01:17I have tons of stuff, whether it's, you know, Docker related performance tuning.
- 1:01:21One article I wrote is kind of building self-healing environments using things like salt and just
- 1:01:28some Python code, which is...
- 1:01:30Very cool.
- 1:01:30Well, thanks so much for sharing your experience.
- 1:01:32It was great to chat with you.
- 1:01:33Awesome.
- 1:01:33Thank you.
- 1:01:34Thank you for having me.
- 1:01:35You bet.
- 1:01:36This has been another episode of Talk Python To Me.
- 1:01:39Today's guest has been Ben Kane, and this episode is brought to you by Rollbar and GoCD.
- 1:01:44Rollbar takes the pain out of errors.
- 1:01:47They give you the context and insight you need to quickly locate and fix errors that might have
- 1:01:52gone unnoticed until your users complain, of course.
- 1:01:54As Talk Python To Me listeners, track a ridiculous number of errors for free at rollbar.com slash
- 1:02:01Talk Python To Me.
- 1:02:03GoCD is the on-premise, open-source, continuous delivery server.
- 1:02:07Want to improve your deployment workflow but keep your code and builds in-house?
- 1:02:11Check out GoCD at talkpython.fm/gocd and take control over your process.
- 1:02:17Are you or a colleague trying to learn Python?
- 1:02:20Have you tried books and videos that just left you bored by covering topics point by point?
- 1:02:24Well, check out my online course, Python Jumpstart by Building 10 Apps at talkpython.fm slash
- 1:02:30course to experience a more engaging way to learn Python.
- 1:02:33And if you're looking for something a little more advanced, try my Write Pythonic Code course
- 1:02:38at talkpython.fm/pythonic.
- 1:02:40Be sure to subscribe to the show.
- 1:02:43Open your favorite podcatcher and search for Python.
- 1:02:45We should be right at the top.
- 1:02:46You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct
- 1:02:52RSS feed at /rss on talkpython.fm.
- 1:02:56This is your host, Michael Kennedy.
- 1:02:57Thanks so much for listening.
- 1:02:59I really appreciate it.
- 1:03:00Now get out there and write some Python code.
- 1:03:02I'll see you next time.