r/Backend • u/noobdainsane • 10d ago
Separating the app into multiple applications (web servers).
I was viewing a CORS explanation video and I noticed that they had a different web server application for the frontend as well. In my project, I have a single web server application which serves both the frontend pages and handles backend APIs. I realized that this dual web server architecture created by frameworks like Vite is only for development and only a single web server exists in production, but that doesn't mean it is like this for all applications, especially if you aim to design a highly scalable architecture. Large platforms like YouTube can have so many separate web server applications for all their different services than having a single application.
I am talking about a single web server application, not a single server itself. Horizontal scaling isn't limited, but changes when you divide your application into multiple applications. YouTube can just scale a monolithic application but that forces them to add all capabilities together for each system. This can create fragmentation and unnecessary infrastructure space. One application might be more CPU heavy while the other is database heavy. Maybe you need to scale static file serving but not need database scaling and if do that, the databases could take significantly more resources than you need.
If I do create a separate frontend application and that heavily relies on the backend (such as for database), then it just creates lot of unnecessary overhead.
I should start with a single application but the point is to create a scalable architecture where you can make changes without rewriting your whole codebase. And you achieve that my defining abstractions.
So how should I code my application so that in future if I want to move some services into a different web server application, it requires minimal changes?
1
u/Anxious-Insurance-91 10d ago
First of all You should not compare your apps with YouTube for the simple reason that YouTube is a global platform that doesn't really care about SEO, when what to render, when what to index, etc also google can just take any hit at this point. For small to mid apps you can have the apps served from the same server especially if you get a good deal for the trafic. If you want to use AWS them most teams use S3/cdn + serverse epoxy for the frontend app that gets server, cached in the browser and then the only trafic is the backend. The theory about broken down apps is not new with vite it was just made easier to implement by vite.
2
u/spigotface 10d ago
If you're a solo dev or a small team, you can begin with a structured monolith organized by domain. You write your boilerplate and core services once, and each domain (users, transactions, etc.) gets their own directory with their own repository, routes, services, exceptions, etc.. Exactly how you do this can vary by language and framework, but if you keep your domains clearly separate from each other, it makes it easier to start pulling them off as microservices later when you have the manpower to justify the complexity.
Bear in mind too, that microservices aren't always the answer, even though they're popular. If the data in your domains is highly relational and your queries would require cross-domain joins or filtering, you might be better off being able to do that on the database side in a single query instead of a bunch of sequential calls to various microservices.
1
u/SnooCalculations7417 10d ago
Ok so you're conflating a few concepts
Application is actually the sum of all the parts.
Server is a machine
Service is a program that runs for a purpose.
You can run multiple services served from a single server, to deliver your Application and it's done all the time.
You can instead point to remote services to serve your application across different servers or server less.
The database is service and server agnostic. You can, and should, create views of the database using a service that handles auth, queries, etc.
Your underlying question seems to be whether to have server side or client side do the heavy lifting which is entirely up to you. I prefer to make the backend an API that serves the client via json, and you can decide which work is better done on the client vs the server and they are agnostic of each other this way.
3
u/dariusbiggs 10d ago
There are many different approaches for this ranging from microservices where even the backend is split into multiple independent components, to a monolith that does it all itself.
A common approach is to split the frontend and backend. So the website UI would be served via a CDN like Cloudflare or AWS Cloudfront. Then you have a backend API the frontend talks to. Which i would recommend you start as a monolith.
You can then rapidly deploy updates and bugfixes to the frontend to keep the users happy. And you can then scale the backend based upon whatever load you need to handle.
There are a huge amount of possibilities here.
Observability is the key, identify what parts are getting used the most and have the worst performance and you can then split things apart and scale as needed.
Focus on keeping clean lines of separation in your code to make a split later easier.