After years of building applications and platforms using the Service Oriented Architecture, I became very interested in microservices last year. So much so that I chose the job offer based on the sole fact that it was providing me an opportunity to design and develop microservices on the AWS platform. I’ll share the pros and cons of microservices in a later post.
In this post, we’ll see how to build microservices in Python using a light-weight framework called Flask. Unlike other web frameworks (e.g. Rails,) Flask is very flexible and doesn’t force you to adopt a specific layout style for your projects. It’s light-weight because it doesn’t require users to use particular tools or libraries. For example, Flask doesn’t come with any database access libraries. You will use extensions to add the functionality that you want.
I have created a fictional project called Cinema 3 that demonstrates the use of microservices using Python and Flask. In this hypothetical project, we have a few microservices working together to allow users to find movies and books tickets online. The microservices which make up the project are:
- Movies: Manages information related to movies e.g. title, rating, etc.
- Show Times: Provides show times for movies.
- Bookings: Handles online booking.
- Users: Manages user accounts for our project.
The microservices talk to each other using REST API. How do they know the address (host:port) of other services? In this example, each microservice runs on a separate port so we could identify them. In real-world projects, people use more advanced techniques such as service discovery (either server or client side; consul is a popular tool that people use for this purpose.)
Flask VS Other Python Web Application Frameworks
Before you decide to use Flask, see how it holds up to against other Python web application frameworks on popularity, features and more. Click on the image below to start comparing.
Sample Code on GitHub
Here’s a link to the project on GitHub. The source code itself is pretty simple as this is just an example to give you a basic understanding of building microservices using Flask.
If you have any comments or question about the project, please let me know in the comments section below.
If you’re looking to learn Flask, here’s an excellent YouTube video series you should watch.