API GW communication

Viewed 73

I might have some misunderstanding around Microservice communication and I need someone that clarify it to me please.

Let's say that we have a nodeJs microservice app and we have an API Gateway that handles incoming requests from clients.

Now, let's imagine that two clients send one-one request to API GW at the same time and that API GW forward it to the right service.

  • How can a service handle two requests at the same time?

  • Now let's say that this service somehow respond both requests at once, how can we identify each response to the right client?

  • How we can handle the huge number for the requests received by API GW??

I hope my questions are clear.

1 Answers

How can a service handle two requests at the same time?

Now let's say that this service somehow respond both requests at once, how can we identify each response to the right client?

API CALL

if the client-app interact by api call (in most of cases) so it just same request/response happening.in microservice client see the microservices as one using api gateway.

scenario 1
client-app call loalhost:9000/getproduct which is api gateway and redirect this request to localhost:8000/getproduct let say product.service and also forward back the response like a simple api call thats it. (reverse-proxy)

scenario 2
client-call call localhost:9000/product-report and this request interacts with two services let called product.service localhost:8000/getproduct and inventory.service localhost:8010/getinventory-data. in this case api gateway redirect request to two end point services and can merge the responses as one and back to client (api gateway aggregates)

so from outside it is just a simple request response and no worries about multiple call. it is asynchronous communication.

Event-Driven
it is very rare that client-app uses event to communicate to backend services. mostly it is used for intercommunication between services. in this case your question become meaningful. this type of communication is asynchronous which means one event with data is publish from microservice-A and this microservice does not care who is consuming it and not wait for response.so it is one direction communication but you can implement in such a way that it acts as bio-direction communication.

scenario
service-A publish event-X on channel-X service-B listen to channel-X and consume event-x and consequently publish event-y(response) on channel-Y where the service-A listen to and receive event-Y. these type of communication uses some patterns to be reliable (SAGA ,event sourcing,CAP theory, circuit breaker etc) but lets say in most of the time these events are tagged with id, date and all data properties which describes the event (who is who).

gRPC
these are fast performant asynchronous communication and this one also mostly use between services. this communication needed when you need immediate guaranteed response like api call(API CALL ALSO IS A ASYNCHRONOUS).in this approach you defining protocol which here calls proto-buffer and you may implement server-client communication between services and here also like api you no need worry about multiple request.

How we can handle the huge number for the requests received by API GW??

another responsibilis of api-gateway is load-balancing and rate-limiting. in microservice implementation the services are containerized (lets say they deploy as images) mostly by docker.let say your service-A is deployed or containerized as named Cntainer-A. you may have number of instances of this container-A with different port. container-A:8010,container-A:8011,container-A:8012. this is called horizontal scaling.the load-balancer will split the incoming traffic (requesting service-A) among these based on how you configured it. rate-limiting will configured load-balancer to limit the define amount of traffic over them. hope help you there.

Related