# nitinpratap22061/reverseproxy-server architecture

> Architecture diagram of the GitHub repository nitinpratap22061/reverseproxy-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nitinpratap22061/reverseproxy-server
- Repository: https://github.com/nitinpratap22061/reverseproxy-server

## Overview

##### Project Type and Purpose
- This is a middleware application acting as a reverse proxy. It intercepts incoming client requests, forwards them to one of the configured backend servers (backend1.js and backend2.js), caches responses for performance, and handles errors such as when a backend server is unavailable (returning a 502 error).

##### Key Components and Their Roles
- Proxy Server (server.js): Acts as the central gateway listening on port 8000. It handles incoming HTTP requests, applies load balancing, manages caching using NodeCache, and forwards requests to backend servers.
- Backend Servers (backend1.js and backend2.js): These represent the service instances that actually process the forwarded requests. They are run on different ports (as defined in the .env file).
- Load Balancing Component: Implemented within the proxy server using a round-robin algorithm. It decides which backend to forward each request.
- Caching Component: Utilizes NodeCache to store responses temporarily, reducing the load on backend servers and speeding up client responses.
- Environment Configuration: Uses environment variables (as mentioned in the README) for dynamic configuration (e.g., backend hosts/ports, cache TTL).

##### Relationships and Interactions
- The client initiates a request to the proxy server.
- The proxy server examines the request and consults its load balancing logic to select either backend1.js or backend2.js.
- The proxy then checks if a cached response exists:
- If yes, it returns the cached response.
- If not, it forwards the request to the selected backend server.
- The backend processes the request and sends the response back. The proxy caches this response before returning it to the client.
- Error handling is applied; if a selected backend server is unreachable, the proxy returns a “502 Bad Gateway” error.

##### Architectural Patterns and Design Principles
- Reverse Proxy Pattern: Central server forwards client requests to appropriate backend services.
- Load Balancer: Round-robin algorithm implemented within the proxy.
- Caching Mechanism: NodeCache is used to reduce redundancy and improve performance.
- Separation of Concerns: The proxy’s responsibilities (routing, caching, error handling) are distinct from the business logic in the backend servers.

## Diagram (Mermaid)

```mermaid
flowchart TD 
    Client("Client"):::client
    ReverseProxy("Reverse Proxy Server (Express.js, http-proxy, NodeCache)"):::proxy
    Backend1("Backend Server 1 (backend1.js)"):::backend
    Backend2("Backend Server 2 (backend2.js)"):::backend

    Client -->|"HTTP Request"| ReverseProxy
    ReverseProxy -->|"RoundRobin_Select"| Backend1
    ReverseProxy -->|"RoundRobin_Select"| Backend2
    Backend1 -->|"Response_with_Cache(NodeCache)"| ReverseProxy
    Backend2 -->|"Response_with_Cache(NodeCache)"| ReverseProxy
    ReverseProxy -->|"HTTP Response"| Client
    ReverseProxy -.->|"502 Bad Gateway"| Client

    click ReverseProxy "https://github.com/nitinpratap22061/reverseproxy-server/blob/main/server.js"
    click Backend1 "https://github.com/nitinpratap22061/reverseproxy-server/blob/main/backend1.js"
    click Backend2 "https://github.com/nitinpratap22061/reverseproxy-server/blob/main/backend2.js"

    classDef client fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef proxy fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef backend fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
