title: How would you design a simple chat application using microservices?
body: <p>So let's say these are my tables:</p>
  
  <pre><code>users
  id(pk) | username
  
  channels
  id(pk) | title
  
  members
  user_id(pk, fk) | channel_id(pk, fk)
  
  messages
  id(pk) | channel_id(fk) | sender_id(fk) | text | created_at
  </code></pre>
  
  <p>From what I read about microservices, they should be able to be deployed by themselves, and have their own database.</p>
  
  <p>I think that means I would have these microservices each running their own SQL instance:</p>
  
  <p>Users Microservice</p>
  
  <pre><code>users
  id(pk) | username
  </code></pre>
  
  <p>Messaging Microservice</p>
  
  <pre><code>channels
  id(pk) | title
  
  members
  user_id(pk, fk) | channel_id(pk, fk)
  
  messages
  id(pk) | channel_id(fk) | sender_id(fk) | text | created_at
  </code></pre>
  
  <p>I read that to expose these microservices, I should have some sort of REST/GraphQL API for each one, so Messaging Microservice would have things like <code>getChannel()</code>, <code>createMessage()</code>, etc.</p>
  
  <p>So my question is what should I do when I need to access data in another microservice, basically how should they talk to each other? For example, say on my frontend I enter a chat channel and want to display its messages. If I do a <code>getChannelMessages()</code>, this would return me a list of</p>
  
  <pre><code>{
    id: 1,
    channel_id: 32,
    sender_id: 48,
    text: 'hello world',
    created_at: some unix time
  }
  </code></pre>
  
  <p>At this point, I don't know who <code>sender_id: 48</code> is, which I need to display. From there, am I supposed to call Users Microservice's API with <code>getUser(sender_id)</code> for every single message or something?</p>
  
  <p>What if a user gets deleted? Feels like I have to do some extra work to reach into the other microservice to delete all of their messages and remove them from channels. Don't things get out of sync?</p>
  
  <p>Lastly, doesn't this seem like a bit overkill if I only have a single frontend? Am I supposed to have a single frontend that talks to each microservice or are my microservices supposed to have their own frontends too? Maybe if this were a web app I could split URLs for each frontend or something, but multiple frontends doesn't make sense to me in the context of say, a mobile app.</p>
  
  <p>Having trouble putting this all together and would appreciate any advice and clarification.</p>