Sick of rewriting Python prototypes in C++. Any sane C++ web frameworks?
Posted by ExtremeMysterious603@reddit | ExperiencedDevs | View on Reddit | 61 comments
This is a constant architectural headache. We have these IO-bound microservices. We start by slapping them together with FastAPI or Flask just to validate a POC. Then the load grows, Python inevitably starts to choke, and we spend weeks porting everything to C++ using stuff like Boost.Asio or Pistache
How are you guys handling this? Does a C++ framework even exist where you can throw together a CRUD app with a DB in a day without drowning in miles of boilerplate and callback hell?
Ok-Till-2305@reddit
Nothing about C++ is sane
Peppy_Tomato@reddit
I don't like the fact that you said "python inevitably starts to choke" without substantiating the claim even a tiny bit.
Infinite_Maximum_820@reddit
You can easily scale Python . Skill issue on your team
db_peligro@reddit
IO bound performance problems by definition have nothing to do with the language your app is written in. Your code is waiting on IO!
Recently did a project where in house engineers wanted to rewrite a slow java app in C++. The underlying problem was that they don't understand ORMs and were issuing literally hundreds of queries for each request. Rewriting in C++ would have done absolutely nothing to resolve perf issues.
thekwoka@reddit
well, not entirely...
like, ones that have runtimes, IO bound performance can impact the overall throughput differently in those runtimes.
Like having concurrency models and parallelism or the lackthereof.
db_peligro@reddit
assuming your language has threads (as python does) concurrency models and languages are orthogonal.
NGTTwo@reddit
Python has threads, but is ultimately still single -threaded because of the GIL - you can't achieve parallelism using Python threads. They are working on it, and I'm expecting GIL-less Python to go mainstream in a couple of years, but it ain't there yet.
Peppy_Tomato@reddit
Python 3.14 got rid of the GIL.
ThatSituation9908@reddit
This doesn’t apply for IO bound.
Python async and multithreading works for IO tasks.
deux3xmachina@reddit
Especially when you can have a separate process with its own thread pool just for handling I/O while your logic threads handle compute. Not easy in every language, but it's absolutely possible in most, especially Python.
not_a_db_admin@reddit
What's actually choking when Python chokes? In every 'Python is too slow' rewrite I've seen, the real perf problem turned out to be the DB or some serializer doing too much work, not the language itself. Going to C++ for IO-bound CRUD feels like a lot of complexity to take on if those weren't ruled out first.
brianly@reddit
Have you ever seen someone bring this data to a reddit conversation? GIL conversations never had any data but people were certain removal would improve the problems with their I/O bound app. Many devs don’t know what you mean by X when you say X-bound.
KWillets@reddit
Cython?
vnixqpr@reddit
Dude you could probably survive way longer with go and rust before paying the complexity tax of C++
trailing_zero_count@reddit
Let's not pretend that async Rust doesn't have a "complexity tax".
LagrangeMultiplier99@reddit
how?
Smallpaul@reddit
Have you considered simply allocating more servers to the Python code? Reddit and YouTube were or are Python code bases that handle massive load.
Gold_Emotion_5064@reddit
I don’t understand this consensus that python is unable to handle load at scale. People hear its “slow” compared to other less abstracted languages then act like python is unable to support massive systems. I’d wager there is very few cases in general programming that someone would have to revert to C++ because python was actually to slow.
LagrangeMultiplier99@reddit
it's not a consensus. python is good enough for even some electronic trading companies.
Twirrim@reddit
In my experience, most of the delays are external to the language. They're dominated by network calls, database queries etc. unless you're incorporating large amounts of business logic into your front end.
Kirk_Kerman@reddit
Exactly. If python is slow at some arbitrary execution, it's still likely several orders of magnitude faster than making an I/O call or even needing to load from disk.
nux_vomica@reddit
if it’s actually io bound then interpreted python isn’t the bottleneck. you could try and use pypy or some other faster implementation instead of cpython as long as the modules you use are compatible
brueluel@reddit
Check out userver, they just dropped v3.0 with a library called userver::easy
The syntax is almost 1:1 with synchronous Python - super fast to wire up - but under the hood, you get proper coroutines, C++20, and an async Postgres driver out of the box. We’ve started using it for any new service that might hit high load so we don’t have to rewrite from scratch later
trailing_zero_count@reddit
+1 for userver, the advantage it has here is that it uses stackful coroutines / green threads so you don't have to worry about function coloring, and it provides most of the necessary features out of the box.
Otherwise you're going to be gluing together a http parser, I/O library, database library etc. I've done all this using the Boost stack using C++20 coroutines but been disappointed with the current status of async Postgres libraries. So I'm using Boost.Mysql instead. I guess Boost.Postgres is coming, but it's not available yet.
Qwertycrackers@reddit
Unironically I think this is the place for Rust. I know people just spam recommending it but for what you are asking I think it is actually the correct choice for once.
yasamoka@reddit
C++ is the absolute worst choice for a web backend.
Anything in Rust, Go, Java, C#, or TypeScript is bound to be miles better.
doomsdaydonut@reddit
Can you elaborate on what makes C++ the worst choice here? Is it the complexity and time to develop? I have developed a few web backends in C++ and didn’t have a great time, but they work well. What makes the other languages better?
dfltr@reddit
TL;DR: Making web apps with C++ is like slicing bread with an orbital laser. Power != usefulness in this case.
On top of that, the sheer number of people using Typescript for web apps means that there are many, many tools with large communities to support them.
DrShocker@reddit
If you try to write a http backend in Go, you can get started with just their standard library. In C++ it requires either writing all that code yourself or going through the pain that is adding dependencies in C++ just to use something that's no where near as well documented.
DrShocker@reddit
Coincidentally, someone posted about doing a chat backend in C++
https://www.reddit.com/r/cpp/comments/1to5dg2/what_happens_when_you_build_a_chat_server_on_one/
DrShocker@reddit
While I agree, it is mildly surprising to me that Rust took off in web backend in some ways that C++ didn't even though you could probably express somewhat similar concepts if you wanted to (although obviously not with a borrow checker)
robert4221@reddit
Infinite flexibility is not infinite power but rather infinite chaos. Google's C++ style guide is 89 pages at a normal font size...
DrShocker@reddit
Yeah as someone who does C++ for work, I definitely would rather work in a language with more opinions sometimes.
deux3xmachina@reddit
With C++, I think the real issue is the lack of hard versioning like Rust editions. It's a problem in C too, but at least there, it's usually just the function you call that changes, not the language syntax itself.
Modern C++ can be nice, if you have a way to enforce the subset(s) of the language you're using. I think this is one of the key reasons Rust has been overtaking C++ in many areas. They're similarly complex languages, but for now, Rust is more consistent and easier to dictate "we're using Rust x.y.z ONLY".
DrShocker@reddit
There's probably also something to be said for the dependency management or particularly the build system story. It's an absolute pain to setup a project that has dependencies because everyone does it differently, and their dependencies do it differently and so on. I know some languages like Odin deliberately don't have a package manager and that's probably fine, but it's gotta be easier than C++ to start working on a new project.
serial_crusher@reddit
Rewriting in another language for performance is more often a code smell than anything else. Take a step back and ask whether the language / runtime is really your bottleneck or if there's something else at play. You probably just need to add better indexes to your database.
inexorable_stratagem@reddit
I love Python and C++ but.... For a Web Backend why would you use C++? Why not Go ?
MrBloodRabbit@reddit
It feels like I'm on StackOverflow again. Multiple statements that you're doing it wrong and no one answers the question.
sfscsdsf@reddit
you gotta try rust for prototyping, and it’ll be as good performant as C++
k958320617@reddit
Monoliths FTW!
ehellas@reddit
If it is IO bound, chanigng to C alone will not necessarily improve things, the disk's speed might still bottlenecking you before the code.
Maybe reevaluate the process? And if Cpp, this is usually a case for go.
DrShocker@reddit
> this is usually a case for go
I usually see C++ bindings (or these days rust) with heavy mathy type of coding so that's my assumption over io stuff.
ehellas@reddit
It could be math heavy + io, then cpp or rust are the way. At least at for the calculation part. But OP wasnt specific enough
Massless@reddit
This is basically the whole reason Go exists. It comes together a lot like Python but scales really well.
flavius-as@reddit
Sounds like an AI post.
Ask your Agent:
But then:
CorrectPeanut5@reddit
Given AWS Lamdas work just fine with python under very heavy load I'm dubious it's a core language issue and more your implementation.
Distinct_Bad_6276@reddit
Why not keep the Python and scale horizontally?
Same_Technology_6491@reddit
You could probably survive way longer with Go and Rust before paying the complexity tax of C++ web services.
throwaway_0x90@reddit
If your CRUD app is running into performance issues under Python, porting to C++ I don't think is correct approach.
How about Java, or at least JVM based languages? Or literally any other modern programming language out there other than Python.
mico9@reddit
c++ for crud app? which planet?
CalmLake999@reddit
Why would you use C++ when you have things like Rust and Go?
thekwoka@reddit
Heck, even just Typescript would be better than writing it in Python and then porting to C++...
olddev-jobhunt@reddit
Is there a reason you can't shard things so that you can scale horizontally?
The problem with being IO bound is that you're always catching up. You can optimize to catch up to a higher level of scale but it's always a matter of incremental progress and that only increases your capacity so much. At some point, you just need another instance so you can double your capacity (or 10x it) just by increasing the parallelism.
Also, frankly - Java. Java (or Kotlin, or Scala) can be pretty quick and is higher level than C++. That might be a good middle ground.
thekwoka@reddit
Why are people using C++ for web stuff?
And why would you make the PoC in Pyton?
ilikeaffection@reddit
You say it's IO-bound. What sort of concurrency strategies are in place to deal with that bottleneck?
Physical-Compote4594@reddit
If it's I/O bound, why not use something like Elixir?
yxhuvud@reddit
Can you give some examples of what these IO bound services actually do? There are extremely few web based cases I can think of where I feel it is motivated to use C++ or similar.
OverOnTheRock@reddit
You could try drogon as a starting point. It has a few database clients. Plus it's own flavour of optional ORM.
Unrelated to Drogon, the concept of "async/await lets developers write sequential-looking code while the compiler does the painful rewrite behind the scenes" may be applicable.
-Dargs@reddit
Why do you need multiple microservices to do CRUD? If you really need multiple dedicated CRUD backends, could you not create a project template for this and just fork it each time to begin with like 90% of the work already done? If common behavior needs to change, pull. Or make it a common versioned dependency, which is probably a better thing to do.
It's an odd problem you have because it's not typically a problem for people.
doomslice@reddit
Are you using C(++) to implement the performance sensitive parts of your Python app (so Python calling directly to C when it counts) or are you completely rewriting? If the latter, why not a middle ground like Golang?
AnnoyedVelociraptor@reddit
How about you move it to Rust?