5 Microservice collaboration

 

This chapter covers

  • Understanding how microservices collaborate through commands, queries, and events
  • Comparing event-based collaboration with collaboration based on commands and queries
  • Implementing an event feed
  • Implementing command-, query-, and event-based collaboration
  • Deploying collaborating microservices to Kubernetes

Each microservice implements a single capability, but to deliver end-user functionality, microservices need to collaborate. Microservices can use three main communication styles for collaboration: commands, queries, and events. Each style has its strengths and weaknesses, and understanding the tradeoffs between them allows you to pick the appropriate one for each microservice collaboration. When you get the collaboration style right, you can implement loosely coupled microservices with clear boundaries.

In this chapter, I’ll show you how to implement all three collaboration styles in code using HTTP GET, POST, PUT, and DELETE endpoints. I will also show how to deploy several collaborating microservices to Kubernetes and have them collaborate as intended using all three collaboration styles.

5.1 Types of collaboration: Commands, queries, and events

Microservices are fine grained and narrowly scoped. To deliver functionality to an end user, microservices need to collaborate.

5.1.1 Commands and queries: Synchronous collaboration

5.1.2 Events: Asynchronous collaboration

5.1.3 Data formats

5.2 Implementing collaboration

5.2.1 Setting up a project for the loyalty program

5.2.2 Implementing commands and queries

5.2.3 Implementing commands with HTTP POST or PUT

5.2.4 Implementing queries with HTTP GET

5.2.5 Implementing an event-based collaboration

5.2.6 Deploying to Kubernetes

5.2.7 Building a Docker container special offers microservice

5.2.8 Building a Docker container for both parts of the loyalty program

5.2.9 Deploying the loyalty program API and the special offers

5.2.10 Deploy EventConsumer