# Performance Troubleshooting {#performance-troubleshooting}


Diagnose and fix performance issues.


## Slow API Responses {#slow-api-responses}



### Database Queries {#database-queries}


**Symptom:** Endpoints take > 1 second

**Diagnosis:**

```sql
-- Find slow queries in PostgreSQL
SELECT pid, now() - pg_stat_activity.query_start AS duration, query
FROM pg_stat_activity
WHERE state = 'active'
ORDER BY duration DESC;
```

**Fixes:**

1. Add database indexes for frequently queried columns
2. Reduce `relations` in API queries
3. Use pagination (`page` + `limit` params)
4. Enable query caching with Redis


### N+1 Query Problem {#n1-query-problem}


**Symptom:** Many small queries instead of one JOIN

**Fix:** Use eager loading for relations:

```typescript
const result = await this.service.findAll({
  relations: ["user", "organization"],
});
```


## High Memory Usage {#high-memory-usage}


**Symptom:** API process exceeding memory limits

**Fixes:**

1. Increase `NODE_OPTIONS="--max-old-space-size=4096"`
2. Check for memory leaks (unclosed connections, large caches)
3. Use streaming for large data exports


## Redis Performance {#redis-performance}


**Diagnosis:**

```bash
redis-cli INFO memory
redis-cli INFO stats
redis-cli SLOWLOG GET 10
```

**Fixes:**

1. Set appropriate `maxmemory` and eviction policy
2. Monitor key expiration
3. Use Redis Cluster for large datasets


## Frontend Performance {#frontend-performance}


**Fixes:**

1. Enable lazy loading for feature modules
2. Use `OnPush` change detection strategy
3. Implement virtual scrolling for long lists
4. Optimize images and assets


## Related Pages {#related-pages}


- [Monitoring & Observability](https://sourcey-gauzy.pages.dev/devops/monitoring/index.md) — monitoring setup
- [Scaling & HA](https://sourcey-gauzy.pages.dev/devops/scaling/index.md) — scaling guide
- [Redis & Caching](https://sourcey-gauzy.pages.dev/advanced/redis-and-caching/index.md) — caching patterns
