Microservices

When applications were simpler, they were a single program or a set of programs all written in the same programming language or technology stack and managed by the same person or organization. Today applications have become so complex that no one set of programs can do everything and they often need to interact with programs written by other organisations and possibly using different technology stacks.

When writing programs, functions are a way to allow a set of instructions to be called from other parts of the program or other programs. Imagine a function in one program that could be called from another program over the network, a local network, or the Internet. This function would return some data to the calling program, which is required for its processing. This mechanism to allow one program to request and receive data from another program is referred to as an Application Programming Interface (API).

The architecture used by such applications is known as Service Oriented Architecture (SOA). Each program that provides specific functionality that other programs can use is referred to as a service or microservice (depending on the granularity of the function). Each microservice generally delivers a specific functionality. Just as functions, microservices accept input parameters and return output values after processing or simply fetching them from a database. The calling program will call microservices in the desired sequence to complete a transaction.

APIs are very widely used in web applications and smartphone apps. The processing and data access logic on the servers is made accessible to apps via APIs. With this approach, apps written using any technology stack and for any device can access the same processing logic on the servers to provide consistent functionality.

API Examples

Let's take the example of an online shopping mobile app. The mobile app has all the presentation logic programmed but has no data to display. The data is maintained in the server database. A typical shopping transaction involves the shopper asking for a list of products of a certain category, selecting products and adding them to a shopping cart, selecting a delivery address, and finally paying for the order. Each step of the transaction is managed by a microservice on the application server. The mobile app uses the API mechanism to invoke each microservice in turn.

  • First, the mobile app will call a microservice to fetch the list of products from the category passed as a parameter. The microservice will return a list of products and product details which the mobile app will then display to the user. Every time the user adds a product to the shopping cart, a microservice is called, which saves the product order in the database. The shopping cart microservice may itself be comprised of multiple services to add a product, change a product quantity, and delete a product.

  • Once the shopping cart has been filled, the microservice to set the delivery address is called. The app could ask the user to enter the address manually but that would take effort and be error-prone. Instead the app can use the smartphone's GPS hardware and determines the latitude and longitude of the user's location. It can then call a microservice that connects to a mapping database and determines the exact address from the latitude and longitude values. All app developers can't maintain a database of addresses, so they use a microservice offered by an organization that does maintain such a database.

  • For the last step of the transaction, which is paying for the order, the app again has to call a microservice provided by a bank or some other financial entity.

This wide repository of microservices with services offered by several organizations in their areas of expertise and accessible over the Internet. It further allows developers to create applications with rich functionality without writing all the code or maintaining all the data by themselves.

Today, the wide usage of APIs and microservices in applications has introduced a new tier in application architectures, the API Gateway. The API Gateway accepts requests from clients and forwards them to the microservices provider. Acting as an intermediary, the API gateway handles the work of authenticating clients, implementing security checks so that malicious requests are not sent to the server, controlling the number of requests so that the server is not overloaded, and other such controls.

APIs form the basis of accessing most cloud services and Software as a Service (SaaS) solutions.

SOA Architecture