There are two issues it’s worthwhile to contemplate earlier than performing information migration in a steady, reside system.
One, how do you guarantee information within the new location stays up to date? At this level, your APIs are nonetheless writing to the previous storage. Any updates to information which have been migrated might be missed within the new storage.
Two, how do you easily migrate your APIs to make use of the brand new storage? Equally, if something unhealthy occurs, it’s worthwhile to roll again your APIs again to previous storage. A clean transition at any time between previous and new storage is the important thing to migration with out downtime.
To unravel these issues, we’ll introduce a read-write flag. A flag is a swap that turns sure functionalities on or off throughout runtime, with none code change.
Our flag has 4 values, every representing a selected means the APIs work together with information storage.
At first, the APIs work together solely with the previous storage, which is the default conduct.
If you toggle the flag to 1, the APIs learn information from the previous storage, however write to each storages. Writing information to each areas should start earlier than information migration begins. This ensures the brand new storage stays up to date and the previous storage stays practical.
As soon as the migration has ended, the APIs can learn information from the brand new storage. If there are sudden errors, you may instantly swap the flag again to 1. Because of “double write”, the previous storage remains to be usable.
Lastly, when all is sweet, you may toggle your APIs to make use of the brand new storage solely. This marks the top of the migration.
Discover how the info stream is dynamically managed by one single swap. All through the method, no code change or deployment is required. All of the associated logic might be pre-prepared earlier than the migration begins.
On the code degree, we will simply carry out conditional checks with the read-write flag. The pseudocode appears one thing like under.
// 4 values of the flagoldOnly = 0
writeBoth = 1
readNew = 2
newOnly = 3// helper capabilities for situation checksoperate isReadNew(flag) flag == newOnly
return flag != newOnly
return flag != oldOnly
// learn logicif isReadNew(flag)
// write logicif isWriteOld(flag)