Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

A microservices example: writing a simple to-do application

September 15, 2016
Saurabh Badhwar
Related topics:
KubernetesMicroservicesPython
Related products:
Red Hat OpenShift Container Platform

Share:

    Microservices are becoming a new trend, thanks to the modularity and granularity they provide on top of advantages like releasing applications in a continuous manner. There are various platforms and projects that are rising which aims to make writing and managing microservices easy.

    Keeping that in mind, I thought, why not make a demo application that can give an example of how microservices are built and how they interact. In this article, I will be building a small application using the Microservice Architecture (MSA).

    The application will be a super simple To-Do management list. So, let's have take a look at what we are going to build and how we are going to build.

    Editor's note: This article references Fedora, which is the upstream project for Red Hat Enterprise Linux (RHEL) --- now free for developers. This tutorial should also work on RHEL, just replace 'dnf' with 'yum' wherever appropriate.

    To-Do Manager: A super simple Microservices Example

    So, talking about our application, it is leveraging the MSA where the whole application is divided into a set of services that specialize in doing a specific task using a simple set of protocols. All the communication between different services occur over the network.

    Now, for building our Application, we will be making use of Python. Our application uses Flask as the framework for getting the basic things up. Currently, the application uses a few files in the JSON format which serves as the database for our application. For the time being, the application to most part is static in nature.

    So, let's talk about the architecture of the application. Currently, our application consists of two services namely the User service and the To-Do service:

    • User Service: The user service provides a RESTful endpoint to list the users in our application and also allows to query the user lists based on their usernames. This service currently runs on port 5000 of our server.
    • To-Do Service: The ToDo service provides a RESTful endpoint to list all the lists as well as providing the list of projects filtered on the basis of usernames. This service runs on port 5001 of our server.

    The application can be located at ToDo Manager, feel free to clone, fork, modify, and extend.

    So, let's setup our development environment and get set up with the application.

    Set up the development environment

    As a personal preference, I use virtual environments for building different python applications since it removes the risk of messing up with the libraries globally on my development system. For this tutorial, I will be using Fedora 24 Workstation. So, let's setup our environment by getting the required tools and setting our virtual environment. Run the following command to get the virtual environment.

    sudo dnf install python-virtualenv

    The next step is to create our project directory

    mkdir todo && cd todo

    Now, let's setup our virtual environment and install the required dependencies for our application:

    virtualenv venv

    The above command will create a virtual environment named venv under the application directory.

    Next, we need to install the dependencies for our application. Our application is current dependent on two libraries - Flask and requests. Here, Flask as I have introduced is a web framework and requests is a library which allows us to make HTTP requests.

    Before installing the dependencies, we need to activate our virtual environment. So, let's do it.

    source venv/bin/activate

    The above command activates our virtual environment and now we need to install the dependencies. Run the below commands to get the dependencies installed in our virtual environment.

    pip install flask requests

    So, now we are done with setting up our development environment. The next step is to setup our directory structure for the application. Currently, our application has two directories namely database and services. The database directory hosts the files containing some dummy data for the users and todo lists made by the users.

    The services directory holds the code for our individual services - in this case the user service and todo service.

    So, before we start coding our services, let's get the database setup.

    Create a file named users.json under the database directory and add the following to the file:

    {
        "saurabh_badhwar": {
            "id":1,
            "name":"Saurabh Badhwar",
            "verified":1
        },
        "aniket": {
            "id":2,
            "name":"Aniket Sharma",
            "verified":1
        },
        "luckas": {
            "id":4,
            "name":"Luckas Friendel",
            "verified":0
        }
    }
    

    The next thing we have to do is create another file named todo.json which contains the data of our lists. Create the file and add the following data to it:

    {
        "saurabh_badhwar": {
            "home": [
                "Buy milk",
                "Look for pest control service",
                "Get a new carpet"
            ],
            "work": [
                "Complete the blogpost",
                "Create presentation for meeting"
            ]
        },
        "aniket": {
            "school": [
                "Complete homework",
                "Prepare for test"
            ]
        }
    }
    

    So, now we are done with the database part for our application. Next, we have to build our services. So, let's start with writing our User service.

    Under the services directory, create a file named users.py and write the code for it:

    So, let's start with the code, first we import the dependencies for the service

    from flask import Flask, jsonify, make_response
    import requests
    import os
    import simplejson as json
    

    The next we do is to initialize our flask service

    app = Flask(name)

    Now, we import our users database in the service and parse it as a JSON file

    with open("{}/database/users.json".format(database_path), "r") as f:
        usr = json.load(f)
    

    The next step is to write our application endpoints, a simple hello world entrypoint can be created as:

    @app.route("/", methods=['GET'])
    def hello():
        ''' Greet the user '''
    
        return "Hey! The service is up, how about doing something useful"

    The @app.route helps set the application route. The above example helps us setup a hello world application point. When a user visits our application at http://localhost:5000 the user will be greeted with the message we have specified.

    Using the same technique we can come up with the other end points for our service. Writing the complete code doesn't seem feasible for this post. You can refer to the repository link provided above for the complete code of the application.

    The final step in writing the service will be to run the server as soon as the application is called. This can be achieved by the following set of code for our user microservice

    if __name__ == '__main__':
        app.run(port=5000, debug=True)
    

    For the ToDo service and rest of the code for the User service, you can look up the repository.

    If you found this article interesting, or built something that you want to deploy, head over and look at Red Hat OpenShift, which provides a great platform to host and manage your Microservices.

    Last updated: October 18, 2018

    Recent Posts

    • Migrating Ansible Automation Platform 2.4 to 2.5

    • Multicluster resiliency with global load balancing and mesh federation

    • Simplify local prototyping with Camel JBang infrastructure

    • Smart deployments at scale: Leveraging ApplicationSets and Helm with cluster labels in Red Hat Advanced Cluster Management for Kubernetes

    • How to verify container signatures in disconnected OpenShift

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue