Junior — Middle
What methods are used for synchronous interaction between microservices?
sobes.tech AI
Answer from AI
Synchronous interaction between microservices is usually carried out through:
- HTTP/REST API — the most common method, where one service makes an HTTP request to another and waits for a response.
- gRPC — a high-performance remote procedure call protocol supporting synchronous calls with protobuf-based contracts.
- SOAP — less common now, but also an option for synchronous interaction via web services.
Example in Python using requests for REST:
import requests
def get_user_data(user_id):
response = requests.get(f'http://user-service/api/users/{user_id}')
if response.status_code == 200:
return response.json()
else:
raise Exception('Error fetching user data')
In synchronous interaction, it is important to consider delays and fault tolerance, as the call blocks execution until a response is received.