THE MODERN RULES OF MICROSERVICES

What are microservices?

Let's understand first the process of traditional application development. In this scenario when the functionality increases we add more code so the size of the application increases gradually and it becomes a complex application which creates a problem for developers in identifying the root cause in case any issue occurred. 

No alt text provided for this image

So here modular structure comes in to picture, Developers, split the large complex application into small modules and compose the application with all the small independent modules which will be doing a small functionality of the application. These modules also can be reusable so that other projects also can make use of this. Once you have done with all the modules you need to deploy your application into the modules.

No alt text provided for this image


Here also we have problems when one of the modules of our application has any issue. In this case, instead of deploying it into the user's machine, it can be deployed into a server.

Web applications have been growing very complicated over time because of the functionality and the way we need our business. There are several application in the world now which are really complex but it really works incredibly. The complexity really becomes harder and harder to maintain. Now we have nice enough modular architecture is that not enough to handle the complexity ?? No one cares how the application is really deployed. 

For the complexity, we have to handle it not only coding side but it can be handled on the runtime execution side also having a single thing deployed. The single thing is called 'Monolithic Architecture'.

 But we have few disadvantages of monolithic architecture 

  • More challenging to deploy a big application (Even if a small code changes for a small functionality need to deploy the entire application, Even if you need to test the entire application before you deploy)
  • Scalability Issue (Some sites will have traffic spikes for a certain period like e-commerce applications).In this case when the traffic increases the servers also have to be increased but in this case, all the requests come to the same server for all functionality.

While we are having a desktop application we have to install into our system we don't have any choice. But when we have a web application we have a server we really don't care where the server is and how it's deployed we just need to go to the main application to talk to.

Now here is the idea let's separate the monolithic application into several small mini-application and deploy it on to separate server. We can have them talk to each other over the network and together work as a large application. Now here we can deploy and test separately for each application.

 Even if scaling also will not have any problem during high traffic only specific applications will be scaled up so the servers for the small application can be scaled up.

 These mini-applications are technically called 'Microservices'. Microservices are the way of breaking your application into a standalone independent application that can be run on different hardware or server instances. They all can talk to each other over the rest APIs and work together as a big application to provide the functionality of your entire application.

Advantages of Microservices :

  • Deployment Flexibility
  • Technology Flexibility
  • Can be scaled separately 

Disadvantages of Microservices :

  • Deployment complexity 
  • Service discovery

What is service discovery all about?

When we build an application with microservices we need to think about how to do those microservices talk to each other. When we say our microservices should talk to each other now the question comes to our mind how can one microservice talk to another microservice??. How does one microservice know where is the other microservice is ??

   One thing we can do is we can hardcode the URL but that's not a good idea for multiple reasons like what will happen when URL changes and when we have multiple instances, So we need a better approach which is "Service discovery".

 The concept behind service discovery is very simple to suppose you creating multiple microservices which has to be accessed by other microservices. So now what we can do is we will create a new microservice as Discovery Server and its job is to keep track of where all our microservices locates. 

So all microservices have to be registered in this discovery server. Now when anyone needs to access the service can ask the discovery server for the microservice location. The process of asking the discovery server to get the microservice location is called Service discovery we can say it is service location discovery also. 

  The service discovery works in two ways client-side service discovery and server-side service discovery. When a client looks to the discovery server and finds the location of the service and the client makes a direct call to the required service center is called as client-side service discovery. 

Ex: Eureka 

No alt text provided for this image

 

Here also we have disadvantages because the client has to make two calls.

Similarly when a client makes a request to the discovery server and the discover server takes care of the entire process to send your request to the proper location is called server-side service discovery. 

No alt text provided for this image

  In this case, we need to have a load balancer and service registry to maintain the server running all the time so that it can respond anytime when the client requested. Based on our requirement we can use either client-side or server-side service discovery. Ex: AWS 

What is API gateway in microservices?

 This is an entry point for external clients which is not really part of our microservice. When we have multiple microservices and all are doing some functionalities. The consumer of the microservices really need to call all the microservices to cover the entire functionality it became difficult for the consumer to call each time for all functionality. 

   To avoid this we can have an abstraction layer in between which will redirect the request based on the requirement and respond to the consumer with proper information. The abstraction layer is called API Gateway.

 Steps for API Gateway

  • First, we need to identify our external API (Front end API/Consumer) like what are those API can be accessed by other people.
  • Then we need to bring that microservice that will act as a gateway.
  •  Now since we have a single entry point so all our requests will go through it. We can do so many things here like we can monitor how many requests are coming in and how long it is taking also.

 We can do authentication here also like we can do token validation JWT and can measure security measures, we can prevent the external APIs. There are lots of open-source API gateway implementations we can use. 

Disadvantages


  • It will be slow because a single entry point can haveing multiple API gateways.
  • since its a single system what if it goes down 
  • If we keep multiple gateways it may create little confusion.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了