Table of Contents
Microservices Interview Questions
1. What is Monolithic Architecture?
Before microservices evolved, all the web-based applications were built with monolithic architectural style. In a monolithic architecture, an application is developed and deployed as a single deployable war(Web ARchive) file. All the user interface, business layer and database are packaged together into a single war file and deployed to an application server. Please refer below diagram.
While an application might be a deployed as a single unit of work, most of the time there will be multiple development teams working on the application. Each development team will have their own discrete pieces of the application they’re responsible for and oftentimes specific customers they’re serving with their functional piece.
2. What are Drawbacks of Monolithic Architecture?
1. Whole Application Fails if single functionality fails
If any single application function or component fails, then the entire application goes down. Imagine a web application with separate functions including payment, login, and history. If a particular function starts consuming more processing power, the entire application’s performance will be compromised.
2. Scaling
Scaling monolithic applications such as the one described in the example can only be accomplished by deploying the same EAR/WAR packages in additional servers, known as horizontal scaling. Each copy of the application in additIonal servers will utilise the same amount of underlying resources, which is inefficient in its design.
3. Deployment
Every time an individual team needed to make a change, the entire application had to be rebuilt, retested and redeployed.
3. What is Microservices Architecture?
a microservice is a small, loosely coupled, distributed service. Microservices allow you to take a large application and decompose it into easy-to–manage components with narrowly defined responsibilities. Microservices help combat the traditional problems of complexity in a large code base by decomposing the large code base down into small, well-defined pieces.
4. What are characteristics of microservices?
5. How micro services interact with each other?
We don’t use RestTemplate to call the micro services as it has a boilerplate code to call and get the response. Spring Cloud helps us and provides the Feign client to do the interactions.
Before Feign client we need to define the RestTempltae object and prepare the arguments explicitly.
Map<String, Long> params = new HashMap<>(); params.put("customer_id", account.getId()); Customer customer = restTemplate.getForObject("http://localhost:8081/customers/{customer_id}" , Customer.class, params); S
With Feign client
@FeignClient(url="localhost:8200" , name="customer-service") public interface CustomerServiceProxy { @GetMapping("/customers") public List<Customer> getAllCustomers(); @GetMapping("/customers/{customer_id}") public Customer getCustomerDetails(@PathVariable("customer_id") Long customer_id); }
Calling feign client
/*Map<String, Long> params = new HashMap<>(); // Part 2 Changes params.put("customer_id", account.getId()); Customer customer = restTemplate.getForObject("http://localhost:8081/customers/{customer_id}" , Customer.class, params);*/ Customer customer = customerServiceProxy.getCustomerDetails(account_id); // Part 3 Changes
Refer below link for more information about Feign client
Feign Client – https://onlyfullstack.blogspot.com/2018/09/part-3-microservices-with-feign-client.html
6. What are the limitations of Feign Client?
Feign client can call only one service specified in its interface. Now what if we have multiple instances of the micro service? We can’t achieve the load balancing. To resolve this issue, Spring Cloud provides the Ribbon Client.
7. What is cleint side load balancing in micro services?What is Ribbon Client?
Feign client can call only one service specified in its interface. Now what if we have multiple instances of the micro service? How do we achieve the load balancing? The answer is Ribbon Client.
Ribbon is a client-side load balancer that gives you a lot of control over the behavior of HTTP and TCP clients. Ribbon is a Inter Process Communication (remote procedure calls) library with built in software load balancers. The primary usage model involves REST calls with various serialization scheme support.
Suppose our customer-service is running on 2 different ports as 8200 and 8201, now using feign client we can only connect to one port that is hardcoded into the @FeignClient annotation as url.
In this scenario RibbonClient will help us to dynamically manage the ports in properties file and also helps to do the load balancing in the available services.
How the ribbon client will identify the available customer-services ? for this we need to add below entry in application.properties file as, With these end points, Ribbon will dynamically distribute the load.
customer-service.ribbon.listOfServers=http://localhost:8200,http://localhost:8201
8. What are the limitations of Ribbon Client? How Spring Cloud resolves them?
Ribbon helps us to do the client side load balancing but what if any of them goes down?
Ribbon does not know which instance of service is down. Now Spring Cloud provides the Eureka Naming server to overcome this problem.
Refer below link for more information about Eureka Naming Server –
Eureka Naming Server – https://onlyfullstack.blogspot.com/2018/09/microservices-with-eureka-naming-server.html
9. What is Naming server in micro services?
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component,the Eureka Client, which makes interactions with the service much easier.
Eureka Naming Server – https://onlyfullstack.blogspot.com/2018/09/microservices-with-eureka-naming-server.html
10. What are advantages of eureka naming server?
2. Handling Fail over of service instances : It will automatically remove the failed service and balance the load to available services.
11. How to implement Eureka Naming Server?
12. What is the entry point for micro services?What is the role of Zuul gateway in micro services?
13. What is the need of Zuul gateway in micro services?
Zuul uses a range of different types of filters that enables us to quickly and nimbly apply functionality to our edge service. These filters help us perform the following functions:
- Authentication and Security – identifying authentication requirements for each resource and rejecting requests that do not satisfy them.
- Insights and Monitoring – tracking meaningful data and statistics at the edge in order to give us an accurate view of production.
- Dynamic Routing – dynamically routing requests to different backend clusters as needed.
- Stress Testing – gradually increasing the traffic to a cluster in order to gauge performance.
- Load Shedding – allocating capacity for each type of request and dropping requests that go over the limit.
- Static Response handling – building some responses directly at the edge instead of forwarding them to an internal cluster
- Multiregion Resiliency – routing requests across AWS regions in order to diversify our ELB usage and move our edge closer to our members
Refer below URL to know more about the Zulu gateway –
14. How to do the distributed tracing in micro services?What is Spring Cloud Sleuth?How to log the unique identifier for each request to trace?
Lets first understand the terminologies used in sleuth and zipkin
https://cloud.spring.io/spring-cloud-static/spring-cloud-sleuth/2.0.0.M9/single/spring-cloud-sleuth.html#_only_sleuth_log_correlation
One request will travel from different applications to process and It becomes very difficult to trace the logs as they will be logged in diffrent log files with no common identifier to identify the request. Spring cloud sleuth helps us by adding an identifier to all the logs.
15. What is Zipkin?How to log the distributed logs at one place in microservices?
Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in microservice architectures. It manages both the collection and lookup of this data. Zipkin’s design is based on the Google Dapper paper.
Applications are instrumented to report timing data to Zipkin. The Zipkin UI also presents a Dependency diagram showing how many traced requests went through each application. If you are troubleshooting latency problems or errors, you can filter or sort all traces based on the application, length of trace, annotation, or timestamp. Once you select a trace, you can see the percentage of the total trace time each span takes which allows you to identify the problem application.
Till now we have configured sleuth which was giving the trace id to find out the logs from different applications. But the problem with sleuth is its distributed and it becomes very difficult to check all log files one by one to check the logs. Zipkin will solve this problem by giving the central access to all the distributed logs.
16. How to handle fault tolerance in microservices?What is Hystrix?
Hystrix is based on the Circuit Breaker Pattern.
Very nice
Good explanation
very helpfull thanks