title: How to merge/consolidate responses from multiple RESTful microservices?
body: <p>Let's say there are two (or more) RESTful microservices serving JSON. Service (A) stores user information (name, login, password, etc) and service (B) stores messages to/from that user (e.g. sender_id, subject, body, rcpt_ids).</p>
  
  <p>Service (A) on <code>/profile/{user_id}</code> may respond with:</p>
  
  <p><code>{id: 1, name:'Bob'}</code></p>
  
  <p><code>{id: 2, name:'Alice'}</code></p>
  
  <p><code>{id: 3, name:'Sue'}</code></p>
  
  <p>and so on</p>
  
  <p>Service (B) responding at <code>/user/{user_id}/messages</code> returns a list of messages destined for that {user_id} like so:</p>
  
  <p><code>{id: 1, subj:'Hey', body:'Lorem ipsum', sender_id: 2, rcpt_ids: [1,3]},</code></p>
  
  <p><code>{id: 2, subj:'Test', body:'blah blah', sender_id: 3, rcpt_ids: [1]}</code></p>
  
  <hr>
  
  <p>How does the client application consuming these services handle putting the message listing together such that names are shown instead of sender/rcpt ids?</p>
  
  <p><strong>Method 1</strong>: Pull the list of messages, then start pulling profile info for each id listed in <code>sender_id</code> and <code>rcpt_ids</code>? That may require 100's of requests and could take a while. Rather naive and inefficient and may not scale with complex apps???</p>
  
  <p><strong>Method 2</strong>: Pull the list of messages, extract all user ids and make bulk request for all relevant users separately... this assumes such service endpoint exists. There is still delay between getting message listing, extracting user ids, sending request for bulk user info, and then awaiting for bulk user info response. </p>
  
  <p>Ideally I want to serve out a complete response set in one go (messages and user info). My research brings me to merging of responses at service layer... a.k.a. <strong>Method 3</strong>: API Gateway technique. </p>
  
  <p>But how does one even implement this?</p>
  
  <p>I can obtain list of messages, extract user ids, make a call behind the scenes and obtain users data, merge result sets, then serve this final result up... This works ok with 2 services behind the scenes... But what if the message listing depends on more services... What if I needed to query multiple services behind the scenes, further parse responses of these, query more services based on secondary (tertiary?) results, and then finally merge... where does this madness stop? How does this affect response times?</p>
  
  <p>And I've now effectively created another "client" that combines all microservice responses into one mega-response... which is no different that <strong>Method 1</strong> above... except at server level.</p>
  
  <p>Is that how it's done in the "real world"? Any insights? Are there any open source projects that are built on such API Gateway architecture I could examine?</p>