jeudi 14 septembre 2023

Name for technique of running two versions of code to ensure they are compatible?

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:

  1. Make two copies of foo, call them foo_v1 and foo_v2.
  2. Change foo so that it calls both foo_v1 and foo_v2, and returns the result of foo_v1. From the outside, the only change is that it got slower, due to running 2 versions.
  3. Change foo so that it compares the results of foo_v1 and foo_v2. If they are different, this is logged.
  4. Change foo_v2 to do whatever the new requirement is, e.g. a performance improvement.
  5. Because we are comparing the results of running both functions with real data, we can be confident that both versions do the same thing.
  6. After some time has passed without detecting any differences, we copy the implementation of foo_v2 into foo, and then delete foo_v1 and foo_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