Search code examples
restserverpublish-subscribemicroservices

Inter-microservices Communication using REST & PUB/SUB


This is still a theory in my mind.

I'm rebuilding my backend by splitting things into microservices. The microservices I'm imagining for starting off are:
- Order (stores order details and status of each order)
- Customer (stores customer details, addresses, orders booked)
- Service Provider (stores service provider details, status & location of each service provider, order(s) currently being processed by the service provider, etc.)
- Payment (stores payment info for each order)
- Channel (communicates with customers via email / SMS / mobile push)

I hope to be able to use PUB/SUB to create a message with corresponding data, which can be used by any other microservice subscribing to that message.

First off, I understand the concept that each microservice should have complete code & data isolation (thus, on different instances / VMs); and that all microservices should communicate strictly using HTTP REST API contracts.

My doubts are as follows:

  1. To show a list of orders, I'll be using the Order DB to get all orders. In each Order document (I'll be using MongoDB for storage), I'll be having a customer_id Foreign Key. Now the issue of resolving customer_name by using customer_id.
    If I need to show 100 orders on the page and go with the assumption that each order has a unique customer_id associated with it, then will I need to do a REST API call 100 times so as to get the names of all the 100 customer_ids? Or, is data replication a good solution for this problem?

  2. I am envisioning something like this w.r.t. PUB/SUB: The business center personnel mark an order as assigned & select the service provider to allot to that order. This creates a message on the cross-server PUB/SUB channel.
    Then, the Channel microservice (which is on a totally different instance / VM) captures this message & sends a Push message & SMS to the service provider's device using the data within the message's contents.
    Is this possible at all?
    UPDATE TO QUESTION 2: I want the Order microservice to be completely independent of any other microservices that will be built upon / side-by-side it. Channel microservice is an example of a microservice that depends upon events taking place within Order microservice.

Also, please guide me as to what all technologies / libraries to use.

What I'll be developing on:
Java
MongoDB
Amazon AWS instances for each microservice.

Would appreciate anyone's help on this.
Thanks!


Solution

  • #1

    If I need to show 100 orders and each order has a unique customer_id, will I need to do 100 REST API call?

    No, just make 1 request with 100 order_id(s) and return a dictionary of order_id <=> customer_id

    #2
    It's a single request

    POST
    /orders/new
    {
        "selected_service_provider_id" : "123"
        ...
    }
    

    Which can return you order_id and you can print it locally for the customer or track progress or what have you.

    On the server side, you receive an order and process it. Processing can include sending an SMS at some stage. This functionality can be implemented inside original service that received this request or as a separate call to another dedicated service.