generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 14
feat(database): Add unified MongoDB and PostgreSQL dual-write mechanism #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f232177
feat: implement dual-write system for MongoDB to PostgreSQL synchroni…
iamitprakash 79d10ce
Merge branch 'develop' into pg-migration
AnujChhikara fc5f38a
feat: enhance PostgreSQL integration with new models and dual-write f…
AnujChhikara 34de92d
refactor: consolidate PostgreSQL models
AnujChhikara 54b6001
feat: implement priority field fix and enhance dual-write functionality
AnujChhikara 73b9c4a
refactor: update PostgreSQL models and enhance dual-write service fun…
AnujChhikara ab26619
refactor: simplify Postgres watchlist model and update dual-write ser…
AnujChhikara b80bf3a
fix: deferred task in postgres
AnujChhikara b157cfc
refactor: update Postgres audit log model
AnujChhikara b7cfd53
refactor: remove old watchlist models and update user role structure …
AnujChhikara e6c37e9
refactor: enhance PostgreSQL model definitions and integrate dual-wri…
AnujChhikara 7219ec5
feat: add task assignment creation functionality and streamline task …
AnujChhikara aba75f3
refactor: update task assignment models and repository to streamline …
AnujChhikara 58786ae
feat: implement dual-write synchronization for team creation invite c…
AnujChhikara 017f5df
feat: add PostgreSQL synchronization service and management command f…
AnujChhikara 01caf14
chore: remove pgAdmin service from docker-compose configuration
AnujChhikara fed055c
chore: remove deprecated docker-compose and environment configuration…
AnujChhikara c3c0398
feat: add PostgreSQL availability checks and environment variables fo…
AnujChhikara c3bb706
feat: configure database settings for testing and production environm…
AnujChhikara 8dcd5b6
Merge branch 'pg-migration' of https://github.com/Real-Dev-Squad/todo…
AnujChhikara d185900
fix: update environment variable names for PostgreSQL configuration i…
AnujChhikara 541cba6
Merge branch 'develop' into pg-migration
AnujChhikara facfdbc
refactor: update PostgreSQL environment variable name and enhance doc…
AnujChhikara b675dac
refactor: rename PostgreSQL task assignment index names for consistency
AnujChhikara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,10 @@ CORS_ALLOWED_ORIGINS='http://localhost:3000,http://localhost:8000' | |
|
||
SWAGGER_UI_PATH='/api/schema' | ||
|
||
ADMIN_EMAILS = "[email protected],[email protected]" | ||
ADMIN_EMAILS = "[email protected],[email protected]" | ||
|
||
POSTGRES_HOST: postgres | ||
POSTGRES_PORT: 5432 | ||
POSTGRES_NAME: todo_postgres | ||
POSTGRES_USER: todo_user | ||
POSTGRES_PASSWORD: todo_password | ||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,324 @@ | ||
# Dual-Write System: MongoDB to Postgres | ||
|
||
## Overview | ||
|
||
The dual-write system ensures that all data written to MongoDB is also persisted in a PostgreSQL database with a well-defined schema. This system is designed to enable future migration from MongoDB to Postgres with minimal operational risk and code changes. | ||
|
||
## Architecture | ||
|
||
### Components | ||
|
||
1. **Postgres Models** (`todo/models/postgres/`) | ||
- Mirror MongoDB collections with normalized schema | ||
- Include sync metadata for tracking sync status | ||
- Use `mongo_id` field to maintain reference to MongoDB documents | ||
|
||
2. **Dual-Write Service** (`todo/services/dual_write_service.py`) | ||
- Core service for writing to both databases | ||
- Handles data transformation between MongoDB and Postgres | ||
- Records sync failures for alerting | ||
|
||
3. **Enhanced Dual-Write Service** (`todo/services/enhanced_dual_write_service.py`) | ||
- Extends base service with batch operations | ||
- Provides enhanced monitoring and metrics | ||
- Supports batch operation processing | ||
|
||
4. **Abstract Repository Pattern** (`todo/repositories/abstract_repository.py`) | ||
- Defines interface for data access operations | ||
- Enables seamless switching between databases in the future | ||
- Provides consistent API across different storage backends | ||
|
||
5. **Postgres Repositories** (`todo/repositories/postgres_repository.py`) | ||
- Concrete implementations of abstract repositories | ||
- Handle Postgres-specific operations | ||
- Maintain compatibility with existing MongoDB repositories | ||
|
||
## Configuration | ||
|
||
### Environment Variables | ||
|
||
```bash | ||
# Dual-Write Configuration | ||
DUAL_WRITE_ENABLED=True # Enable/disable dual-write | ||
DUAL_WRITE_RETRY_ATTEMPTS=3 # Number of retry attempts | ||
DUAL_WRITE_RETRY_DELAY=5 # Delay between retries (seconds) | ||
|
||
# Postgres Configuration | ||
POSTGRES_HOST=localhost | ||
POSTGRES_PORT=5432 | ||
POSTGRES_NAME=todo_postgres | ||
POSTGRES_USER=todo_user | ||
POSTGRES_PASSWORD=todo_password | ||
``` | ||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Django Settings | ||
|
||
The system automatically configures Django to use Postgres as the primary database while maintaining MongoDB connectivity through the existing `DatabaseManager`. | ||
|
||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Usage | ||
|
||
### Basic Usage | ||
|
||
```python | ||
from todo.services.enhanced_dual_write_service import EnhancedDualWriteService | ||
|
||
# Initialize the service | ||
dual_write_service = EnhancedDualWriteService() | ||
|
||
# Create a document (writes to both MongoDB and Postgres) | ||
success = dual_write_service.create_document( | ||
collection_name='users', | ||
data=user_data, | ||
mongo_id=str(user_id) | ||
) | ||
|
||
# Update a document | ||
success = dual_write_service.update_document( | ||
collection_name='users', | ||
mongo_id=str(user_id), | ||
data=updated_data | ||
) | ||
|
||
# Delete a document | ||
success = dual_write_service.delete_document( | ||
collection_name='users', | ||
mongo_id=str(user_id) | ||
) | ||
``` | ||
|
||
### Batch Operations | ||
|
||
```python | ||
# Perform multiple operations in batch | ||
operations = [ | ||
{ | ||
'collection_name': 'users', | ||
'data': user_data, | ||
'mongo_id': str(user_id), | ||
'operation': 'create' | ||
}, | ||
{ | ||
'collection_name': 'tasks', | ||
'data': task_data, | ||
'mongo_id': str(task_id), | ||
'operation': 'update' | ||
} | ||
] | ||
|
||
success = dual_write_service.batch_operations(operations) | ||
``` | ||
|
||
## Data Mapping | ||
|
||
### MongoDB to Postgres Schema | ||
|
||
| MongoDB Collection | Postgres Table | Key Fields | | ||
|-------------------|----------------|------------| | ||
| `users` | `postgres_users` | `google_id`, `email_id`, `name` | | ||
| `tasks` | `postgres_tasks` | `title`, `status`, `priority`, `created_by` | | ||
| `teams` | `postgres_teams` | `name`, `invite_code`, `created_by` | | ||
| `labels` | `postgres_labels` | `name`, `color` | | ||
| `roles` | `postgres_roles` | `name`, `permissions` | | ||
| `task_assignments` | `postgres_task_assignments` | `task_mongo_id`, `user_mongo_id` | | ||
| `watchlists` | `postgres_watchlists` | `name`, `user_mongo_id` | | ||
| `user_team_details` | `postgres_user_team_details` | `user_id`, `team_id` | | ||
| `user_roles` | `postgres_user_roles` | `user_mongo_id`, `role_mongo_id` | | ||
| `audit_logs` | `postgres_audit_logs` | `action`, `collection_name`, `document_id` | | ||
|
||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### Field Transformations | ||
|
||
- **ObjectId Fields**: Converted to strings (24 characters) | ||
- **Nested Objects**: Flattened or stored in separate tables | ||
- **Arrays**: Stored in junction tables (e.g., `PostgresTaskLabel`) | ||
- **Timestamps**: Preserved as-is | ||
- **Enums**: Mapped to Postgres choices | ||
|
||
## Sync Status Tracking | ||
|
||
Each Postgres record includes sync metadata: | ||
|
||
```python | ||
class SyncMetadata: | ||
sync_status: str # 'SYNCED', 'PENDING', 'FAILED' | ||
sync_error: str # Error message if sync failed | ||
last_sync_at: datetime # Last successful sync timestamp | ||
``` | ||
|
||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Error Handling and Alerting | ||
|
||
### Sync Failures | ||
|
||
The system automatically records sync failures: | ||
|
||
```python | ||
# Get sync failures | ||
failures = dual_write_service.get_sync_failures() | ||
|
||
# Get sync metrics | ||
metrics = dual_write_service.get_sync_metrics() | ||
``` | ||
|
||
### Alerting | ||
|
||
- **Immediate Logging**: All failures are logged with ERROR level | ||
- **Critical Alerts**: Logged with CRITICAL level for immediate attention | ||
- **Failure Tracking**: Maintains list of recent failures for monitoring | ||
|
||
### Retry Mechanism | ||
|
||
- **Automatic Retries**: Failed operations are automatically retried | ||
- **Configurable Attempts**: Set via `DUAL_WRITE_RETRY_ATTEMPTS` | ||
- **Exponential Backoff**: Delay increases between retry attempts | ||
- **Manual Retry**: Failed operations can be manually retried | ||
|
||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Monitoring and Health Checks | ||
|
||
### Metrics | ||
|
||
```python | ||
# Get comprehensive sync metrics | ||
metrics = dual_write_service.get_sync_metrics() | ||
|
||
# Check sync status of specific document | ||
status = dual_write_service.get_sync_status('users', str(user_id)) | ||
``` | ||
|
||
## Future Migration Path | ||
|
||
### Phase 1: Dual-Write (Current) | ||
- All writes go to both MongoDB and Postgres | ||
- Reads continue from MongoDB | ||
- Postgres schema is validated and optimized | ||
|
||
### Phase 2: Read Migration | ||
- Gradually shift read operations to Postgres | ||
- Use feature flags to control read source | ||
- Monitor performance and data consistency | ||
|
||
### Phase 3: Full Migration | ||
- All operations use Postgres | ||
- MongoDB becomes read-only backup | ||
- Eventually decommission MongoDB | ||
|
||
### Code Changes Required | ||
|
||
The abstract repository pattern minimizes code changes: | ||
|
||
```python | ||
# Current: MongoDB repository | ||
from todo.repositories.user_repository import UserRepository | ||
user_repo = UserRepository() | ||
|
||
# Future: Postgres repository (minimal code change) | ||
from todo.repositories.postgres_repository import PostgresUserRepository | ||
user_repo = PostgresUserRepository() | ||
|
||
# Same interface, different implementation | ||
user = user_repo.get_by_email("[email protected]") | ||
``` | ||
|
||
## Performance Considerations | ||
|
||
### Synchronous Operations | ||
- **Pros**: Immediate consistency, simple error handling | ||
- **Cons**: Higher latency, potential for MongoDB write failures | ||
|
||
### Batch Operations | ||
- **Pros**: Reduced database round trips, better throughput | ||
- **Cons**: Potential for partial failures | ||
|
||
## Security | ||
|
||
### Data Privacy | ||
- All sensitive data is encrypted in transit | ||
- Postgres connections use SSL | ||
- Access controls are maintained across both databases | ||
|
||
### Audit Trail | ||
- All operations are logged in audit logs | ||
- Sync failures are tracked for compliance | ||
- Data integrity is maintained through transactions | ||
|
||
## Testing | ||
|
||
### Unit Tests | ||
- Test individual components in isolation | ||
- Mock external dependencies | ||
- Verify data transformation logic | ||
|
||
### Integration Tests | ||
- Test end-to-end sync operations | ||
- Verify data consistency between databases | ||
- Test failure scenarios and recovery | ||
|
||
### Performance Tests | ||
- Measure sync latency under load | ||
- Test batch operation efficiency | ||
|
||
## Troubleshooting | ||
|
||
### Common Issues | ||
|
||
1. **Postgres Connection Failures** | ||
- Check database credentials and network connectivity | ||
- Verify Postgres service is running | ||
- Check firewall settings | ||
|
||
2. **Sync Failures** | ||
- Review sync error logs | ||
- Check data transformation logic | ||
- Verify Postgres schema matches expectations | ||
|
||
3. **Performance Issues** | ||
- Monitor sync latency | ||
- Optimize batch operation sizes | ||
- Monitor database performance | ||
|
||
### Debug Commands | ||
|
||
```python | ||
# Enable debug logging | ||
import logging | ||
logging.getLogger('todo.services.dual_write_service').setLevel(logging.DEBUG) | ||
|
||
# Check sync status | ||
status = dual_write_service.get_sync_status('users', str(user_id)) | ||
print(f"Sync status: {status}") | ||
|
||
# Get recent failures | ||
failures = dual_write_service.get_sync_failures() | ||
for failure in failures: | ||
print(f"Collection: {failure['collection']}, ID: {failure['mongo_id']}") | ||
``` | ||
|
||
## Deployment | ||
|
||
### Prerequisites | ||
- PostgreSQL 15+ with appropriate extensions | ||
- MongoDB 7+ (existing) | ||
- Python 3.9+ with required packages | ||
|
||
### Setup Steps | ||
1. Create Postgres database and user | ||
2. Run Django migrations | ||
3. Configure environment variables | ||
4. Verify sync operations | ||
|
||
### Production Considerations | ||
- Use connection pooling for Postgres | ||
- Set up monitoring and alerting | ||
- Implement backup and recovery procedures | ||
|
||
## Support and Maintenance | ||
|
||
### Regular Maintenance | ||
- Monitor sync metrics and failures | ||
- Review and optimize Postgres performance | ||
- Update sync logic as schema evolves | ||
- Clean up old sync failure records | ||
|
||
### Updates and Upgrades | ||
- Test sync operations after schema changes | ||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Verify data consistency after updates | ||
- Monitor performance impact of changes | ||
- Update documentation as needed |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.