We cannot deny the importance of Microservices in the current market scenario and there are good reasons for this. Microservices are providing faster software, more innovation, better software at a low cost. Therefore more and more software engineers are trying to make a career in Microservices. Here in this article, we have listed frequently asked Microservices interview questions.
We have divided the Microservices interview questions into the below sections:
- Spring Boot
Microservices Interview Questions: Basic
What do you understand by Microservices?
Microservices is an architecture and approach where many small services constitute a large and complex application. One service has a single and precise functionality to perform equipped with a defined interface to interact with other services. These small services can be independently deployed, maintained, tested, and are scalable. A single small service/ module/ process is a microservice.
The Microservices architecture is built around a business domain where the services communicate with each other. Different teams have ownership of different services/ modules.
What are the advantages of Microservices?
- Independently deployable small services
- Independently developed small services
- The use of different technologies results in technology flexibility
- Scaling of single service as per need provides independent scalability
- Faster deployment of small modules results in faster delivery
- Easy to understand small modules
- Easier maintenance of individual services
- Lightweight messaging makes data communication and coordination between modules easy. E.g. REST API
- If one of the services fails in the application other services keep running. The amendment can be made only in the failed service without any impact on the other services hence it provides failure minimisation.
Explain the architecture of Microservices
The Microservices architecture has the below four main components:
- API layer: This layer is the entry of all types of requests from clients. This layer enables the services to interact with each other through HTTP, TCP UDP protocols.
- Logic layer: The logic of the application is present in this layer to provide resolution of the business requirement. It can be written in different programming languages for different microservices.
- Database layer: Each microservice has its own database storage.
- Container orchestration: To maintain the Microservices as separate entities they are run in their own containers.
Explain the important features of Microservices
- Loosely coupled: Microservices as independent and connected to each other through messaging systems e.g. REST, HTTP, etc.
- Reusable: The same microservice can be used for a mobile client, desktop client, another microservice, or some other system leading to reusability.
- Stateless: Microservices do not maintain or store state. It means that they take a request from the client, process it, and then send the response. However, we can deliberately store a state in case of a ‘stateful microservice’.
- Lightweight: Microservices are small and use lightweight containers to hold them. Also, for communication, they use lightweight messaging protocols.
- Polyglot: The logical layer or the programming can be done in any programming language.
What are the common challenges during Microservices implementation?
- Diverse technologies: We use different technologies for different Microservices. While maintaining this may cause issues because the team member performing maintenance may not be aware of the technology used.
- Complex communication: In a Microservice architecture, multiple independent services communicate with each other. Therefore the dependencies are coded into the infrastructure configuration. Hence, the system communication can be very complex to maintain.
- Testing: Each team focuses on their individual microservice and tests them as a separate entity. Hence, the integration testing of the whole Microservice application is complex and often goes missing.
- Management: With an increase in the number of Microservices, managing them could be a challenge.
- Monitoring: In distributed systems like Microservices the root cause of an issue is spanned across multiple Microservices. In such a case, it is challenging to find the root cause of the problem. To find the issue we need proper monitoring and tracking of the request. We need special tools which will help us to understand the dependencies and correlation of services within the whole system.
- Fault tolerance: It is important to design an application for fault tolerance. To prepare the Microservices for external and internal failures is challenging due to the complex architecture.
- Migration: The process of migration from monolithic to a microservices-based application requires better planning. Because we will introduce new networks and connections, also the tasks will be broken into smaller modules.
- Complex operations: A microservice-based application needs testing in several testing environments and then subsequently in production. The recovery, security, and maintenance are challenging for Microservices.
Name and explain various popular tools used for creating Microservices architecture
- Operating System: Linux
- Programming language: Java, Python, Elixir
- Orchestration: Kubernetes, Swarm
- API management and testing: Postman, API Fortress, Tyk
- Messaging: RabbitMQ, Amazon Simple Queue Service (SQS), Apache Kafka
- Monitoring: Prometheus, Logstash, Graylog
- Containers: Docker
- Toolkits: fabric8, Seneca
- Architectural Framework: goa, Kong
- Serverless tools: Serverless, Kubeless, AWS Lambda, Claudia
Microservices Interview Questions: Concepts
Explain the differences between Microservices, Monolithic, and SOA.
What are Coupling and Cohesion in Microservices?
Microservices has small services serving as individual modules and to communicate with each other they use protocols. In a poor design, they may be excessively dependent on each other with a lot of communication happening between them. Another case could be where the methods of the same module are working in isolation with each other without any communication. We would want to avoid both situations.
Coupling: It is the degree to which the modules are dependent, interacting with each other. Hence, coupling works across modules. It can be achieved by designing good interface boundaries.
Cohesion: It is the degree to which the methods/ elements of a module serve a common purpose together. Hence, cohesion works within a module.
An ideal Microservices architecture will aim for high cohesion and low coupling to achieve below:
- Related code/methods should be present in the same module.
- Each module should work independently with minimum required communication between them.
Microservices Interview Questions: Spring Boot
This section contains Spring Boot microservices interview questions together with Spring Cloud, Spring Security, exception handling, and logging.
What do you know about Spring Boot?
Spring boot is a Java-based, open-source framework which helps in creating production-ready and stand-alone applications. Spring boot applications can be written without the need of setting up the whole Spring configuration. Therefore, Spring Boot is easier to understand, faster to develop, and increases productivity.
More about Spring Boot here.
What is Spring Cloud in Microservices?
It is an open-source module of Spring which helps in quickly developing and deploying the distributed applications on the cloud. Spring Cloud in Microservices provides tools to create patterns for distributed systems rapidly. E.g., service discovery, circuit breakers, configuration management, intelligent routing, etc.
More about Spring Cloud here.
How to implement Spring Security into a Spring Boot application?
To secure the application from unauthorised access we use Spring Security in microservices. To configure Spring security into an application we add the Spring Security on the classpath. This way Spring Boot will automatically secure all HTTP endpoints with basic security. Spring Security in Microservices can be further changed by making enhancements to the security settings.
- Maven users can define POM dependencies as below in pom.xml.
- Gradle dependencies are defined as below in the build.gradle file.
Step 2: Create a config class which extends WebSecurityConfigurerAdapter and overrides its methods -configure() , userDetailService() etc.
Complete code snippets here.
How to perform exception handling in Microservices?
In case of an exception in service, we need to properly handle it and return a response to convey details about the exception.
ExceptionHandler for Controller based exceptions
It is a Spring annotation which helps us in handling the exceptions thrown during the execution of Controller operations. This can be used on methods of @ControllerAdvice classes to apply an ExceptionHandler on multiple controllers. It can be applied to all the controllers or a subset of them.
ResponseEntityExceptionHandler class of Spring
It provides basic exception handling methods, we can override them to provide a more sophisticated error response.
We can create new methods for exception handling using ResponseEntityExceptionHandler as our base class.
Microservices Interview Questions: Security
What is the Open Authorisation Protocol?
Open Authorisation Protocol or OAuth is an authorisation protocol which has two versions – OAuth 1.0 and OAuth 2.0. It is an open standard for interacting with protected data on the internet. Through OAuth protocol end-user’s account information is used by third-party applications like Facebook without exposing the user’s credentials.
OAuth 1.0 works on cryptography and therefore it is difficult to implement by the programmers. OAuth 2.0 is easier to implement but due to the use of HTTP/TLS the security can be compromised. Hence it becomes important to work on security features properly while using OAuth 2.0.
Example of OAuth authorisation protocol: Google API’s via OAuth 2.0.
Microservices Interview Questions: API
Why use RESTful API in Microservices?
An API(Application Programming Interface) defines the rules, protocols, commands, permissions for the users/applications to access the data or service. In the case of Microservices API define the rules for data sharing, access permissions, and other commands. RESTful API is HTTP based type of API which is widely used in Microservices.
RESTful API in Microservices acts as a glue to hold the different services of the application together.
RESTful API is used in Microservices because:
- It provides the means of communication between services.
- It is a way of accepting some inputs, performing a complex calculation, and returning the result.
- It is language independent.
- It is a stateless protocol.
- Works on CRUD with simple commands:
Create: POST command
Read: GET command
Update: PUT command
Delete: DELETE command
What is Idempotence in Microservices?
If an action(call) is repeated multiple times and the server state(or result) remains the same after every call then this action is idempotent. It means that after one call or multiple calls we receive the same result.
It is important that Microservices must have the idempotence property because there are scenarios where APIs get duplicate requests. In such scenarios, the system should be fault-tolerant and should not give erroneous results.
E.g. REST requests- GET, HEAD, OPTIONS, TRACE are idempotent because they do not change the resource state at the server. They are just accessing the existing state/ data.
REST request POST is NOT idempotent, because every time we POST a request we are making changes in the server state by adding new resources. Hence, the result of every POST request will differ.
Microservices is used by most of the technology and social media giants today. A career in microservices can be really rewarding and therefore we have compiled the most frequently asked questions for easy preparation. Here we conclude the article and hope that you find it helpful in preparing for microservices interviews.