Comprehensive guide for migration from monolithic to microservices architecture
The architecture of server-side application development for complex and large applications (applications with huge number of users and large volume of data) shouldn’t just involve faster response and providing web services for wide variety of platforms. It should be easy to scale, upgrade, update, test, and deploy. It should also be highly available, allowing the developers write components of the server- side application in different programming languages and use different databases.
Therefore, this leads the developers who build large and complex applications to switch from the common monolithic architecture to microservices architecture that allows us to do all this easily. As microservices architecture is being widely used in enterprises that build large and complex applications, it’s really important to learn how to design and create server-side applications using this architecture.
In this article, we will review microservices architecture concepts and discuss the pros and cons of microservices versus monolithic application designs. This article followed by two tutorials: i- create applications based on microservices architecture with Node.js using the Seneca toolkit, and ii- develop server-side applications using microservices with Seneca js
In this article, we’ll cover the following topics:
- Understanding monolithic architecture
- Scaling, upgrading, deploying, and writing applications based on monolithic architecture
- Discussing microservices architecture in depth
- Scaling, upgrading, deploying, and writing applications based on microservices architecture
- Getting started with Seneca
- Creating and calling services using Seneca
What is monolithic architecture?
To understand microservices architecture, it’s important to first understand
monolithic architecture, which is its opposite.
In monolithic architecture, different functional components of the server-side application, such as payment processing, account management, push notifications, and other components, all blend together in a single unit.
For example, applications are usually divided into three parts. The parts are HTML pages or native UI that run on the user’s machine, server-side application that runs on the server, and database that also runs on the server. The server-side application is responsible for handling HTTP requests, retrieving and storing data in a database, executing algorithms, and so on. If the server-side application is a single executable (that is, running is a single process) that does all these tasks, then we say that the server-side application is monolithic.
This is a common way of building server-side applications. Almost every major CMS, web servers, server-side frameworks, and so on are built using monolithic architecture.
This architecture may seem successful, but problems are likely to arise when your application is large and complex.
Demerits of monolithic architecture
The following are some of the issues caused by server-side applications built using the monolithic architecture.
Scaling monolithic architecture
As traffic to your server-side application increases, you will need to scale your
server-side application to handle the traffic.
In case of monolithic architecture, you can scale the server-side application by running the same executable on multiple servers and place the servers behind a load balancer or you can use round robin DNS to distribute the traffic among the servers:
In the preceding diagram, all the servers will be running the same server-side application.
Although scaling is easy, scaling monolithic server-side application ends up with scaling all the components rather than the components that require greater resource. Thus, causing unbalanced utilization of resources sometimes, depending on the quantity and types of resources the components need.
Let’s consider some examples to understand the issues caused while scaling monolithic server-side applications:
- Suppose there is a component of server-side application that requires a more powerful or special kind of hardware, we cannot simply scale this particular component as all the components are packed together, therefore everything needs to be scaled together. So, to make sure that the component gets enough resources, you need to run the server-side application on some more servers with powerful or special hardware, leading to consumption of more resources than actually required.
- Suppose we have a component that requires to be executed on a specific server operating system that is not free of charge, we cannot simply run this particular component in a non-free operating system as all the components are packed together and therefore, just to execute this specific component, we need to install the non-free operating system on all servers, increasing the cost greatly.
These are just some examples. There are many more issues that you are likely to come across while scaling a monolithic server-side application.
So, when we scale monolithic server-side applications, the components that don’t need more powerful or special kind of resource starts receiving them, therefore deceasing resources for the component that needs them. We can say that scaling monolithic server-side application involves scaling all components that are forcing to duplicate everything in the new servers.
Writing monolithic server-side applications
Monolithic server-side applications are written in a particular programming language using a particular framework. Enterprises usually have developers who are experts in different programming languages and frameworks to build server- side applications; therefore, if they are asked to build a monolithic server-side application, then it will be difficult for them to work together.
The components of a monolithic server-side application can be reused only in the same framework using, which it’s built. So, you cannot reuse them for some other kind of project that’s built using different technologies.
Other issues of monolithic architecture
Here are some other issues that developers might face, depending on the technology that is used to build the monolithic server-side application:
- It may need to be completely rebuild and redeployed for every small change made to it. This is a time-consuming task and makes your application inaccessible for a long time.
- It may completely fail if any one of the components fails. It’s difficult to build a monolithic application to handle failure of specific components and degrade application features accordingly.
- It may be difficult to find how much resources are each components consuming.
- It may be difficult to test and debug individual components separately.
Intro to Microservices architecture
We saw the problems caused by monolithic architecture. These problems lead developers to switch from monolithic architecture to microservices architecture.
In microservices architecture, the server-side application is divided into services.
A service (or microservice) is a small and independent process that constitutes a particular functionality of the complete server-side application. For example, you can have a service for payment processing, another service for account management, and so on; the services need to communicate with each other via a network.
You don’t have to run each service in a different server, that is, you can run multiple services in a single computer. The ratio of server to services depends on different factors. A common factor is the amount and type of resources and technologies required. For example, if a service needs a lot of RAM and CPU time, then it would be better to run it individually on a server. If there are some services that don’t need much resources, then you can run them all in a single server together.
The following diagram shows an example of the microservices architecture:
Here, you can think of Service 1 as the web server with which a browser communicates and other services providing APIs for various functionalities. The web services communicate with other services to get data.
Pros of microservices architecture
Due to the fact that services are small and independent and communicate via network, microservices architecture solves many problems that monolithic architecture had. Here are some of the benefits of microservices architecture:
- As the services communicate via a network, they can be written in different programming languages using different frameworks
- Making a change to a service only requires that particular service to be redeployed instead of all the services, which is a faster procedure
- It becomes easier to measure how much resources are consumed by each service as each service runs in a different process
- It becomes easier to test and debug, as you can analyze each service separately
- Services can be reused by other applications as they interact via network calls
Scaling services
Apart from the preceding benefits, one of the major benefits of microservices architecture is that you can scale individual services that require scaling instead of all the services, therefore preventing duplication of resources and unbalanced utilization of resources.
Suppose we want to scale Service 1 in the preceding diagram. Here is a diagram that shows how it can be scaled:
Here, we are running two instances of Service 1 on two different servers kept behind a load balancer, which distributes the traffic between them. All other services run
the same way, as scaling them wasn’t required. If you wanted to scale Service 3, then you can run multiple instances of Service 3 on multiple servers and place them behind a load balancer.
Cons of microservices architecture
Although there are a lot of pros of using microservices architecture compared to monolithic architecture, there are some cons of microservices architecture as well:
- As the server-side application is divided into services, deploying, and optionally, configuring each service separately is a cumbersome and time-consuming task.
- Communication between services is likely to lag as it’s done via a network.
- This sort of server-side applications more prone to network security vulnerabilities as services communicate via a network.
- Writing code for communicating with other services can be harder, that is, you need to make network calls and then parse the data to read it. This also requires more processing. Note that although there are frameworks to build server-side applications using microservices that make fetching and parsing data easier, it still doesn’t deduct the processing and network wait time.
- You will surely need some sort of monitoring tool to monitor services as they may go down due to network, hardware, or software failure. Although you may use the monitoring tool only when your application suddenly stops, to build the monitoring software or use some sort of service, monitoring software needs some level of extra experience and expertise.
- Microservices-based server-side applications are slower than monolithic- based server-side applications as communication via networks is slower compared to memory.
When to use microservices architecture
It may seem like its difficult to choose between monolithic and microservices
architecture, but it’s actually not so hard to decide between them.
If you are building a server-side application using monolithic architecture and you feel that you are unlikely to face any monolithic issues that we discussed earlier, then you can stick to monolithic architecture. In future, if you are facing issues that can be solved using microservices architecture, then you should switch to microservices architecture.
If you are switching from a monolithic architecture to microservices architecture, then you don’t have to rewrite the complete application, instead you can only convert the components that are causing issues to services by doing some code refactoring. This sort of server-side applications where the main application logic is monolithic but some specific functionality is exposed via services is called microservices architecture with monolithic core. As issues increase further, you can start converting more components of the monolithic core to services.
If you are building a server-side application using monolithic architecture and you feel that you are likely to face any of the monolithic issues that we discussed earlier, then you should immediately switch to microservices architecture or microservices architecture with monolithic core, depending on what suits you the best.
Data management
In microservices architecture, each service can have its own database to store data and can also use a centralized database.
Some developers don’t use a centralized database at all, instead all services have their own database to store the data. To synchronize the data between the services, the services omit events when their data is changed and other services subscribe to the event and update the data. The problem with this mechanism is that if a service is down, then it may miss some events. There is also going to be a lot of duplicate data, and finally, it is difficult to code this kind of system.
Therefore, it’s a good idea to have a centralized database and also let each service to maintain their own database if they want to store something that they don’t want to share with others. Services should not connect to the centralized database directly, instead there should be another service called database service that provides APIs to work with the centralized database. This extra layer has many advantages, such as the underlying schema can be changed without updating and redeploying all the services that are dependent on the schema, we can add a caching layer without making changes to the services, you can change the type of database without making any changes to the services and there are many other benefits. We can also have multiple database services if there are multiple schemas, or if there are different types of database, or due to some other reason that benefits the overall architecture and decouples the services.
Summary
In this article, we introduced advance web development concepts like microservices and reviewed its architecture. Then, we moved on comparing its architecture and functionalities with monolithic ones. Specifically, we discussed pros and cons of each approach for managing large scale server side applications.
Here are related articles if you wish to learn more advance topics for web development:
Best practices for securing and scaling Node.JS applications
Comprehensive overview of Angular 2 architecture and features
How Bootstrap 4 extensible content containers or Cards work
Comprehensive overview of Bootstrap 4 features for user interface customizations
Intro to functional reactive programming for advance js web development
Using advance js and webrtc for cross browser communications in real time
Intro to real-time bidirectional communications between browsers and webSocket servers
Junior or senior web developers can also explore career opportunities around blockchain development by reading below articles:
Blockchain Developer Guide- Comprehensive Blockchain Ethereum Developer Guide from Beginner to Advance Level
Blockchain Developer Guide- Comprehensive Blockchain Hyperledger Developer Guide from Beginner to Advance Level