I am looking for a name to call this pattern that I have seen when maintaining legacy codebases.
Short version:
Run two versions of a function side-by-side, to ensure that changes to the new version remain compatible with the old version.
Question: Is there a well-known name for this pattern?
Long version:
- We need to change the internal implementation of some server function
foo
. For example, this could be for better performance, or to connect to a different database. - We must not change what the function returns i.e. the function still does the same job, but with some change to how it is implemented.
- The function is in a production server environment, and must remain available at all times.
- There are no dev environments, unit tests, or well-defined requirements, and creating them is well outside of our budget.
- The change is simple enough that it is within our budget.
(I acknowledge that in this situation, many best practices have been overlooked. The pattern I describe is a pragmatic solution.)
So we do these steps:
- Make two copies of
foo
, call themfoo_v1
andfoo_v2
. - Change
foo
so that it calls bothfoo_v1
andfoo_v2
, and returns the result offoo_v1
. From the outside, the only change is that it got slower, due to running 2 versions. - Change
foo
so that it compares the results offoo_v1
andfoo_v2
. If they are different, this is logged. - Change
foo_v2
to do whatever the new requirement is, e.g. a performance improvement. - Because we are comparing the results of running both functions with real data, we can be confident that both versions do the same thing.
- After some time has passed without detecting any differences, we copy the implementation of
foo_v2
intofoo
, and then deletefoo_v1
andfoo_v2
.
The key is that at all times the results come from a version of the function that we know is good.
I have used this pattern on a small scale when optimizing PHP functions which were making too many SQL queries. This Stripe blog post describes a large project with some similarities. They used a Ruby library named Scientist.
Aucun commentaire:
Enregistrer un commentaire