Microservices vs API

The terms API and microservices are often thrown around pretty loosely. What do these terms actually mean? How do they relate to one another? In this piece, we’ll tackle these questions, as well as answer some common misconceptions in the space.

What is an API?

Before we discuss microservices, we should first define what an API actually is at its most basic level. An API, or Application Programming Interface, is a set of rules and protocols governing the interaction between software applications. In essence, it acts as a sort of Rosetta Stone, setting the core language by which applications across environments can communicate. APIs exist locally, such as those within your computer which govern graphics processing, memory allocation, file storage, etc., but also exist on the macro level, connecting web APIs to resource servers, machines to machines, and so forth.

In the context of web APIs, these APIs typically bridge a connection between clients and servers. They take an instruction – say, “get the current status of a user record” – and convert it into a machine-usable command – such as “curl -X GET “https://api.io/v1/users/user10” -H “accept: application/json””.

Notably, how these APIs work can take a few different forms, and you will often hear a set of acronyms bandied about in the world of web APIs. REST, or Representational State Transfer, is a [way of connecting clients and servers](https://www.codecademy.com/article/what-is-rest) without requiring that either know anything about the other. SOAP, or Simple Object Access Protocol, is a more tightly coupled paradigm that is typically used for handling secure records requiring a high level of coupling and synchronicity. Other solutions such as GraphQL utilize novel methods of organizing and retrieving data, making it easier for APIs to do more complex functions such as replying to requests with custom data formats and structures.

Benefits of an API Paradigm

APIs are the powerhouse of the modern internet and web – they power the complex interactions between clients, servers, and services, unlocking amazing functionality by providing a common language of communication.

Beyond this connective benefit, APIs offer some attributes that make it more powerful than localized apps or mainframe operations:

* Interoperability – APIs seldom care about what is connecting, and mostly care about how they’re connecting. For this reason, APIs provide an infrastructural interoperability that allows for discrete systems and functions which would otherwise be incompatible to connect and cooperate.

* Efficiency – APIs allow for the same server functions and resources to be used across multiple development situations, reducing development time by creating portable and reusable functions.

* Scalability – by decoupling from centralization applications, modular development is made possible. Existing functions and resources can be tied to new API endpoints or actions, thereby allowing new development without having to change the entirety of the stack underpinning it all.

* Cost control – APIs operate on a per-request basis, and as such, limited resources can be controlled granularly by throttling or reducing access.

Pros and Cons of APIs

* Pros

** Ease of integration – APIs offer simplified integration of various systems, allowing for different applications to work in harmony rather than making a single system/point of failure.

** Reusable – APIs promote code reuse across different applications, which prevents duplication of functions and resources.

** Flexibility – APIs are quicker and easier to develop and deploy than full-fledged centralized systems.

* Cons

** Security – API security is a critical point of concern. APIs expose the underlying resources and logic to the end user, requiring proper and well-designed security to mitigate issues.

** Maintenance – APIs must be maintained and regularly updated, which can increase long-term costs

** Complexity – APIs scale in complexity, and this can result in a gordian knot if developers aren’t careful

The Monolithic Paradigm

In the early days of API development, everything was based on centralization. A user could call up a system to perform an action using its resources, often a mainframe or central server, and the results would be pushed to the user. 

This often created tight coupling between the request and the resources underpinning it, resulting in a development style known as “The Monolith”. In the monolith, there was a single, unified software architecture where all components are interconnected and interdependent. The interface and its systems were bundled together, and any change that happened to a component meant the entire system had to be updated.

This “one stop shop” approach typically resulted in a single codebase, shared resources, and centralized management.

Pros and Cons of the Monolith

* Pros

** Simplicity – monoliths bundled everything together, which made initial development and deployment quite simple. This benefit was quickly lost in scaling, however, so it was very much a double-edged sword.

** Performance – at smaller scales, monoliths served as a central highway between requests and resources, making for high performance requests across the board.

** Control – the monolith is the ultimate approach to centralized control. Everything was dealt with singularly and centrally, granting significant control that would otherwise be hard to enforce.

* Cons

** Hard to scale – while the monolith had huge gains in simplicity at the start, as development scaled, each development piece meant working on the entire corpus rather than individual parts. This was hugely limiting and expensive.

** Maintenance challenges – APIs need to be constantly maintained and updated to stay effective in the changing user and economic environment they live in, and monoliths make this a very costly approach, especially as the code increases in complexity

** Limited flexibility/extensibility – changes in one part can cause ripple effects throughout the entire system. This limits the core flexibility and extensibility of the system as a whole.

What are Microservices?

As the monolith began to show itself as problematic, a new approach was needed to connect services without the baggage of a centralized solution. Microservices were developed as an architectural style to solve many of these problems. In essence, microservices is a style in which an application is composed of small, independent services that communicate over a network. Each piece of system is broken into discrete units, and are bundled with the resources and systems needed to function in concert with others.

Imagine an API which contains student names, their grades, their enrollment data, and so forth. In the monolith, all of this would exist as a single server somewhere with a single endpoint to interact with. In a microservices paradigm, there might be a StudentData service, a ClassroomData service, a Grades database, an EnrollmentManager service, etc. If a new service needs to be developed – say we’re adding a Gym that is accessible only to enrolled students – that service, GymAccess, can be added independently from the rest.

This approach is an answer to the issues of the monolith, breaking individual components, functions, and processes into discrete units.

Benefits of the Microservice Model

* Scalability – the microservices model makes it easier to scale individual services based on demand, allowing for new resources to be added as needed without updating the core codebase.

* Extensible – new development has reduced cost both temporally and monetarily, as new components are easier to create than new monolithic branches.

* Flexibility – microservices enable the use of different technologies for different services, eliminating the “one size fits all” approach of the monolith.

* Resilience – fault isolation ensures that a failure in one service doesn’t affect the entire application, increasing both security and service uptime.

Pros and Cons of Microservices

* Pros:

** Independent deployment – services can be deployed independently with or without consideration of other services.

** Technology agnosticism – different services can use different technologies instead of adhering to a central truth or demand from the core service.

** Improved fault isolation and tolerance – failures are contained within individual services. This leads to easier repairs and issue mitigation overall.

* Cons:

** Complexity – this approach results in higher complexity due to the distributed systems, both in terms of codebase and in terms of control.

** Inter-Service communication – this introduces a cost through managing communication mechanisms, which can introduce infrastructural costs which were negligible under the monolith paradigm.

** Operational overhead – more resources are needed for monitoring and managing the network of connected services.

Differences in Documentation

It bears mention that microservices require some changes in how they’re documented compared to typical software solutions. Microservices documentation requires highly detailed information about each service’s internal architecture, dependencies, deployment processes, and configurations, as the entire network should have context around its communication. It also requires substantial documentation around security, transport, authentication, and authorization within the system, as modular components mean a heightened focus on transit of data between constituent parts.

API documentation tends to focus on endpoints, request/response formats, authentication methods, and usage examples. General API documentation is largely more focused on infrastructure, and less so on design documentation. In the case of microservices, this design documentation is very necessary, as it exposes the design paradigm which the API was created under, which is vital for understanding the way the system works.

Let’s look at some differences in documentation approaches between general APIs and microservices.

Scope & Detail

* Microservices documentation

** Includes: Internal architecture, dependencies, deployment, configurations.

** Examples: Service interfaces, database schemas, configuration files.

* API documentation

** Includes: Endpoints, request/response formats, authentication methods, usage examples.

** Examples: API endpoints, sample requests, error codes, and responses.

Inter-Service Communication

* Microservices documentation

** Includes: Messaging protocols, event handling, service discovery mechanisms.

** Examples: Inter-service communication protocols, message formats, service discovery tools.

* API documentation

** Includes: How external systems interact with the API, detailing endpoints and integration points.

** Examples: Endpoint descriptions, integration guides, data formats.

Operational Aspects

* Microservices documentation

** Includes: Operational guidelines, monitoring, logging, scaling strategies, fault tolerance mechanisms.

** Examples: Deployment guides, monitoring setup, scaling strategies.

* API documentation

** Includes: Usage guidelines for developers on how to consume the API, without delving deeply into operational considerations.

** Examples: Rate limits, authentication methods, usage examples.

Maintenance and Updates

* Microservices documentation

** Includes: Versioning strategies, deprecation policies, upgrade paths.

** Examples: Version history, change logs, migration guides.

* API documentation

** Includes: API versioning, deprecation warnings, backward compatibility notes.

** Examples: Deprecated endpoints, version-specific usage examples, upgrade instructions.

Security Considerations

* Microservices documentation

** Includes: Security protocols, access control mechanisms, encryption details.

** Examples: Service-specific security settings, role-based access control, data encryption methods.

* API documentation

** Includes: Authentication and authorization methods, rate limiting, secure communication protocols.

** Examples: OAuth setup, API key usage, HTTPS requirements.

Use Cases and Applications for Microservices and APIs

APIs and microservices often have the same use cases due to how they relate to one another, but there are specific use cases that are more appropriate for standard APIs than microservices (and vice versa).

Let’s take a look at a few of them now.

Microservices Use Cases

** Financial services – when there is a need to separate services between different products, branches, and partners, microservices shine. In the financial sector, separate services for user accounts, transactions, fraud detection, analytics, and more are relatively commonplace.

** Logistics and supply chain – modular services for inventory management, shipping, order tracking, and customer notifications are relatively common in the logistics space.

** Social media platforms – social media platforms often contain many different services, often services that were designed independently and then acquired. In this case, microservices can play a huge role in connecting these disparate services and providing a central backbone for communication. 

Standard API Use Cases

** Cloud services – as cloud services are essentially local services moved to remote servers, they often benefit from having standard APIs serving infrastructural purposes. 

** IoT devices – as IoT devices often have low memory and processing allowances, their internal systems typically run as a lean single API – although they can be part of a larger microservice collection, this microservice exists outside of the core device.

Differences in Deployment Strategies

Due to their different methods of connecting systems and resources, standard APIs and microservices often see different deployment strategies.

* Microservices

** Continuous Deployment – frequent updates and deployments with minimal downtime and constant iteration are a hallmark of microservice development due to its extensible and scalable nature.

** Containerization – use of Docker, Kubernetes, and other containerized solutions makes for easy deployment and scaling.

** Service Discovery – tools like Consul or Eureka help to locate services dynamically, reducing the overhead and complexity of the internal systems.

* APIs

** Versioning – deploying new versions of APIs while maintaining old versions.

** Gateway Services – use of API gateways for routing requests, rate limiting, and security.

** Monitoring – tools for tracking API usage, performance, and error rates.

Monitoring and Management

Monitoring and management can also differ significantly between core APIs and microservice implementations.

* Microservices

** Tools – Prometheus, Grafana, ELK Stack, and other monitoring and logging solutions are designed to collect data from a collection of services, centralizing them for understanding and context.

** Metrics – Service health, response times, error rates, and resource usage are tracked per service or cluster, allowing for discrete units to be measured against other systems for general health.

** Challenges – Managing logs and metrics across multiple services can be challenging, requiring additional services not required in monolithic or traditional API approaches.

* APIs

** Tools – Postman, Swagger, and API Management platforms like Apigee or AWS API Gateway play a huge role in managing standard APIs.

** Metrics – API call counts, latency, throughput, and error responses are collected centrally, as they are core components of a healthy infrastructure.

** Challenges – Ensuring high availability and handling peak loads is the most pressing consideration for most APIs.

Scalability and Performance

Scalability and performance considerations are very different when comparing singular APIs versus microservices.

* Microservices

** Horizontal Scaling – microservices can add instances of a service or resource to handle increased load.

** Load Balancing – microservices distribute requests across clusters or instances to improve load balancing.

** Performance – individual services are optimized for better overall performance, creating a cumulative effect that is often the focus over individual gains.

* APIs

** Caching – caching, or storing data that is often requested, is used to reduce the load on services.

** Rate Limiting – APIs often control the number of requests to prevent abuse, either rejecting or throttling them to maintain overall health.

** Optimization – standard APIs focus on minimizing response times and maximizing throughput.

Common Misconceptions about Microservices and APIs

Microservices and APIs – Are They the Same Thing?

Microservices and APIs are not the same thing, but they are intimately related. This is a bit of a “square” and “rectangle” situation – all microservices are APIs, but not all APIs are microservices.

* Microservices are an architectural style; APIs are a method of communication.

* Microservices are APIs in segmentation – the opposite of a microservice isn’t an API, it’s a monolith.

Do Microservices Require APIs for Communication?

Generally, microservices require APIs to communicate. The very nature of a microservice means that its communication framework is probably separate from its codebase. The specific language, framework, etc. chosen by the developer is often implemented in the transit layer through a gateway, shim, or other frontend. In this way, you could say that an API is required for communication as the API converts the internal functions and logic of the microservice to the external – but this is a bit like asking “do highways need roads”.

Are APIs Only Used by Microservices?

APIs are best thought of like infrastructure. APIs are the highways and roads that connect our cities (services) together. The paradigms that work on top of these connections don’t define the underlying structure.

APIs can be used by a wide variety of systems and services. While microservices use APIs by their core nature, APIs can also be used in non-networked computers to draw graphics, or in medical devices to connect data storage with measurement devices. APIs are used anywhere technology exists.

Is The Performance Improvement in Microservices Guaranteed?

The performance improvement promised by microservices depends a lot on how it’s implemented. It’s not necessarily true that simply developing a microservice will generate huge gains in performance – performance is a multifaceted question, and this can shift depending on specific needs, implementation, environment controls, etc.

In essence, it’s possible to do microservices “incorrectly” or “poorly”, and it’s also possible to make a perfect microservice that fails due to network limitations.

Conclusion

In conclusion, microservices and APIs are intimately related – whereas APIs are infrastructural solutions, microservices are a paradigm which focuses on making lots of little APIs instead of a single monolithic codebase. It’s possible to use an API without using a microservice, but you must use an API to have a microservice.

Microservices provide more extensibility and scalability, but this is traded off with additional complexity as each component is individually crafted as part of a cohesive network. APIs in their raw form are simpler, but far less efficient in terms of scalability and extensibility, and novel development is often quite expensive, as you must update the entire codebase instead of just a singular piece or part.