Memory Database (Default)
File: memory.py
Purpose: In-memory storage for testing and development
Implementation Details
class MemoryDatabaseService(DatabaseService):
def __init__(self):
self._data: Dict[str, Dict[str, Dict[str, Any]]] = {}
# Structure: {db_name: {doc_id: document}}
Characteristics
- Storage: Python dictionary in RAM
- Persistence: None (data lost on restart)
- Performance: Extremely fast (no I/O)
- Revision Tracking: Returns placeholder
"memory-rev" - Auto-initialization: No setup required
When to Use
- Unit testing
- Local development without external dependencies
- CI/CD pipelines
- Prototype/demo environments
Configuration
# .env or environment variables
DATABASE_PROVIDER=memory
# No additional configuration needed
Special Considerations
- No persistence across restarts
- Single-process only (no shared state)
- No concurrency control
- Unlimited storage (until RAM exhausted)
Example Usage
from services.database_service import get_database_service
from config import Settings
settings = Settings(DATABASE_PROVIDER="memory")
db = get_database_service(settings)
# Save a document
db.save("agents", "agent-1", {"name": "Test Agent"})
# Retrieve it
agent = db.get("agents", "agent-1")
print(agent) # {"_id": "agent-1", "name": "Test Agent"}
# List all agents
all_agents = db.list_all("agents")
# Delete the agent
db.delete("agents", "agent-1")
Testing
Memory database is the default for all unit tests:
import pytest
from services.database_service.memory import MemoryDatabaseService
@pytest.fixture
def db_service():
"""Create a fresh memory database for each test."""
return MemoryDatabaseService()
def test_save_and_get(db_service):
doc = {"name": "Test Agent"}
db_service.save("agents", "test-id", doc)
retrieved = db_service.get("agents", "test-id")
assert retrieved["name"] == "Test Agent"
Advantages
- Zero Configuration: Works out of the box
- Fast: No network or disk I/O overhead
- Isolation: Each instance is independent
- Predictable: No external dependencies to fail
- Clean State: Easy to reset between tests
Limitations
- No Persistence: Data lost on application restart
- Memory Bound: Limited by available RAM
- Single Process: Cannot share state between processes
- No Transactions: No ACID guarantees
- No Concurrency: Not thread-safe without additional locking
Production Use
NOT RECOMMENDED for production environments. Use a persistent database like:
Related Documentation
- Configuration - Database provider configuration
- Schema - Collection and document schemas
- Testing - Testing strategies
- Database Service README - Overview
Last Updated: 2026-01-11