What is Reverse Proxy in System Design: Simple Explanation
reverse proxy is a server that sits between clients and backend servers, forwarding client requests to the appropriate server and returning the server's response. It helps improve security, load balancing, and scalability by hiding backend details and managing traffic.How It Works
Imagine a receptionist at a busy office building. Instead of visitors going directly to different offices, they first meet the receptionist who then directs them to the right office. A reverse proxy acts like this receptionist for web traffic.
When a user sends a request, it first reaches the reverse proxy server. The proxy then decides which backend server should handle the request and forwards it there. After the backend server processes the request, the reverse proxy sends the response back to the user. This way, users never directly interact with the backend servers.
This setup helps hide the backend servers' details, balance the load among multiple servers, and add security layers like filtering or encryption.
Example
This example shows a simple reverse proxy using Node.js and the http-proxy library. It forwards incoming requests to a backend server running on port 9000.
import http from 'http'; import httpProxy from 'http-proxy'; // Create a proxy server const proxy = httpProxy.createProxyServer({}); // Create an HTTP server that listens on port 8000 const server = http.createServer((req, res) => { // Forward request to backend server at http://localhost:9000 proxy.web(req, res, { target: 'http://localhost:9000' }); }); server.listen(8000, () => { console.log('Reverse proxy server running on http://localhost:8000'); });
When to Use
Use a reverse proxy when you want to:
- Improve security: Hide backend servers from direct access and filter malicious requests.
- Balance load: Distribute incoming traffic evenly across multiple servers to avoid overload.
- Enable caching: Store responses temporarily to speed up repeated requests.
- Handle SSL termination: Manage encryption and decryption at the proxy instead of backend servers.
- Centralize logging and monitoring: Track all incoming requests in one place.
Real-world examples include large websites like Netflix or Amazon, which use reverse proxies to manage millions of user requests efficiently and securely.
Key Points
- A reverse proxy sits between clients and backend servers.
- It forwards client requests to the correct backend server.
- It improves security by hiding backend details.
- It helps balance load and improve scalability.
- It can handle SSL termination and caching.