- Published on
Microservice
- Authors
- Name
- Bowen Y
Monolithic VS Microservice
In a monolithic architecture, the components interact directly within a single application instance, often sharing data in-memory or through internal function calls.
In contrast, in a microservices architecture, different services are typically deployed on separate servers or environments and communicate through network calls, often using HTTP requests.
Scalability
- Monolithic`: Scaling requires scaling the entire application, even if only one part of the application needs more resources.
- Microservice: Individual components can be scaled as needed, without affecting the entire application.
Data Transfer:
- Monolithic: In a monolithic application, components typically communicate with each other through function calls, method invocations, or shared memory. This internal communication is often straightforward since all components are part of the same codebase.
- Performance: Since communication is internal and direct, it tends to be faster with lower latency, which is beneficial for operations requiring quick data access and manipulation.
- Microservice: In a microservices architecture, services communicate with each other using well-defined APIs, often over a network. This communication is typically based on lightweight protocols such as HTTP/REST or message queues.
- Performance: Network communication introduces latency. Also, ensuring data consistency across services can be challenging and might require implementing patterns like Saga for distributed transactions.
Data Transfer
- API Calls (Synchronous Communication):
- Microservices communicate with each other using APIs, typically over HTTP/HTTPS. REST (Representational State Transfer) is a common choice for API design, but some systems might use other methods like GraphQL or gRPC.
- RESTful APIs: a request-response model where one service sends a request, and the receiver sends back a response.
- gRPC: especially in performance-critical systems, is using gRPC, which allows for efficient binary communication based on Protocol Buffers
- Communication is often stateless, especially in RESTful services. Each request from one service to another contains all the information needed to understand and process the request.
- Data Format
- The data transferred between services is typically in a lightweight, easy-to-parse format like JSON or XML.
- Asynchronous Communication
- To decouple services and enhance performance, microservices often communicate asynchronously. This means one service sends a message without waiting for an immediate response from the other service.
- Asynchronous communication can be implemented using message queues, event streams, or other messaging systems like Kafka, RabbitMQ, or AWS SQS.
- Message Queues:
- Event Streaming:
- Service Discovery
- In microservices, services need to discover each other dynamically. Service discovery mechanisms allow services to find and communicate with each other in a distributed environment. This can be done through service registries and service discovery tools.
- Load Balancing and Fault Tolerance
- Microservices architectures often implement load balancers to distribute requests efficiently across multiple instances of a service.
- They also use circuit breakers and other fault tolerance patterns to prevent failures in one service from cascading to others.
- API Gateway
- An API Gateway is often used as a single entry point for client requests, which are then routed to the appropriate microservice. It can handle cross-cutting concerns like authentication, SSL termination, and rate limiting.
- Circuit Breakers and Retry Mechanisms
- To maintain stability, microservices architectures often implement patterns like circuit breakers (using tools like Hystrix). This prevents a failing service from causing a cascading failure across other services.
- Distributed Tracing and Monitoring
- With multiple services communicating, it's important to have distributed tracing (like Zipkin or Jaeger) to track requests across services. Monitoring and logging (using tools like Prometheus or ELK stack) are also crucial to observe the health and performance of services.