3. What is a Backend, how do they work and why do we need them?
Understanding the Backend: A Comprehensive Overview
What is a Backend Server?
- The backend server is defined as a computer that listens for various types of requests (HTTP, WebSocket, gRPC) through an open port accessible over the Internet.
- It serves content such as static files (images, JavaScript, HTML) or JSON and can also accept data from clients.
Request Flow to the Server
- The example used involves a backend server deployed on AWS, illustrating how requests are processed from a browser to the server.
- The request flow begins with the domain name resolution via DNS servers, which translates domain names into IP addresses.
DNS Records and Their Role
- DNS has different types of records; 'A' records point to specific IP addresses while 'CNAME' records link to domain names or subdomains.
- In this case, two 'A' records exist for the subdomain "backend demo," pointing to an EC2 instance's public IP address in AWS.
Security Measures: Firewalls and Security Groups
- Before reaching the server, requests pass through AWS's native firewall which must allow certain ports (e.g., 443 for HTTPS).
- Security groups define which ports are accessible over the internet; without proper configuration, requests may be blocked.
Reverse Proxy Configuration with Nginx
- A reverse proxy like Nginx sits in front of other servers to manage redirects and configurations centrally.
- Nginx listens on Port 80 and redirects traffic to Port 443 for secure HTTPS connections using SSL certificates managed by Let's Encrypt (via sbot).
Final Steps in Request Processing
- Requests routed through Nginx are forwarded to a local Node.js server running on localhost at port 3001.
- The entire process includes multiple hops: from browser → DNS → AWS instance → firewall → Nginx → final Node.js server.
Summary of Request Journey
- To summarize: requests originate in the browser, go through DNS resolution, reach AWS servers via firewalls, get processed by Nginx, and finally hit the local Node.js application.
Understanding the Role of Backends in Web Applications
The Importance of Backends
- The backend is crucial for processing requests and managing data flow between users and servers, as illustrated by a user liking a post on Instagram.
- When a user clicks the like button, a request is sent to the server, which identifies the user and saves their action in a database.
- The server also determines whose post was liked and sends a notification to that user, showcasing how backends facilitate real-time interactions.
- A centralized server holds information about all users, allowing personalized experiences based on individual profiles and actions.
- In essence, the backend's primary responsibility revolves around data management—fetching, receiving, and persisting data.
Frontend vs. Backend Functionality
- Questions arise regarding why not perform all operations on the frontend instead of relying on backends for functionalities.
- To understand this distinction, it's essential to explore how frontends operate behind the scenes through an example using Next.js applications.
How Frontend Works
- Upon refreshing a webpage, the browser fetches an HTML document from a specified domain name along with various resources (JavaScript files, images).
- Each resource is fetched through separate requests; this process involves communication with an EC2 instance hosting the application.
- The configuration allows traffic through specific ports (443 for HTTPS and 80 for HTTP), ensuring proper routing to the frontend server.
Rendering Process in Browsers
- Once requests reach the frontend server at port 3000, it serves necessary files (JS/CSS/HTML), which are then processed by the browser.
- After receiving these files, browsers execute JavaScript code that hydrates event listeners for interactive elements like buttons.
Key Differences Between Frontend and Backend Processing
- Unlike traditional backends where processing occurs on servers (e.g., AWS EC2), frontends rely on browsers as runtime environments executing client-side logic.
- This shift means that while servers handle data management tasks, browsers manage execution of code directly affecting user interaction.
Limitations of Browser Environments
- Browsers operate within sandboxed environments that isolate them from operating systems; this can limit certain functionalities compared to traditional backends.
Understanding Browser Security Policies and Backend Logic
The Isolated Environment of Browsers
- Browsers operate in an isolated environment, limiting code access to resources like the Document Object Model (DOM), browser APIs (e.g., local storage, cookies), and external APIs with proper headers.
- This isolation acts as a security policy that restricts JavaScript from calling external APIs not associated with the current domain, preventing unauthorized data access.
Security Implications of Code Execution
- The sandboxing and security restrictions are crucial because browsers execute remote code, which could potentially access sensitive user data if not properly isolated.
- Without these policies, malicious code could easily compromise user files and sensitive information by accessing the file system directly.
Reasons Against Writing Backend Logic in Frontend
1. Security Restrictions
- One primary reason for not writing backend logic in frontend is due to strict browser security policies that prevent direct file system access or manipulation.
2. API Access Limitations
- Frontend applications cannot freely call external APIs unless they have appropriate CORS headers; this limitation hinders backend server functionality which often requires multiple data sources.
3. Database Connectivity Issues
- Backend servers utilize native database drivers (e.g., PG for PostgreSQL, MongoDB) that allow efficient communication with databases—something browsers cannot do due to their inability to maintain persistent connections.
4. Connection Management Challenges
- Backend servers manage connection pools to handle numerous requests efficiently without overwhelming the database server—a capability lacking in browser environments.
5. Computing Power Constraints
- Frontend applications run on various devices with differing computing capabilities; heavy business logic can lead to performance issues on less powerful devices compared to centralized backend servers that can scale resources as needed.
Conclusion: The Necessity of a Backend Server
- Understanding these limitations highlights why backend logic should remain separate from frontend applications, ensuring both security and efficiency as we delve deeper into backend functionalities.