Microservices: Should a database only be accessible via API, or should all services in the domain have direct access?
I have a fairly standard microservices architecture. RESTful APIs and Workers, all connected through queues and message passing.
I have a users CRUD API that can support all user database operations. If I have a worker in the same domain that's listening to events and will need to change values in the user database based on the events. Should that user worker access the user database directly (through a seperate connection string), or should it use the user API?
Using the API would mean that the worker and the API are tightly coupled, and changes that would affect the worker, could also require a change to the API. This introduces coupling and increases the cost of change. On the other hand, the worker having direct database access means there isn't a single interface for the data, but would allow these to be deployed independently (minus breaking changes).
I feel like both are valid approaches, but I'm lacking the right terms to Google, and would love any suggestions or resources that have helped you decide between the two. Thanks!
Posted Mar 4, 2020, 4:04 PM UTC
We have had many issues with some people doing direct db updates instead of using api and this causes a lot of hard to debug problems downstream. I know this is a little different situation, but having one interface to the data seems to work better for us.
Can you go into some of the details what those are? Are those issues model drift where you have an string value in the model of "Thing" in one worker and "thing" in the other worker, and during processing, you do a if(x == "thing") and you get a bug?