A load balancer accepts incoming traffic from clients and routes requests to its registered targets (such as EC2 instances) in one or more Availability Zones. The load balancer also monitors the health of its registered targets and ensures that it routes traffic only to healthy targets. When the load balancer detects an unhealthy target, it stops routing traffic to that target. It then resumes routing traffic to that target when it detects that the target is healthy again.
You configure your load balancer to accept incoming traffic by specifying one or more listeners. A listener is a process that checks for connection requests. It is configured with a protocol and port number for connections from clients to the load balancer. Likewise, it is configured with a protocol and port number for connections from the load balancer to the targets.
Elastic Load Balancing supports the following types of load balancers:
Application Load Balancers
Network Load Balancers
Gateway Load Balancers
Classic Load Balancers
There is a key difference in how the load balancer types are configured. With Application Load Balancers, Network Load Balancers, and Gateway Load Balancers, you register targets in target groups, and route traffic to the target groups. With Classic Load Balancers, you register instances with the load balancer.
Count Min Sketch The algorithm is used for data which is coming as a stream and we want to have a fast processing. We will have multiple hash functions and will have a table where we store the output of the values hashes. If we need to find the occurrence of the value we run the values through the hash, get the stored value and then take the minimum of those values.
Sharding helps in spreading out the data in different machines in case the data volume to too high. Sharding has its own disadvantages like increase maintenance, query result aggregation, increased infrastructure cost.
Before considering sharding we should consider if vertical scaling will solve the issue. If yes then should go for vertical scaling rather than Sharding.
If you are preparing for an interview or just trying to get started with a Microservice then you should be aware of some of the design patterns which can be used to make your microservices resilient and useful.
Principles Used to Design Microservice Architecture
The principles used to design Microservices are as follows:
Independent & Autonomous Services
Scalability
Decentralization
Resilient Services
Real-Time Load Balancing
Availability
Continuous delivery through DevOps Integration
Seamless API Integration and Continuous Monitoring
Isolation from Failures
Auto -Provisioning
These are some of the Microservices Design Patterns
Retry Pattern In this pattern we try to retry to request to another microservice for certain number of time in case there is a failure before return a failure status to the client.
Circuit Breaker Pattern In this pattern the microservices will keep track on the number of times a service returns failure. In case the number of failures goes above a threshold then the circuit opens and starts returning error for the calling service. The circuit breaker will keep on checking the failing service. Once we get a success then the circuit will close again so that the traffic can go to the service which was failing previously.
Bulkhead Pattern The patterns helps in isolating the services within a microservice from failure.
Aggregator Pattern The pattern is used to get the response from various microservices and the aggregate the result together.
API Gateway The request first goes to the API gateway which routes the request to respective service based on the API Gateway.
Chained or Chain of Responsibility Chained or Chain of Responsibility Design Patterns produces a single output which is a combination of multiple chained outputs. So, if you have three services lined up in a chain, then, the request from the client is first received by Service A. Then, this service communicates with the next Service B and collects data. Finally, the second service communicates with the third service to generate the consolidated output.
Asynchronous Messaging This pattern is used for asynchronous messaging between different microservices. We can add a messaging queue between the services. This pattern is helpful if we have a lot of requests coming and the service might need some time to process the request.
Event Sourcing The event sourcing design pattern creates events regarding the changes in the application state. Also, these events are stored as a sequence of events to help the developers track which change was made when. So, with the help of this, you can always adjust the application state to cope up with the past changes. You can also query these events, for any data change and simultaneously publish these events from the event store.
Command Query Responsibility Segregation According to this pattern, the application will be divided into two parts: Command and Query. The command part will handle all the requests related to CREATE, UPDATE, DELETE while the query part will take care of the materialized views.
Branch Branch microservice design pattern is a design pattern in which you can simultaneously process the requests and responses from two or more independent microservices.
Saga Pattern This pattern helps with implementing a business transaction that spans multiple services. A saga is a sequence of local transactions. Each local transaction updates the database and publishes a message or event to trigger the next local transaction in the saga. If a local transaction fails because it violates a business rule then the saga executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.
There are two ways of coordination sagas:
Choreography - each local transaction publishes domain events that trigger local transactions in other services
Dynamic Programming - Optimization of the solution.
1. Greedy Algorithm - In a greedy algorithm the decision is taken once and the result is calculated based on the same decision. Like Dijastra's Algorithm where we always take the shortest vertex to find the shortest path. Other example Minimum cost spanning tree - Always select the minimum cost edge.
2. Dynamic Programming - The decision is taken at every step to get to an optimal solution. Memoization is a technique used to optimize the time complexity. In the technique we store the result of the calculation so that we can use them again. It is a top down approach. This is used in Recursion.
Tabulation is a technique which is a bottom up approach. We start with the smallest value and calculate the bigger problems. It is used as part of the iterative method
Domain-Driven Design is about creating shared understanding of the problem space that is reinforced ubiquitously via conversations, code and diagrams. Things to know in Domain Driven Design
What is Bounded Context ? Bounded context defines tangible boundaries of applicability of some sub-domain. It is an area where a certain sub-domain makes sense, while the others don't. It can be a talk, a presentation, a code project with physical boundaries defined by the artifact.
What is Ubiquitous Language ? A Bounded Context is an explicit boundary within which a domain model exists. Inside the boundary all terms and phrases of the Ubiquitous Language have specific meaning, and the model reflects the Language with exactness.
WebSockets, on the other hand, allow for sending message-based data, similar to UDP, but with the reliability of TCP. WebSocket uses HTTP as the initial transport mechanism, but keeps the TCP connection alive after the HTTP response is received so that it can be used for sending messages between client and server.
Factory Pattern - Let the factory do the job of creating the objects rather than instantiating the object using New https://java-design-patterns.com/patterns/factory/
Abstract Factory Pattern - Creating a Factory from list of factories. https://java-design-patterns.com/patterns/abstract-factory/
Builder Pattern - The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern. Telescoping constructor anti-pattern means that the constructor is getting bigger and bigger due to addition of a lot of parameters. https://java-design-patterns.com/patterns/builder/
Prototype Pattern - Clone the existing object and then make modification to the object rather than creating the object from scratch. https://java-design-patterns.com/patterns/prototype/ In java can be done by implementing the Cloneable and overriding the Clone() method. But Clonable makes a shallow copy. We need to override the clone method for the child objects and the clonable method should be updated
Advantages: Help in
scalability, performance and availability
Composing Microservices
Composition
Patterns
Broker
Composition Pattern
Aggregate Composition
Pattern
Chained Composition Pattern
Proxy Composition Pattern
Branch Composition Pattern
Data Consistency
Two Phase Commit
Saga Pattern-Saga
Execution Coordinator
Eventual Consistency :
Eventually the Data would be consistent across the systems. It is a
practice rather than pattern. It chooses Availability over Consistency.