Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

A microservices example: writing a simple to-do application

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

    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

    • Red Hat Enterprise Linux 10.2 and 9.8: Top features for developers

    • What GPU kernels mean for your distributed inference

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.