Skip to content

[Bug]: Lock Contention in EpisodicMemoryManager #908

@sscargal

Description

@sscargal

Describe the bug

During load testing in #837, profiling shows heavy lock contention in EpisodicMemoryManager and low requests-per-second in locust, even with a low number of users. The problem is worse as we add more users and load.

Image

Image
Image
Image
Image

Steps to reproduce

Use the locustfile.py and profile_memmachine.sh from #837 to generate the load and profile the FastAPI processes running inside the memmachine-app Docker container.

Expected behavior

  • Remove serialization locks in high contention code paths.
  • Expect higher Requests per Second per FastAPI worker with Locust.

Environment

  • OS: Ubuntu Linux 25.04
  • MemMachine: v0.2.2
  • Locust load generator

Additional context

The open_or_create_episodic_memory method currently holds a global lock while performing database I/O to fetch session info. This serializes all requests to that worker.

Suggested Solution: Use a double-checked locking pattern or a per-session lock to allow concurrent I/O for different sessions.

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions