TASK
Implementation
When a migration goes wrong in production, you need a rollback plan. Different situations call for different strategies: instant feature flag disables for behavioural changes, blue-green traffic switch for deployment issues, migration down() functions for schema changes, and database restore for catastrophic data corruption.
Implement a node that supports four rollback strategies:
// 1. Migration rollback: run down() for version 3
{ "type": "rollback", "msg_id": 1, "version": 3 }
-> { "type": "rollback_complete", "in_reply_to": 1,
"rolled_back_from": 3, "rolled_back_to": 2,
"duration_seconds": 5 }
// 2. Feature flag rollback: disable instantly
{ "type": "disable_feature", "msg_id": 2,
"feature": "new_checkout" }
-> { "type": "feature_disabled", "in_reply_to": 2,
"feature": "new_checkout", "rolled_back_instant": true }
// 3. Blue-green: switch traffic back to the previous environment
{ "type": "switch_traffic", "msg_id": 3,
"from": "green", "to": "blue" }
-> { "type": "traffic_switched", "in_reply_to": 3,
"current_environment": "blue", "downtime_seconds": 0 }
// 4. Restore from backup (last resort)
{ "type": "restore", "msg_id": 4,
"backup": "users_backup_20240115" }
-> { "type": "restore_complete", "in_reply_to": 4,
"restored_rows": 100000, "duration_seconds": 60 }Sample Test Cases
Rollback migrationTimeout: 5000ms
Input
{
"src": "admin",
"dest": "migrations",
"body": {
"type": "rollback",
"msg_id": 1,
"version": 3
}
}Expected Output
{"type": "rollback_complete", "in_reply_to": 1, "rolled_back_from": 3, "rolled_back_to": 2, "duration_seconds": 5}Feature flag rollbackTimeout: 5000ms
Input
{
"src": "admin",
"dest": "features",
"body": {
"type": "disable_feature",
"msg_id": 1,
"feature": "new_checkout"
}
}Expected Output
{"type": "feature_disabled", "in_reply_to": 1, "feature": "new_checkout", "rolled_back_instant": true}Hints
Hint 1▾
Migration rollback: run the down() function for the target version
Hint 2▾
Feature flag rollback: disable the flag instantly without a deployment
Hint 3▾
Blue-green: two identical environments; switch traffic from green back to blue in seconds
Hint 4▾
Database restore is the last resort: restore from a pre-migration backup
Hint 5▾
Rollback strategies range from instant (feature flag) to slow (database restore)
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
migration rollbackfeature flagsblue-green deploymentdatabase restoreinstant rollback
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()