# FM-KED-007 — 502 Bad Gateway Error (Application Unreachable)

**Severity:**<span style="white-space: pre-wrap;"> S1 — Critical</span>  
**Recovery Class:**<span style="white-space: pre-wrap;"> B — Standard Recovery</span>  
**Covered by Monthly Support:**<span style="white-space: pre-wrap;"> Yes (known causes only)</span>

---

### Description

<span style="white-space: pre-wrap;">Nginx returns a </span>**502 Bad Gateway**<span style="white-space: pre-wrap;"> error because the Django application backend becomes unreachable.</span>

<span style="white-space: pre-wrap;">In the majority of observed cases, this is caused by </span>**Out Of Memory (OOM)**<span style="white-space: pre-wrap;"> conditions on the virtual machine hosting the application worker, leading to termination of Gunicorn or equivalent application processes.</span>

---

### Typical Symptoms

- HTTP 502 responses from Nginx
- Application intermittently unavailable
- Gunicorn workers restarting or disappearing
- Kernel logs indicating OOM events

---

### Primary Root Cause

- Application requests producing excessive memory usage
- Large datasets loaded into memory
- Insufficient RAM on the worker virtual machine

When memory limits are exceeded, the operating system terminates the application process, leaving Nginx without a valid upstream.

---

### Diagnostic Checklist

#### Confirm OOM Condition

```bash
dmesg | grep -i oom
journalctl -k | grep -i kill
```

#### Check Available Memory

```bash
free -h
```

#### Verify Application Server Status

```bash
systemctl status gunicorn
```

---

### Recovery Options

Apply one or more of the following, depending on constraints.

---

#### Option 1: Reduce Request Scope

- Apply stricter filters to API requests
- Limit requested date ranges
- Reduce number of portfolios, instruments, or entities per request
- Avoid bulk data retrieval in a single call

This reduces memory pressure at the application level.

---

#### Option 2: Increase RAM on Worker Virtual Machine

- Increase memory allocation on the worker VM
- Restart application services after resizing
- Verify stability under previous load

This addresses the issue at the infrastructure level.

---

### Escalation and Unknown Issues

If the issue persists after:

- request scope reduction, and
- sufficient memory allocation

<span style="white-space: pre-wrap;">then the incident is classified as an </span>**unknown issue**.

<span style="white-space: pre-wrap;">Such cases require investigation, profiling, or architectural analysis and are </span>**not covered**<span style="white-space: pre-wrap;"> by the standard monthly support allocation.</span>

---

### Preventive Notes

- Avoid unbounded API queries
- Monitor memory usage trends
- Define safe defaults and limits at API level
- Prefer asynchronous processing for heavy workloads

---

### Responsibility Boundary

Finmars SCSA provides best-effort diagnostics and guidance for known memory-related causes.  
Application design decisions and infrastructure capacity planning beyond documented scenarios require separate analysis.

---