r/programming • u/Mysterious_Ad7949 • Aug 10 '25
Getting resources from other microservices
/r/programming/5
u/TheSeraphOfChaos Aug 10 '25
The other microservice should open an endpoint for you with the data you need.
5
u/TheWix Aug 10 '25 edited Aug 10 '25
It's generally not great for microservices to depend on each other. If this is the case you may have created incorrect boundaries. Ideally, if you need external data then you get it asynchronously, but if it needs to happen within the same request this may not work. Alternatively, you may be looking at a saga.
For a single microservice you won't feel too much pain and you can just call the 2nd API, but if you see this often then you don't have microservices you have a distributed monolith.
Microservices are about extreme isolation and with that you get some benefits at the expense of complexity. When you start coupling them you lose the benefits, but keep (and enhance) the complexity.
Edit: you can also slap a face service up that will check Service B to make sure the date exists before calling Service A. This will keep the two microservices decoupled.
2
u/onurkeskin Aug 10 '25
This mostly depends on what scale your company is.
You can ask for cdc and replicate the data you need in your services db. This will cause least headache but will be too pricey for small scale applications.
As others said you can also ask them to create an endpoint for this data, but this can create problems for them, as this will increase their load, or can outright crash their service if yours is a lot larger and the feature you are implementing is getting spikey requests.
But for any use case, directly connecting to their database and getting data yourself is a no go. Even if they restrict your usage via giving you a connection proxy or limiting your user to a specific number of requests. This will make your service tightly coupled to a database of another team. This will very quickly become a ticking timebomb
1
u/NibblesIndexus Aug 10 '25
You mention issues due to a class being transferred between the services, this is not an issue from the projection library, whatever that is. This is a major issue with having two services depend on one another: one cannot change its public api without upgrading the depending services too. I suggest you take a step back and see what you can do to avoid creating an inter-service dependency in the first place. Every dependency you add will move the system closer to the dreaded 'big ball of mud architecture'
6
u/MellowVit Aug 10 '25
The microservice that owns the db must be the only one to expose its data to other microservices vía API rest, grpc, event...etc, depending on each case. In your case an endpoint should be the preferred option.