Microservice communication — services talk via HTTP, gRPC, or messages.
HTTP (synchronous):
1// Service A calls Service B2async function getOrderWithUser(orderId) {3 const order = await fetch(`http://order-service/orders/${orderId}`);4 const user = await fetch(`http://user-service/users/${order.userId}`);5 return { ...order, user };6}
Message queue (asynchronous):
1// Service A publishes event2await channel.publish("orders", "order.created", {3 orderId: 123,4 userId: 456,5});67// Service B consumes event8await channel.consume("orders", async (msg) => {9 const event = JSON.parse(msg.content);10 if (msg.fields.routingKey === "order.created") {11 await sendConfirmation(event.userId);12 }13 channel.ack(msg);14});
Patterns: