What is a Backend, how do they work and why do we need them?
This video provides a comprehensive overview of how backend servers function, the flow of requests from browsers to servers, the role of DNS and firewalls, why backend logic cannot simply be implemented on the frontend, and the fundamental reasons for using backend in modern web applications.
1. What is a Backend Server and How Does It Work?
A backend server is fundamentally a computer that listens for network requests (HTTP, WebSocket, gRPC, etc.) on specified ports (commonly 80 for HTTP and 443 for HTTPS) to serve or receive data. It provides content such as static files (images, JS, HTML) or dynamic data like JSON.
- The server responds to client requests by either serving data or accepting input.
- The example uses a backend server deployed on an AWS EC2 instance.
- The server is accessed through a domain name managed by a DNS server, which resolves the domain to the server’s IP address.
- The request route:
- Browser sends a request to domain/subdomain.
- DNS server resolves the domain to EC2 instance’s public IP.
- The request passes through AWS security groups (firewalls) configured to allow specific ports (80, 443).
- The request arrives at the EC2 instance.
- Behind the EC2 server, a reverse proxy (Nginx) manages incoming traffic by forwarding requests to the appropriate local service, e.g., Node.js backend on port 3001.
- Nginx also handles redirecting HTTP traffic (port 80) to HTTPS (port 443), often managing SSL certificates automatically (example uses
sbotutility).
Summary of Server Request Flow:
- Process management on the server uses tools like PM2, which keeps backend and frontend processes running efficiently.
2. Why Do We Need Backend Servers?
Backend servers provide the central management of data and state. For example, on Instagram:
- When a user “likes” a post:
- The frontend sends a request to the backend.
- The backend identifies the user and updates the database with this like action.
- The backend then triggers other actions such as notifying the post’s owner.
This centralized processing is essential because:
- The backend holds data about all users, posts, and interactions.
- It persists this data securely in a database.
- It performs centralized authorization, validation, and business logic.
- Enables communication among users (e.g., notifications).
Core Backend Responsibilities:
- Fetching data.
- Receiving data from clients.
- Persisting data.
- Executing business logic.
- Managing centralized state and interactions.
3. Limitations of Implementing Backend Logic on the Frontend
A natural question is why not run backend code purely in the frontend since browsers execute code?
How Frontends Work:
- Frontend code (JavaScript, CSS, HTML) is served to the browser.
- The browser downloads and executes the code locally.
- The frontend acts as a runtime environment to render UI and handle user interactions.
- Browsers run code in sandboxed environments for security.
Key Limitations and Reasons for Separate Backend:
Why Backend Needs Native Environment:
- Ability to manage environment variables and server resources.
- Maintain efficient, persistent connections to databases.
- Handle thousands of simultaneous requests reliably.
- Perform heavy and sensitive business logic securely.
Why Browsers Enforce Restrictions:
- Prevent malicious scripts from accessing files or sensitive data on user machines.
- Enforce security boundaries between domains to avoid unauthorized data access.
4. How Frontend Applications Work Internally
Using a Next.js frontend example:
- The browser requests the main HTML document from the frontend domain.
- The server returns the HTML file.
- The browser then makes multiple requests for assets (JS, CSS, fonts).
- CSS files are processed to "paint" styles on the page.
- JavaScript files hydrate the HTML by attaching event listeners and enabling interactivity.
- All frontend logic executes client-side in the browser's sandbox.
Typical Frontend Request Flow:
In contrast to backend servers, frontend code runs in the client runtime, which drastically changes how and where processing happens.
5. Key Takeaways and Conclusion
- Backend servers act as centralized, secure, persistent environments for processing, storing, and managing data.
- Backend processing is necessary for security, performance, and functionality of modern web applications.
- Frontend environments are designed for presentation and user interaction, with strong security and sandbox boundaries.
- Browsers cannot replace backend due to security restrictions, networking policies, database access needs, and computing limitations.
- Understanding the distinct roles and flows of backend and frontend is fundamental before diving into backend development.
This foundational knowledge sets the stage for effective backend engineering by clarifying why backend services exist, how requests flow over the Internet, and why backend logic cannot simply be mirrored in frontend code.