Microservices are a type of software architecture in which application functionality is broken into a set of discrete units, each one called a microservice. This style of architecture is the opposite of a monolith, a type of application wherein the entire application runs as a single executable (and, usually, as a single process).
By breaking complex applications into smaller pieces, microservices help developers work more efficiently. They can also increase the scalability, performance and resiliency of applications, while potentially saving money on resource consumption.
At the same time, however, microservices introduce special challenges in areas such as security and application management. That’s mainly because microservices apps contain more moving pieces, which increases the effort required to track the internal state of microservices applications—not to mention the distributed infrastructure on which they usually run.
How Do Microservices Work?
In a microservices application, each microservice typically handles a discrete unit of application functionality. For example, one microservice might handle user authentication, while another microservice drives the application user interface and a third transfers data to and from a database.
That said, there are no special rules regarding how to delegate application functionality to microservices. Certain tasks could be divided up between microservices; for instance, an application could include multiple microservices that each handles a different part of the authentication process. It’s also theoretically possible to have a single microservice handle multiple tasks, although doing so is rare because the intermixing of unrelated functionality partly defeats the purpose of using microservices.
There isn’t a certain number of microservices that an application needs to include in order to count as a microservices app—you could implement just two or three microservices. Modern microservices applications, however, often include around a dozen microservices.
When developers create a microservices application, they typically manage the code for each microservice separately. Thus, instead of working on a large body of source code that covers the application as a whole, developers can divide development work into more focused areas by making changes to the code for individual microservices.
Individual microservices usually share data via internal networks (meaning networks that are only accessible from within the application, not the internet). Communications between microservices are typically managed via APIs or message brokers, both of which provide a consistent means of formatting data and transferring it from one microservice to another. Using these communication paths, individual microservices interact to handle application requests and provide full application functionality.
Instead of running on a single server, microservices often run on distributed infrastructure. Under this approach, each microservice operates on a different server and interacts with other microservices over the internet.
Distributed infrastructures aren’t a requirement for microservices; all of an application’s microservices could run on just one server. But by distributing microservices across a cluster, developers and IT operations teams can improve the performance and reliability of microservices applications. The main reason why is that in a distributed hosting environment, a “hungry” microservice that consumes a large volume of resources won’t deprive other resources of the microservices they need to run, since those microservices are hosted on different servers.
What Are the Benefits of Microservices?
By breaking complex applications (and the codebases behind them) into smaller pieces, microservices can deliver a number of important advantages:
- Scalability: In a microservices application, each microservice can be scaled individually by increasing or decreasing the number of instances of that microservice. Scaling individual microservices requires less effort and fewer resources than scaling an entire application. In this respect, microservices help maximize application scalability.
- Resilience: The fact that microservices are deployed separately also tends to increase application resiliency. In most cases, applications can continue to operate even if one microservice temporarily fails. That’s not so in the case of monoliths, where a failure anywhere in the application will usually bring down the entire application.
- Isolation: Because each microservice operates independently, microservices allow application functions, as well as data, to be isolated at the microservice level. This isolation can reduce the risk of a security issue that impacts one microservice breaching the entire application, or compromising all of the data managed by the application. Although application-wide breaches are possible with microservices, the risk is reduced by isolation between microservices.
- Smoother deployment: In most cases, each microservice inside an application can be deployed independently. Deploying microservices one-by-one is typically easier than deploying an entire application in a single go because there is less code to deploy and fewer configuration variables to manage. In this regard, microservices can lower the risk of application deployment.
- Code manageability: Because the codebase for each microservice is usually developed independently, it is typically easier for developers to build and update microservices than it would be to create a complex application using a single codebase. Developers can make changes to the code of one microservice without worrying about causing issues in other parts of the application codebase because microservices usually are not dependent on each other at the code level.
- Faster updates: The division of the codebase into smaller parts also means that it’s usually easier to update microservices applications. This translates to more rapid innovation and faster updates for users.
What Are the Drawbacks of Microservices?
The drawbacks of microservices can be summarized in a single word: complexity.
In some respects, microservices make complexity easier to handle because they allow developers to work with individual pieces of an application when writing code. However, beyond the context of coding and code management, microservices increase complexity in most respects.
This complexity means that microservices are usually:
- Harder to deploy, because teams need to deploy each microservice individually, rather than all at once.
- Harder to manage, because each microservice must be monitored separately, even as the team also tracks overall application performance.
- Harder to secure in some respects, due especially to the added security exposures associated with microservices’ extensive use of APIs and the network to exchange data.
To manage these complexities, development and IT teams usually rely on a more extensive set of tools when working with microservices applications than they would when dealing with monoliths. They use release automation tools to manage deployment, for instance, and orchestration platforms such as Kubernetes to scale microservices instances up and down. These tools help manage the complexity of microservices, but they also add to complexity in their own right by making the technology stack surrounding microservices larger and more complicated.
Due to these limitations, developers should think strategically about whether microservices make sense for a given application or use case. While microservices are typically the way to go when building complex applications for which scalability and resilience are priorities, they may be overkill for smaller, less complicated applications.
Examples of Microservices
Microservices are widely used today to deploy applications of all types.
Microservices SaaS app
Perhaps one of the most common examples of a microservices application in the real world is a software-as-a-service (SaaS) app that relies on a set of microservices to operate its various components. The exact architecture can vary from one app to another, but it might look something like this:
- One microservice to handle the UI.
- One microservice to generate app content.
- One microservice to authenticate users.
- One microservice to send and receive data from an external database.
- One microservice to generate reports on user behavior.
- One microservice to collect application metrics and write them to a log file.
- One or more microservices to integrate the application with any third-party services (such as cloud storage or an e-commerce platform integration) that it depends on.
Streaming content platform providers such as Netflix famously make extensive use of microservices to power their operations.
In use cases like content streaming in particular, microservices are beneficial because they help applications scale up and down rapidly—an important consideration for streaming platforms that experience fluctuations in demand. Microservices also increase reliability and resiliency, helping providers avoid blips or delays that could have a major impact on the user experience.
Social media platforms
For similar reasons, social media platforms, too, are known to rely heavily on microservices. These platforms also need to be massively scalable and resilient, a use case that microservices enable well.
Although the internet of things (IoT) ecosystem continues to evolve, microservices offer important potential in this domain as a way of building applications that can be distributed efficiently across a sprawling IoT infrastructure. Individual IoT devices that may lack the resources required to host an entire application can instead run individual microservices, then rely on the network to interact and compose the complete application.
Although microservices aren’t the right architectural solution for every type of application, they have become the go-to approach for most applications in today’s cloud-centric world. They help teams build more scalable and agile applications, while also providing some important reliability and security benefits. The main drawback of microservices is that they significantly increase the complexity of applications, but that challenge is often outweighed by the advantages that microservices confer.