I have a service, call it serviceA, which has a relational database. This service is old and bloated, modifications to the code are very difficult. But I also can't get rid of it, it's the heart of the system. Now I want to build serviceB. ServiceB is basically an extension of serviceA's capabilities, it has exactly the same domain. For this reason, and for reasons of performance, they both save their data in the same database. Both serviceA and serviceB can save an item of a certain type, call it itemC. The difference is that serviceB adds some metadata to itemC when it saves it.
Now here's the problem: there's a lot of business logic in serviceA around saving itemC. When saving an instance of itemC using serviceB this logic must execute and I have to know that the metadata is saved and that it's done quickly, no eventual consistency. I thought of three possible solutions:
- Having serviceA expose an endpoint which serviceB will use to save itemC and subsequently save the metadata. The problem is that this approach is potentially slow (there's no assurance that the metadata won't be saved a significant amount of time after itemC), complicated (I have to manage retries and consistency logic) and doesn't guarantee consistency.
- Having serviceB pass all necessary queries to save the metadata to serviceA and then having serviceA run them all in a transaction together with the query creating itemC. Potentially brittle because serviceA will have to run queries it knows nothing about.
- Extracting the itemC saving logic to a third service and have both services use it. That would require a lot of refactoring effort due to the condition of serviceA's code. It would also mean this third service would know about both services' tables, introducing coupling.
Any better ideas?
That would require a lot of refactoring effort due to the condition of service's A codetime to stop and pay back the tech debt. You can not keep building a sandcastle and ask others how to ignore the mess. Any solution based on ignoring the current design instead of change or adapt it is likely to contribute to the tech. debt. So, not a solution at all.