In decomposing a monolithic web application into smaller services and by following the Strangler Fig pattern, I'm in the middle of a problem for which I can't find a practical solution. There is a web app written in PHP and I'm taking steps to:
Move highly cohesive code into its own service (more precisely a docker container later and for now I wouldn't want to refactor a lot or rewrite significant parts of the application, which is huge).
Move business logic from controllers and other places to a service / repository layer under a known namespace (this is a clean up step)
Modify all old code to invoke new code (it will be like
Then bring up a docker container in the same network which handles the requests from step
$this->newService->...calls will be remote calls but the code is not remote. I just added an extra network call here.)
Now the problem I'm facing is that I see different code that changes the model state (by model I mean M as in MVC) and then passes it to some other methods, they make their own changes on the model-object and then the changes are finally committed and written to the database.
But since I'm delegating all jobs to the new service, it would be like this:
$this->newService->methodA(123); // this selects a record from a MySQL table, performs some work and commits those changes $this->newService->methodB(123); // this does the same thing
So with this new service I will have two selects and two updates, but in the monolithic application, there is no such repetition:
$model = Model::getByID(123); // these are local calls $this->serviceOne->methodA($model); $this->serviceTwo->methodB($model); $model->save();
My question is, what should be done in this case to prevent multiple database reads and writes? I am considering building a caching layer in order to persist model state and in that way avoid multiple individual reads and writes to the database and then later all at once commit all accumulated model changes to the database, but I can't figure if that is the right solution or not. I'm literally lost.