--- issue: 072 title: "Backend: chat messages stored as embedded array — unbounded document growth, 16MB ceiling" severity: high domain: Chat labels: [performance, backend, data-model] status: open created: 2026-05-30 source: Full Codebase Audit 2026-05-30 --- # Backend: chat messages stored as embedded array — unbounded document growth, 16MB ceiling **Severity:** high **Domain:** Chat **Labels:** performance, backend, data-model ## Description `backend/src/models/Chat.ts:173` stores all messages as an embedded array in the Chat document. MongoDB's 16MB document size limit will be hit for active long-running chats. Reads also load the full message history into memory even when only the latest page is needed. ## Options 1. Migrate messages to a `Messages` collection keyed by `chatId` with pagination. 2. Cap embedded messages and archive older ones. 3. Keep embedded but project only needed messages (slice) on reads. ## Recommendation Plan migration to a dedicated `Messages` collection. This is a large data-model migration that needs careful coordination. ## Affected Files - `backend/src/models/Chat.ts:173` - `backend/src/services/chat/ChatService.ts` — all read paths ## References - [Full Codebase Audit 2026-05-30](../09%20-%20Audits/Full%20Codebase%20Audit%20-%202026-05-30.md) — DEC-42