0

I was planing to create an API based on microservices, I'm stuck on how to solve a specific scenario.

Initial plan is, all microservices are only accessible through a REST API.

Based on the next schema, if you want to get all the orders you simply go to /orders and get a list of them.

But whats happens when you want to get all orders related to a client? You go to /clients/123/orders and internally clients make a request to orders and then clients returns you the orders list, but don't make any sense to make a internal request to orders, receive a long list from orders and return that list again to the client from clients. Also the pagination will be a pain here.

I read about having an accessible view of the orders database from clients but not sure whats is the best approach for this.

enter image description here

6
  • 2
    /orders?client=123? /orders/client/123? "get all orders" is fun while you're testing, but imagine how long that would take you to download from say Amazon...
    – Mat
    Commented Feb 27, 2020 at 15:47
  • If you need to be able to get all or specific orders for a client it would be in the Orders bounded context. The Orders DB would have to have to record the client id along with the order for you to query it. i.e: as mentioned above /orders/client/123 or similar. Pagination is fine, you will have to keep track of the relevant current page where paging is implemented .i.e: web client/mobile client etc.. and have a query on the Orders gateway to allow requesting paged data. If the complete list is always very very short you can even return all orders and purely use the client for paging.
    – Nope
    Commented Feb 27, 2020 at 16:10
  • 3
    There's no reason for you to use the API under the covers. A request to /clients/123/orders, doesn't mean that the API will make another API call to /orders. It would just be the same old /orders call, but will a client ID filter. The REST API is only for the consumer, you're not bound by its interface under the covers. Commented Feb 27, 2020 at 16:29
  • @ChrisMurray For returning paged data, while current page and number of items displayed can be managed by the gateway, an endpoint in the Orders-Query service would have to exist that takes parameters to allow identifying how many items to skip, to return and what sort order is relevant for the query. Its not preferred for the Query Service to worry about sorting and paging, etc.. but if a client has tons of orders, returning all each time to show a subset of <pageSize> would be very inefficient. An alternative is to create a separate OrderPagedQuery Service but that would prob be overkill.
    – Nope
    Commented Feb 28, 2020 at 10:49
  • 1
    Then we are talking about the same thing :) I was looking at ` It would just be the same old /orders call, but will a client ID filter` which I thought you meant the API should always return all data and let the caller sort it out. Never mind then :)
    – Nope
    Commented Feb 28, 2020 at 12:16

2 Answers 2

2

A request to /clients/123/orders is an orders-related request, so it should be handled by the orders microservice, which will only return the orders for client 123.

The job of the orders microservice is to handle requests about orders, not to handle requests where the path starts with /orders.

3
  • So there should be a rule in the api gateway to route requests with that format to orders? Commented Feb 27, 2020 at 18:57
  • @RicardoAlbear If that is how your technology stack works. Or you could choose a path starting with /orders, however that might break your rules of good API design.
    – user20574
    Commented Feb 27, 2020 at 19:06
  • The solution of set a rule on the api gateway for /orders and /clients/{client_id}/orders pointed to the orders microservice looks the most logic for me, and you preserve the API design. Commented Feb 27, 2020 at 19:11
0

You could design paging behavior for your backend processing, receiving parameters such as startOrderDate, startOrderId, pageNumber, pageSize, etc and returning information such as pageNumber, hasMoreRecords, etc....

1
  • This is an XY problem, and this solution only solves Y.
    – user20574
    Commented Feb 27, 2020 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.