keploy icon indicating copy to clipboard operation
keploy copied to clipboard

replay: HTTP, HTTP/2 and MySQL data mocks stay in the agent's RAM for the whole run, so a long recording can OOM the replay agent

Open Aditya-eddy opened this issue 1 month ago • 2 comments

What happens

With default settings, the replay agent keeps every HTTP, HTTP/2 and MySQL data mock in RAM for the whole replay. Only per-test mocks go to the agent's on-disk store (DiskMocks, #4350), and with the defaults these kinds' data mocks aren't per-test. For an app whose dependencies are mostly HTTP or MySQL, the agent's resident memory therefore grows with the length of the recording. A long recording can OOM-kill the replay agent before its app starts, the same failure a Mongo-backed recording hit through the staging load.

Why

  1. What the recorders write. MySQL tags data commands mocks (pkg/agent/proxy/integrations/mysql/recorder/record_v2.go:1865; COM_STMT_PREPARE gets connection). HTTP tags exchanges HTTP_CLIENT, and HTTP/2 tags them HTTP2_CLIENT.
  2. How that becomes a session mock. DeriveLifetime (pkg/models/lifetime.go:296-305) makes any non-config tag on a kind in kindsWithImplicitSessionLifetime (:435-441: HTTP, HTTP2, MySQL, Postgres, PostgresV2, Generic, DNS) a LifetimeSession. The only exception is when KEPLOY_STRICT_MOCK_WINDOW is set to an enabling value. The comment says this preserves the pre-Phase-2 implicit reusability.
  3. Session mocks are always resident. They travel in the stream's unfiltered section, which StoreMocksStream (pkg/service/agent/agent.go) always keeps in RAM. Only per-test mocks with valid timestamps can go to disk (EligibleForDisk, pkg/agent/proxy/diskmocks.go:78).
  4. Result sets are never stored apart. Even as per-test mocks, MySQL result sets would not be kept apart from their request: EligibleForResponseSpill (diskmocks.go:92) covers HTTP bodies of 8 KB or more and Mongo only.

The lifetime is derived by the process that reads the mock files, which is the replayer (the k8s-proxy controller for auto-replay). It then travels to the agent with each mock. In the record-prod deployment neither the controller nor the replay pods set KEPLOY_STRICT_MOCK_WINDOW (checked 2026-10-15), so this fallback is live there.

Where it was seen

Found by reading the code during the RCA of the record-prod auto-replay OOM. That recording is Mongo-backed, so this path wasn't the cause there. In its last round, 2,230 mocks stayed resident and 81,738 per-test mocks went to disk. No HTTP- or MySQL-heavy recording has been measured yet, so how fast the agent's memory grows for those is still open.

Possible fixes

  1. Set KEPLOY_STRICT_MOCK_WINDOW=1 where the replayer runs. HTTP, HTTP/2 and MySQL data mocks then become per-test and go to disk. Before doing that:
    • MySQL data mocks stop being reusable across tests.
    • The HTTP/2 matcher reads only GetSessionMocks (integrations pkg/http2/match.go:43), and the SQS matcher reads only the session tree. Their data mocks would become unreachable, so those matchers have to read the per-test tier first.
  2. Take MySQL, and possibly HTTP and HTTP/2, out of the lax fallback for explicitly tagged data mocks. Keep reuse only for input-independent commands, as COM_PING, COM_STATISTICS, COM_DEBUG and COM_RESET_CONNECTION already are.
  3. Store MySQL result sets apart from their requests, as Mongo responses are, so a loaded window holds only requests until a mock is served.
  4. Index the on-disk store by request shape (kind plus command or method and path), so reusable mocks can live on disk and be read on demand.

Any of these needs a run of the MySQL, HTTP/2 and SQS sample lanes before it ships, plus a measurement of agent memory on a long MySQL- or HTTP-heavy recording.

Aditya-eddy avatar Oct 15 '26 12:10 Aditya-eddy

/claim

LogicVortex123 avatar Nov 16 '26 14:11 LogicVortex123

@Aditya-eddy is this issue open for a PR

LogicVortex123 avatar Nov 16 '26 14:11 LogicVortex123