Create a Microservice in 15 minutes flat!

Rahul Prabhu
Analytics Vidhya
Published in
4 min readApr 30, 2021
Photo by Krisztina Papp from Pexels

Microservices are like Baazars: One can get everything they might want, but every shop sells only one type of item: A clothes shop sells clothes, Fruits shops sells fruits. The Bazaar itself is not one giant shop: rather, it is a grouping of smaller shops, each which sell specialized goods. Now, imagine the confusion if each and every shop had to sell each and everything! No shop would be able to specialize. This reduces quality and introduces unnecessary burdens.

This idea can be applied to Software as well. A single, giant codebase that tries to do everything won’t be able to specialize. This reduces quality and introduces unnecessary burdens. Every update, from a variable name change to the rewrite of entire classes, will require the entire monolith to be redeployed. Also, these monolithic codebases are notoriously difficult to scale to respond to real-time traffic. Furthermore, an error in any one module of the monolith can cause the entire monolith to come crashing down.

How can we counter these issues? Enter Microservices: small isolated containers which perform small, specific functions. When taken as a whole, microservices cover all the functions of a Monolithic Codebase. Microservices offer granular scaling, enable continuous integration/deployment, and simplify maintenance of scaled services.

We will create our own Microservice, using Python, Flask, and Heroku, in under 15 minutes — for no cost at all.

Heroku

You will need to create a Heroku account. Heroku is a cloud service platform, similar to AWS and Google Cloud. We will be using Heroku since it is easy to set up and offers an excellent free tier. After you sign up, Heroku will redirect you to your app overview dashboard.

Press the New dropdown button, then select ‘New App’

Enter a project name (Heroku will use this name as the subdomain at _.herokuapp.com when it creates the URL for your microservice).

We have created our Heroku app. To deploy your code, Heroku will need a Github Repository from where it can retrieve your codebase. I have created a template repository with everything you and Heroku need to run your microservice, here: https://github.com/grokwithrahul/Flask-Microservice-Template

Fork the repository, and rename it.

Using the template:

The python file that will run as your microservice is located at src/microservice.py. In the file, there are two functions: one whose URL is ‘/’ and returns the string “hello.” The other allows you to execute code that takes a long time to run; without causing Heroku to hang. You can define the heavy_process by replacing the code under heavyProcess().

Linking to Heroku

So far, we have created a Heroku account and a Heroku App. Also, we have forked the Microservice template. Now, it’s time to link the Github Repository with Heroku.

Navigate to the Deploy tab of your app’s dashboard. Click on Connect to Github.

After signing in to your GitHub account, enter the name of your repository. Press ‘Connect’

After the repo is connected, you can enable automatic deploys if you want. Therefore, every commit will be automatically deployed on Heroku.

To deploy your code initially, scroll down and press Manual Deploy. This will kick off the deployment.

There you have it! You have built a microservice. To access it, you can click Open App in your app dashboard. That will take you to the ‘/’ URL of the microservice and should kick off the function associated with it.

Understanding the benefits of Microservices

Now that our Microservices are up and running, we can examine the benefits it has to offer.

Source: Algorithmia

Microservices allow granular scaling. A single microservice running a single task can be scaled almost immediately to respond to increased traffic. This way, you only need to scale the functions you need to — unlike Monolithic applications, where you would need to scale the entire program.

Microservices offer CI/CD. Every microservice can be deployed immediately after development. To deploy a monolithic application, all teams working on it would have to finish their projects, wait for other teams to finish there's, put a hard stop on everything, then bundle it out.

Microservices are easy to maintain. A server outage will not take out your entire application. Microservices allow you to isolate faults and separate failures, allowing your main application to continue running, even when one of its functions fails. It is also easier to understand and debug microservices since they have smaller codebases.

--

--