Junior — Middle
How do you organize data transfer through protocols between microservices in your practice?
sobes.tech AI
Answer from AI
In practice, data transfer between microservices often uses protocols such as HTTP/REST, gRPC, or message brokers (e.g., Kafka, RabbitMQ).
For HTTP/REST, data is usually transmitted in JSON or Protobuf format. An example in Go using an HTTP client:
package main
import (
"bytes"
"encoding/json"
"net/http"
)
type Payload struct {
ID int `json:"id"`
Name string `json:"name"`
}
func sendData(url string, data Payload) error {
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
For gRPC, protobuf schemas are created and generated clients and servers are used, which ensures efficient binary transmission and strict typing.
When using message brokers, data is sent to a queue and consumed asynchronously, which increases system resilience and scalability.