Skip to content

[Feat]: Asynchronous write-back for hybrid memory sync #432

Description

@eahishak

Is your feature request related to a problem?

Write operations currently block while persisting to durable store. This slows down ingestion and increases API latency.

Describe the solution you'd like

Perform multiple memory insertions under load — throughput decreases linearly as concurrent requests increase.

Describe alternatives you've considered

Write-backs should be asynchronous with retry logic, reducing blocking time while maintaining consistency.

Additional context

Adding a background write queue or asyncio task group can cut write latency by ~25%.

Activity

  1. added theissue type on Nov 2, 2025
  2. added
    keep-openPrevents the auto-close task from closing this issue.
    on Apr 22, 2026
  3. sscargal commented on Apr 22, 2026

    @sscargal
    Contributor

    MemMachine has made extensive use of async operations.

    Here's my analysis of this feature request against the current codebase relative to this issue.

    ✅ Already addressed (partially)

    Request Where it's implemented
    Async write methods add_episodes in memmachine.py uses asyncio.gather(*tasks) to fan out to episodic + semantic memory concurrently
    Background worker / asyncio task group ShortTermMemoryConsolidator.summarize() is explicitly non-blocking — it buffers episodes and runs a background asyncio.create_task loop
    Background ingestion with retry/backoff SemanticService._background_ingestion_task() runs as a background loop with exponential backoff up to 60s
    Async R/W locking AsyncRWLock in rw_locks.py provides full writer-priority async locking with context managers

    ⚠️ Not yet fully addressed

    The core concern — write operations blocking the API response until durable persistence completes — is still present. In memmachine.py::add_episodes:

    await asyncio.gather(*tasks)
    return episode_ids

    Writes to episodic and semantic storage are concurrent with each other (via asyncio.gather), but the API handler still awaits full completion before returning. The issue's request for a "background write queue" at the ingestion layer — where the API acknowledges receipt and returns immediately while writes are drained asynchronously — is not implemented in the hot path.

    The ShortTermMemoryConsolidator achieves this pattern for summarization, and SemanticService does it for background ingestion polling, but neither covers the primary durable-store write path.

    Conclusion

    The existing async infrastructure (locks, task groups, background workers) is excellent groundwork. What remains is a targeted enhancement: introducing a write-back queue in the add_episodes hot path so the REST handler can return episode IDs immediately and defer persistence. The issue's suggested ~25% latency improvement is a reasonable expectation given the background patterns already proven in ShortTermMemoryConsolidator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    keep-openPrevents the auto-close task from closing this issue.performanceIssues relating to MemMachine performance

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions