Sobes.tech
Middle+

Tell about microservices interaction.

sobes.tech AI

Answer from AI

Microservice interaction can be:

  • Synchronous: The sender waits for a response from the receiver. Examples: REST, gRPC.
  • Asynchronous: The sender does not wait for an immediate response. Examples: Message queues (Kafka, RabbitMQ), Event Bus.

Main interaction methods:

  1. RESTful API:

    • Protocol: HTTP.
    • Data format: JSON, XML.
    • Interaction type: Synchronous.
    • Advantages: Simplicity, broad support, easy to debug.
    • Disadvantages: Tight coupling, thread blocking during long processing on the other end.
  2. gRPC:

    • Protocol: HTTP/2.
    • Data format: Protocol Buffers.
    • Interaction type: Synchronous (streaming scenarios possible).
    • Advantages: High performance, strictly typed contracts, stream support.
    • Disadvantages: Code generation required, harder to debug without special tools.
  3. Message Queues:

    • Examples: Apache Kafka, RabbitMQ, ActiveMQ.
    • Interaction type: Asynchronous.
    • Models: Publisher/Subscriber, Point-to-Point.
    • Advantages: Loose coupling, scalability, fault tolerance, reprocessing capability, load management.
    • Disadvantages: Increased infrastructure complexity, delay due to asynchrony, challenges in debugging distributed transactions.
  4. Event Bus (Event-Driven Architecture):

    • Services publish events that other services can subscribe to and react.
    • Interaction type: Fully asynchronous.
    • Advantages: Very loose coupling, high flexibility.
    • Disadvantages: Difficulty in tracking event flow, state management in distributed systems, debugging challenges.

The choice of method depends on requirements for performance, reliability, fault tolerance, service coupling degree, and interaction logic complexity. Often, combinations of different approaches are used. For example, REST for data requests and Kafka for event notifications.

Additional aspects:

  • Service Discovery: Mechanism for locating service instances (e.g., Eureka, Consul, Zookeeper, Kubernetes DNS). Allows services to find each other without hard IP bindings.
  • API Gateway: Single entry point for external clients. Can perform authentication, authorization, routing, request aggregation, monitoring, load balancing.
  • Circuit Breaker: Pattern to prevent cascading failures in synchronous calls. If a remote service is unavailable or responds with errors, the Circuit Breaker "breaks" the connection for a period.
  • Idempotency: Important in asynchronous interactions or retries. An operation is idempotent if multiple executions with the same parameters produce the same result as a single execution.
  • Distributed Transactions: A complex topic in microservices. Patterns include Saga (coordinating local transactions, compensatory actions), Two-Phase Commit (complex and often not recommended in microservices).