This tool migrates LightRAG's LLM response cache between different KV storage implementations. It specifically migrates caches generated during file extraction (mode default), including entity extraction and summary caches.
The tool migrates the following cache types:
default:extract:* - Entity and relationship extraction cachesdefault:summary:* - Entity and relationship summary cachesNote: Query caches (modes like mix,local, global, etc.) are NOT migrated.
The LLM Cache Migration Tool reads the storage configuration of the LightRAG Server and provides an LLM migration option to select source and destination storage. Ensure that both the source and destination storage have been correctly configured and are accessible via the LightRAG Server before cache migration.
Run from the LightRAG project root directory:
python -m lightrag.tools.migrate_llm_cache
# or
python lightrag/tools/migrate_llm_cache.py
The tool guides you through the following steps:
Supported KV Storage Types: [1] JsonKVStorage [2] RedisKVStorage [3] PGKVStorage [4] MongoKVStorage Select Source storage type (1-4) (Press Enter to exit): 1
Note: You can press Enter or type 0 at any storage selection prompt to exit gracefully.
The tool will:
Checking environment variables... ✓ All required environment variables are set Initializing Source storage... - Storage Type: JsonKVStorage - Workspace: space1 - Connection Status: ✓ Success Counting cache records... - Total: 8,734 records
Progress Display by Storage Type:
Counting cache records... - Total: 8,734 records
Scanning Redis keys... found 8,734 records
Counting PostgreSQL records... (took 2.3s)
Counting MongoDB documents... (took 1.8s)
The tool automatically excludes the source storage type from the target selection and renumbers the remaining options sequentially:
Available Storage Types for Target (source: JsonKVStorage excluded): [1] RedisKVStorage [2] PGKVStorage [3] MongoKVStorage Select Target storage type (1-3) (Press Enter or 0 to exit): 1
Important Notes:
0 to exit at this point as wellThe tool then validates the target storage following the same process as the source (checking environment variables, initializing connection, counting records).
================================================== Migration Confirmation Source: JsonKVStorage (workspace: space1) - 8,734 records Target: MongoKVStorage (workspace: space1) - 0 records Batch Size: 1,000 records/batch Memory Mode: Streaming (memory-optimized) ⚠️ Warning: Target storage already has 0 records Migration will overwrite records with the same keys Continue? (y/n): y
The tool uses streaming migration by default for memory efficiency. Observe migration progress:
=== Starting Streaming Migration === 💡 Memory-optimized mode: Processing 1,000 records at a time Batch 1/9: ████████░░░░░░░░░░░░ 1000/8734 (11.4%) - default:extract ✓ Batch 2/9: ████████████░░░░░░░░ 2000/8734 (22.9%) - default:extract ✓ ... Batch 9/9: ████████████████████ 8734/8734 (100.0%) - default:summary ✓ Persisting data to disk... ✓ Data persisted successfully
Key Features:
The tool provides a comprehensive final report showing statistics and any errors encountered:
Successful Migration:
Migration Complete - Final Report 📊 Statistics: Total source records: 8,734 Total batches: 9 Successful batches: 9 Failed batches: 0 Successfully migrated: 8,734 Failed to migrate: 0 Success rate: 100.00% ✓ SUCCESS: All records migrated successfully!
Migration with Errors:
Migration Complete - Final Report 📊 Statistics: Total source records: 8,734 Total batches: 9 Successful batches: 8 Failed batches: 1 Successfully migrated: 7,734 Failed to migrate: 1,000 Success rate: 88.55% ⚠️ Errors encountered: 1 Error Details: ------------------------------------------------------------ Error Summary: - ConnectionError: 1 occurrence(s) First 5 errors: 1. Batch 2 Type: ConnectionError Message: Connection timeout after 30s Records lost: 1,000 ⚠️ WARNING: Migration completed with errors! Please review the error details above.
The tool retrieves workspace in the following priority order:
Storage-specific workspace environment variables
POSTGRES_WORKSPACEMONGODB_WORKSPACEREDIS_WORKSPACEGeneric workspace environment variable
WORKSPACEDefault value
For large datasets, the tool implements storage-specific pagination strategies:
This ensures the tool can handle millions of cache records without memory issues.
The tool uses optimized filtering methods for different storage types:
_id field with cursor streamingThe tool implements comprehensive error tracking to ensure transparent and resilient migrations:
After migration completes, a detailed report includes:
The tool supports multiple configuration methods with the following priority:
Configure storage settings in your .env file:
# Generic workspace (shared by all storages)
WORKSPACE=space1
# Or configure independent workspace for specific storage
POSTGRES_WORKSPACE=pg_space
MONGODB_WORKSPACE=mongo_space
REDIS_WORKSPACE=redis_space
Workspace Priority: Storage-specific > Generic WORKSPACE > Empty string
WORKING_DIR=./rag_storage
REDIS_URI=redis://localhost:6379
POSTGRES_HOST=localhost POSTGRES_PORT=5432 POSTGRES_USER=your_username POSTGRES_PASSWORD=your_password POSTGRES_DATABASE=your_database
MONGO_URI=mongodb://root:root@localhost:27017/ MONGO_DATABASE=LightRAG
Alternatively, create a config.ini file in the project root:
[redis]
uri = redis://localhost:6379
[postgres]
host = localhost
port = 5432
user = postgres
password = yourpassword
database = lightrag
[mongodb]
uri = mongodb://root:root@localhost:27017/
database = LightRAG
Note: Environment variables take precedence over config.ini settings. JsonKVStorage uses WORKING_DIR environment variable or defaults to ./rag_storage.
✗ Missing required environment variables: POSTGRES_USER, POSTGRES_PASSWORD
Solution: Add missing variables to your .env file
✗ Initialization failed: Connection refused
Solutions:
Solutions:
Use case: Migrating from single-machine development to production
# 1. Configure environment variables
WORKSPACE=production
MONGO_URI=mongodb://user:pass@prod-server:27017/
MONGO_DATABASE=LightRAG
# 2. Run tool
python -m lightrag.tools.migrate_llm_cache
# 3. Select: 1 (JsonKVStorage) -> 1 (MongoKVStorage - renumbered from 4)
Note: After selecting JsonKVStorage as source, MongoKVStorage will be shown as option [1] in the target selection since options are renumbered after excluding the source.
Use case: Migrating from cache storage to relational database
# 1. Ensure both databases are accessible
REDIS_URI=redis://old-redis:6379
POSTGRES_HOST=new-postgres-server
# ... Other PostgreSQL configs
# 2. Run tool
python -m lightrag.tools.migrate_llm_cache
# 3. Select: 2 (RedisKVStorage) -> 2 (PGKVStorage - renumbered from 3)
Note: After selecting RedisKVStorage as source, PGKVStorage will be shown as option [2] in the target selection.
Use case: Migrating data between different workspace environments
# Configure separate workspaces for source and target
POSTGRES_WORKSPACE=dev_workspace # For development environment
MONGODB_WORKSPACE=prod_workspace # For production environment
# Run tool
python -m lightrag.tools.migrate_llm_cache
# Select: 3 (PGKVStorage with dev_workspace) -> 3 (MongoKVStorage with prod_workspace)
Note: This allows you to migrate between different logical data partitions while changing storage backends.
default:extract:* and default:summary:*Backup Before Migration
Verify Results
Monitor Performance
Clean Old Data