The microservice architecture has gained momentum in recent years, and is gaining popularity in web application development due to its modularity and scalability. Microservice architecture can offer almost all the features of a monolith that we saw in the earlier section. Additionally, it offers many more features and flexibility, and hence is often considered a superior choice for complex applications. Unlike the monolithic architecture, it's quite difficult to generalize the microservice architecture as it could vary heavily depending on the use case and implementation. But they do share some common traits and they are, in general, the following:
- Microservice components are loosely coupled. Components can be developed, tested, deployed, and scaled independently without disrupting other components.
- Components need not be developed using the same technology stack. This means a single component can choose its own technology stack and programming language.
- They often utilize advanced features such as service discovery, circuit breaking, load balancing, and so on.
- Microservice components are mostly lightweight and they do a specific functionality. For example, an authentication service will only care about authenticating a user into the system.
- Often has an extensive monitoring and troubleshooting setup.
An example of a microservice web application architecture would be as follows:
Let's imagine a huge online e-commerce system where customers can go through categories of merchandise, maintain favorites, add items to a shopping cart, make and track orders, and so on. The system has inventory management, customer management, multiple payment modes, order management, and so on. The application consists of several modules and components including a UI gateway application, which builds a nice rich user interface and also handles user authentication and load balancing, and several other backend applications responsible for managing the inventory, verifying payment, and managing orders. It also has performance monitoring and automatic failover for services.
The application will be deployed as multiple executable WAR files in Docker containers hosted by a cloud provider. Take a look at the following diagram:

The advantages of a microservice web application architecture are as detailed here:
- Loosely coupled components resulting in better isolation, easier to test and faster to startup.
- Faster development turnaround and better time to market. New features can be built faster and existing features can be easily refactored.
- Services can be deployed independently making the application more reliable and make patching easier.
- Issues, such as a memory leak in one of the services, are isolated and hence will not bring down the entire application.
- Technology adoption is easier, components can be independently upgraded in incremental migration making it possible to have a different stack for each component.
- More complex and efficient scaling models can be established. Critical services can be scaled more effectively. Infrastructure is used more efficiently.
- Individual components will start up faster making it possible to parallelize and improve overall start-up.
- Teams will be less dependent on each other. Best suited for agile teams.
The disadvantages of a microservice web application architecture are as detailed here:
- More complex in terms of the overall stack as different components might have different technology stacks forcing the team to invest more time in keeping up with them.
- Difficult to perform end-to-end tests and integration tests as there are more moving parts in the stack.
- The entire application is more complex to deploy as there are complexities with containers and virtualization involved.
- Scaling is more efficient but setting upscaling is more complex as it would require advanced features such as service discovery, DNS routing, and so on.
- Requires a larger team to maintain the application as there are more components and more technologies involved.
- Team members share varying skill sets based on the component they work on, making replacements and knowledge sharing harder.
- The technical stack is complex and most of the times harder to learn.
- Initial development time will be higher making time to market slower.
- Requires a complex infrastructure. Most often will require containers (Docker) and multiple JVM or app containers to run on.