Benchmarking Python Flask versus Python FastAPI
I'm back with a truly nerdy tiny research project
Hey all!
Yeah I know, it’s been a hot minute since a Research Weekly newsletter has hit your inbox… I’ve been in a sort of transitionary period for the last couple months and have just now settled down to get back to the normal grind.
If you saw my blog on my latest career transition, you know that I’ve finally made my way into the proper dev space. No more doing dev and threat intel 50/50, now I get to bang out code full-time and really focus on building cool things.
Needless to say, I’m excited.
As I’ve taken some time off from writing on the blog and newsletter, I’ve started to think about cadence. If you followed me on YouTube for any period of time, you know that I have a bit of a hate/hate relationship with upload schedules. That said, my reasoning for switching things up this time around is moreso on fighting the urge to put out stuff for the sake of it… there’s enough of that on Twitter.
As such, the Valhalla Research Weekly is transitioning into the Valhalla Research Newsletter, and will be released monthly on or around the first weekday of every month. If I find something particularly cool or finish up on a cool project, I’ll send out another email, but it’s not going to get spammy. I think this is a better format and will allow me to send you actual research instead of a hastily written thoughtdump and a bunch of links.
Speaking of research…
Andrew Huberman’s Alcohol Podcast
I’m not just a massive consumer of podcasts. I kicked my Rogan habit a while ago when I realized how moronic 99% of that content is, listen to the occasional dev related podcast, and religiously listen to Behind the Bastards. That said, my wife happened upon Andrew Huberman’s podcast Huberman Labs, and she became hooked after the first episode she listened to.
I’d seen Huberman on Lex Fridman’s podcast and remember enjoying him but hadn’t really pursued it any more than that. After my wife listened to his podcast on alcohol, she insisted I listen to it… then three friends, separately without knowing each other, all recommended the very same episode, within the same week.
Needless to say, Huberman’s podcast episode on alcohol will terrify you.
From a massive increase in cancer risk to significant increases in stress hormones, decreases in sperm efficacy and a thousand risks big and small, alcohol is terrible for your body. It also has literally no positive side effects aside from the very short-lived good vibe you get after a glass of whiskey or a good craft beer. It jacks up your sleep, stresses you out, and, again, majorly increases your risk of cancer.
I’m not going to pretend like I completely quit drinking cold turkey, but I definitely cut back pretty majorly after finishing the episode, from 2 or so drinks a night to 2-3 a week at most. I’m not going to try to sum up the research of a Stanford doctorate, but I will urge you to go and listen for yourself.
https://hubermanlab.com/what-alcohol-does-to-your-body-brain-health/
Randy’s Game Shipping Series
Now for something very different. I’ve been someone who at least enjoys game development content quite a lot in my off time. I wish I could say this has lead to me giving it a try, but it hasn’t… I’ve just kinda sat on the sidelines and watched the other greats do their thing. Someday, maybe that will change…
Randy has been someone I’ve followed (on his first and second channel) for quite a long time. He started off with incredibly entertaining, goofy devlogs about building a game from scratch, at first without an engine at all and then on Ryan Fleury’s Telescope engine. He then went through a fairly tumultuous period where he wrestled with the act of content creation for its own sake versus development first, content later. This struggle is something I very much vibe with.
Recently, he’s started uploading minimally edited devlogs containing just the meat and potatoes and has focused far more effort on creating and shipping games and tech demos. His most recent line rendering video is fairly interesting and walks through some super cool, low-level concepts.
If you’re at least passively interested in game development or low-level rendering code, I’d check out Randy’s channels. They’re solid, entertaining and really highlight a lot of the difficulty involved in that space.
FastAPI versus Flask Benchmarking
Now for a personal project of mine: I’ve started working with FastAPI to build Python-based backends. Both by name and by reputation, I hear FastAPI is pretty… fast. I also like a lot of the syntax for creating API’s with FastAPI versus Flask, but I was curious if I could get down to a solid understanding of just how much faster FastAPI is against Flask, or if it was all-talk.
I decided to create two identical API’s in both frameworks and do some scaled benchmarking to see which was faster. I would use exactly the same test script and keep everything very simple, just one GET endpoint and one POST endpoint. The endpoints would take a parameter (or JSON post data) as input with a delay value, the backend would sleep for that inputted amount of time and then would return. The tester will randomize delay inputs and time how long it takes for each endpoint to return, discounting the actual inputted delay.
My purpose for using a delay like this is to simulate something synchronous on the backend so there’s nothing sneaky that could make one framework appear faster than the other, btw.
So, the results: Fast seems to be faster.
I say seems to be because, frankly, this test is super simplistic. It’s single-threaded, running on localhost, only simulating actual computation instead of actually doing real computation and was done at a fairly low scale with tests consisting of 100 requests per each API framework.
That said, every time I ran the test, the winner was Fast, and the delta between the two was anywhere from 13% to 35%. That’s not nothing. Of the 10-15 tests I ran, Flask didn’t beat out Fast once. As for the issues with the testing methodology, from what I’ve seen, Fast excels at multi-threading and handling requests at scale compared to Flask. The style of simulating computation probably wouldn’t affect the test that much, and any change between running on localhost and running remotely would probably be pretty much equal between the two frameworks.
I’m going to do some more testing on this, but right now the benchmarking pretty much confirms what I’ve seen, that FastAPI is genuinely faster than Flask.
—
I hope you enjoyed this Valhalla Research Newsletter. If you did, I would greatly appreciate it if you’d share it on your social media channels and give me a follow on Twitter.