What is Microservices Architecture
Microservices are a design pattern in which applications are composed of small, discrete, independent modules that communicate with each other using well-defined contracts making it easier to develop, test, and deploy isolated parts of your application. Microservices is more about applying a certain number of principles and architectural patterns as architecture. Each micro service lives independently, but on the other hand, it also depend on each other.
Microservices-based applications can scale easily, and are easier to maintain, because we can change the source code of one service of our application independently of all the others, and there is no need to redeploy the entire application.
Advantages
- Scalability: Microservices architecture allows for independent scaling of services based on demand. We can scale individual services horizontally or vertically without affecting other parts of the system.
- Flexibility and Agility: Microservices enable teams to work independently on different services, allowing for faster development cycles. It helps in easy deployment of new features as well. This agility is particularly beneficial in rapidly changing environments.
- Technology Diversity: Developers can use different technologies and languages to develop each microservice, depending on the specific requirements of the service. This allows teams to choose the best tools for the job, promoting innovation and experimentation.
- Fault Isolation: Since each microservice is a separate and independent component, failures in one service isolate themselves and do not affect the entire system. This improves fault tolerance and resilience.
- Ease of Deployment: Teams typically deploy microservices as independently deployable units. It makes it easier to manage and update them without disrupting the complete system. Continuous deployment and DevOps practices suit microservices architectures well.
- Scalable Teams: Microservices enable organizations to organize teams around specific business capabilities or services. This enables smaller, cross-functional teams to take ownership of individual services. It also leads to faster innovation and better alignment with business goals.
Implement a microservice in ASP.NET Core
For the sake of understanding, our microservices based application will comprise three services, namely Employee, Manager, and Company. For brevity, I’ll create the Employee microservice here, and we can follow the same steps to create the other two microservices.
The Employee microservice will include the following types:
- Employee – the entity class
- IEmployeeRepository – the interface for the customer repository
- EmployeeRepository – the customer repository class
- EmployeeController – an API controller that exposes one endpoint to retrieve data.
public class Employee { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } } To create the employee repository, create an interface named IEmployeeRepository in a file having the same name and enter the following code. public interface IEmployeeRepository { public Employee GetById(int id); } The EmployeeRepository class implements the IEmployeeRepository interface and provides an implementation of the GetById method. public class EmployeeRepository : IEmployeeRepository { private readonly List<Employee> _employee; public EmployeeRepository() { _employee = new List<Employee> { new Employee { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Hyderabad, India" }, new Employee { Id = 2, FirstName = "Steve", LastName = "Smith", Address = "Chicago, USA" } }; } public Employee GetById(int id) { return _employee.Find(x => x.Id == id); } }
We should register an instance of type IEmployeeRepository in the Program.cs file using the following piece of code.
builder.Services.AddScoped<IEmployeeRepository, EmployeeRepository>();
This will enable us to use dependency injection to create an instance of type IEmployeeRepository in the controller class. Lastly, we will create a new API controller named Employee and write the following piece of code.
using Microsoft.AspNetCore.Mvc; namespace CustomerAPI.Controllers { [Route("api/[controller]")] [ApiController] public class EmployeeController : ControllerBase { private readonly IEmployeeRepository _employeeRepository; public CustomerController(IEmployeeRepository employeeRepository) { _employeeRepository = employeeRepository; } [HttpGet("{id}")] public Employee Get(int id) { return _employeeRepository.GetById(id); } } }
And that’s all we need to do to create a minimalistic microservice. When we execute the above application and hit the HTTP GET endpoint of the employee microservice, your web browser will display the data related to the employee.
Microservices vs. monolith
Monolithic applications consolidate all of their business functionality in a single process. As a result, all the various functions of the application become intertwined in change cycles, and a minor change to one function might require us to rebuild and redeploy the entire application.
The microservices architecture breaks out the functionality of our application into many independently deployable services, enabling us to build, deploy, and manage each service separately.