3

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!

Daksh
  • 1,177
  • 2
  • 18
  • 28

3 Answers3

2

#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.

oleksii
  • 35,458
  • 16
  • 93
  • 163
  • To #2, I want Order microservice to be totally independent of Channel microservice. Plus, I want Channel to plug in to Order so that the person developing Channel microservice will need to take care of the appropriate points of where to send an SMS. Not only SMS/push, but any other unthought-of microservice per se – Daksh Dec 08 '15 at 15:24
  • Well your services are independent from each other - code wise. They are integrated via REST. You'd have common data structures and API. You create a data transfer object and send it away. You can have a processing pipe tree, initially an order goes to the root ingest API, that API decides where to route your message. For this to work, services must be aware of the location of other services and the interface. You cannot make microservices completely independent. Such system will not be able to exchange messages inside as it is not aware of any subcomponents. Make sense? – oleksii Dec 08 '15 at 15:32
  • So, instead of my thought-of producing a message in the cross-server-shared Pub/Sub channel, I am only able to call an API from the API gateway? Another doubt, can my API gateway forward my API request to multiple microservices' APIs, and not only one? (kinda obvious, but I would love to know the HOW of it). Also, please do tell me does a cross-server-pub-sub system even exist as per your knowledge? – Daksh Dec 08 '15 at 15:45
  • You keep asking good, but too many questions :) If you want to use REST, then you services need to know about one another. You can send one request to many services. Kinda like this: `client => gateway => { service1, service2, ..., serviceN, database(s) }`. If you want to use pubsub - that's a different story. Have a look at message queues, such as nservicebus, or kafka (among many others) – oleksii Dec 08 '15 at 16:19
  • I did imagine that the API Gateway does not simply act as a one-to-one forwarder, but can be programmed to forward the request to multiple receivers. Since I haven't ever built an API Gateway, thus the question. And as for the quality/amount of the questions, haha, yes, still learning about all of this, so the questions might just keep coming in :-P . Thanks for all your help! :-D – Daksh Dec 08 '15 at 17:12
1

To your first question, you don't need to do 100 queries, just one with the array of your 100 documents, like the following:

db.collection.find( { _id : { $in : [1,2,3,4] } } );

https://stackoverflow.com/a/7713461/1384539

Community
  • 1
  • 1
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • So, will I get 100 response objects to the 100 query IDs in order ("in order" being key)? – Daksh Dec 08 '15 at 15:19
0

I know this question is 1 year old, but I would like to add my answer to the first point.

One option would be to use some form of CQRS and store on the OrderDB also some of the user details when creating an order. This way when you have to show the list of orders you already have all the details you need. Also, the order document would represent a photograph of the user state at the moment of the order creation.

Of course, in case you don't have the user details when storing the order, you just need to make a GET call to the User Service, but that would be 1 call, not 100.

David Guida
  • 950
  • 9
  • 19