by PathOfEclipse · 4y ago · 0 comments
1) Network speeds have increased dramatically compared to CPU speeds.
2) People don't optimize code very much.
3) Web apps tend to do more work per request than they did in the 90s.
Regardless, I've never seen an app saturate it's network pipe, but I've seen plenty saturate all their CPU cores, including relatively well-tuned ones. For instance, I wrote a Netty-based reverse proxy app once, and while I got it to run far faster than the typical app in that company, it was still CPU-bound in all my tests.
We picked Python Asyncio based Tornado async for our services. As soon as we hit scale we were getting CPU bound.
Deep diving into profiling came up with JSON parsing being the culprit.
It's a painful problem to crack once you hit those limits. In some cases you could genuinely skip parsing the JSON (by returning a raw JSON containing string to client) such as when you are simply getting data from cache or db.
In other cases you simply can't skip it. For eg when you are interfacing with a 3rd party library that will only speak JSON. At that stage you are stuck.
You could try and use a wrapper around faster native JSON parser (say uJson) but it will be a trade-off between the parsing time and the time taken to copy the string to the FFI parser and copy back the results. And deal with all the complexity that that entails.
Or you could hand it off as a job to an async queue (this might be the canonical architectural approach to prevent blocking the event loop) but then you have just shifted the problem to a different place where you'll still need to throw more instances at the problem. And this adds extra latency.
I too was in the "don't optimize prematurely" camp but picking Python today for new services IMO would be taking that principle a bit too far.
Especially considering the ergonomics that modern languages like Golang or Rust offer.
If you hit that doing something novel and obscure, sure. If it is literally parsing JSON, you spend a little effort researching non-stdlib JSON parsing libraries, pick one of the several stable much-faster-than-stdlib ones, and move on.
Once the team has the know-how to write a service in Rust/Golang or even the modern pleasant to write Java, it becomes hard to justify why we'd pick Python for a new service at all.
He very much did not explicitly mention that its already done, with established results, and that the described “pain” isn’t something you have to take on at all.
Once again, I said to look at what your load actually is. If you're CPU bound, then yes, buy a bigger instance, optimize, or switch languages.
It's like how if you are putting up a blog, you probably shouldn't be looking at running kubernetes clusters for just that blog.
And that's not even taking into account frameworks and ORMs like Hibernate, which itself can multiply the CPU used several-fold on top of JDBC, or whatever lower-level interface it wraps. I've never measured frameworks in dynamic languages, but I have no reason to believe they aren't similarly inefficient.
And, yes, one of the optimizations I did for my reverse proxy app was to upgrade the JSON library, which brought a significant performance boost. But it's not the only source of CPU usage for apps, nor was it the only major optimization I successfully applied.
True, but that has nothing to do with what OP said:
> you spend a large amount of time waiting on database responses.