Principle:LMCache LMCache Engine Lifecycle Management
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Resource_Management |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
A singleton lifecycle pattern that manages creation, reuse, and destruction of cache engine instances to prevent resource leaks.
Description
Engine Lifecycle Management ensures that LMCache engine instances are properly created, tracked, and destroyed. The LMCacheEngineBuilder uses a singleton-per-ID pattern: engines are created via get_or_create and identified by an instance_id string (typically "lmcache"). Multiple calls with the same ID return the existing instance. The destroy method handles orderly shutdown: stopping the stats logger, closing the engine (which releases storage backends, GPU connectors, and transfer channels), cleaning up instance dictionaries, and destroying the stats monitor.
Usage
Use this principle when the serving engine shuts down or when switching between configurations. The destroy method must be called to prevent resource leaks (pinned memory, open file handles, ZMQ sockets, NIXL channels).
Theoretical Basis
The lifecycle follows the create-use-destroy pattern with singleton semantics:
# Pseudocode
engine = LMCacheEngineBuilder.get_or_create(id, config, metadata, gpu_connector)
# ... use engine for store/retrieve ...
LMCacheEngineBuilder.destroy(id) # Releases all resources
Resource cleanup order:
- Stats logger shutdown
- Engine close (storage backends, GPU connectors)
- Instance dictionary cleanup
- Stats monitor destruction