12E - Microservices - Communication
Microservice Communications
In this section, we will discuss microservice communications, focusing on the dimensions of synchronous and asynchronous communication.
Synchronous Communication
- In synchronous communication, the client sends a request to a microservice.
- The microservice processes the request and may forward it to another microservice.
- The client is blocked while waiting for the response.
- Both the client and services can be blocked in synchronous communication.
Asynchronous Communication
- In asynchronous communication, the client sends a request to a microservice.
- The client is not blocked while waiting for the response and can perform other operations.
- Multiple requests can be sent before receiving responses.
- Responses may arrive out of order.
Advantages of Asynchronous Communication
Asynchronous communication allows clients to perform operations while waiting for responses. However, it introduces additional complexity.
Handling Out-of-order Messages
- Asynchronous programming frameworks handle messages arriving out of order.
- A futures API is commonly used to handle responses when they arrive.
Communication Patterns
Different communication patterns exist in networked programs, including request-response, streaming (publish-subscribe), and message passing.
Request-Response Pattern
- Client sends a request and receives a response.
- Can be synchronous or asynchronous.
Streaming (Publish-Subscribe) Pattern
- Service sends a stream of responses to the client.
- Client subscribes to specific information updates from the service.
Message Passing Pattern
- Clients or microservices send data to a channel.
- Other microservices can read data from that channel.
- Allows scaling by adding more instances if needed.
New Section
This section discusses the different paradigms and frameworks used in microservices, including synchronous and asynchronous communication, streaming, and message passing.
Communication Paradigms
- Synchronous communication is commonly used in microservices as it runs on top of HTTP. gRPC can be used for both synchronous and asynchronous request-response communication.
- Streaming can be achieved using gRPC's streaming capabilities.
- Message passing frameworks are also available for streaming or message-based communication between microservices.
New Section
This section emphasizes that when implementing microservices, developers can focus on the logic of the microservice itself without dealing with networking details. Frameworks and libraries are used to facilitate communication between microservices.
Simplified Microservice Implementation
- Developers can focus on implementing the logic of a microservice without worrying about networking details.
- Frameworks and libraries are utilized to handle communication between microservices.
New Section
This section explains why certain combinations of synchronous streaming and message passing may not make sense in microservice architecture due to blocking issues.
Blocking Issues with Certain Combinations
- Combinations such as client sending a request and receiving a stream of responses or message passing where a microservice sends a message to a channel but doesn't wait for other services to read messages from the channel can lead to blocking issues.
New Section
This section highlights the decision-making process involved in encoding messages and data transferred over the network in microservices.
Encoding Messages and Data
- Microservices need to decide how to encode messages and data transferred over the network.
- Common formats include text-based formats like JSON, CSV, XML, YAML, as well as binary formats like Protocol Buffers (protobuf).
- Text-based formats are editable and easily viewable, while binary formats are usually smaller in size and faster to encode and decode.
New Section
This section discusses the advantages and considerations of using text-based or binary formats for encoding data in microservices.
Text-Based vs Binary Formats
- Text-based formats like JSON and CSV are editable and easily viewable.
- Binary formats have the advantage of smaller size, resulting in less bandwidth usage on the network.
- Binary data is generally faster to operate on compared to text data.
- Language-specific solutions like Python's pickle exist for handling binary data, while language-agnostic solutions like protobufs provide a meta-language for defining data types that can be compiled into various programming languages.
New Section
This section explains the availability of language-specific and language-agnostic solutions for handling binary data in microservices.
Language-Specific vs Language-Agnostic Solutions
- Language-specific solutions like Python's pickle library exist for handling binary data.
- Protobufs provide a language-agnostic solution where a meta-language is used to define data types that can be compiled into different programming languages.