# 🚀 DOING: Architecture & Bottleneck Fixes

## 📌 Context
- **Idea:** Backend Architecture Audit (2026-04-29)
- **Status:** ✅ Done

## 🎯 Goal
Fix the 7 identified bottlenecks and critical issues in the FastAPI backend to ensure production-grade stability, prevent deadlocks, and future-proof the application.

## ⚠️ Active Trade-offs
- None. These are strict improvements without functional regressions.

## 🛠 Impact & Files Touched
| File | Symbol | Change | Blast Radius |
|------|--------|--------|--------------|
| `config.py` | `LANGFUSE_PUBLIC_KEY`, `LANGFUSE_SECRET_KEY` | DELETE | Low (Duplicate block removed) |
| `server.py` | `startup_event`, `shutdown_event` | MODIFY | Medium (App lifecycle updated to `lifespan`) |
| `starrocks_connection.py` | `_pool_lock`, `get_pool` | MODIFY | Medium (Thread/Async safety for DB pool) |
| `pool_wrapper.py` | `get_pooled_connection_compat` | MODIFY | Low (Added backoff to retry) |

**Risk:** Medium (Touching startup lifecycle and DB connections. Will verify via server restart).

## 📋 Execution Checklist

### Phase 1: P0 Critical Fixes (~10m)
- [x] Task 1.1: Fix duplicate LANGFUSE keys in `config.py`.
- [x] Task 1.2: Migrate `@app.on_event` to `lifespan` in `server.py` (Includes moving `start_publish_engine`).
- [x] Task 1.3: Fix `asyncio.Lock()` initialization in `starrocks_connection.py` to be inside the async context.

### Phase 2: P1 Performance Fixes (~5m)
- [x] Task 2.1: Add exponential backoff to connection retries in `pool_wrapper.py`.
- [x] Task 2.2: Reduce `STARROCKS_POOL_MAXSIZE` default from 80 to 20 in `starrocks_connection.py`.

### Phase 3: Cleanup (~5m)
- [x] Task 3.1: Remove unused root-level legacy databases (`123.db`).

## 🔄 Sub-Doings
_None yet_

## ✅ Completion Gate
- [x] All tasks [x] | [x] Sub-Doings resolved | [x] Smoke test passed | [x] Trade-offs confirmed

---
_Started: 2026-04-29 | Updated: 2026-04-29_
