Python Microservices Development - Second Edition

By Simon Fraser , Tarek Ziadé
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    Discovering Quart
About this book

The small scope and self-contained nature of microservices make them faster, cleaner, and more scalable than code-heavy monolithic applications. However, building microservices architecture that is efficient as well as lightweight into your applications can be challenging due to the complexity of all the interacting pieces.

Python Microservices Development, Second Edition will teach you how to overcome these issues and craft applications that are built as small standard units using proven best practices and avoiding common pitfalls. Through hands-on examples, this book will help you to build efficient microservices using Quart, SQLAlchemy, and other modern Python tools

In this updated edition, you will learn how to secure connections between services and how to script Nginx using Lua to build web application firewall features such as rate limiting. Python Microservices Development, Second Edition describes how to use containers and AWS to deploy your services. By the end of the book, you’ll have created a complete Python application based on microservices.

Publication date:
September 2021


Understanding Microservices

We are always trying to improve how we create software. Computer programming is less than 100 years old, and we have evolved rapidly through technology, design, and philosophy to improve the tools and applications we produce.

Microservices have revolutionized software products by improving the readability and scalability of services, and have allowed organizations to speed up their release cycles and be more responsive to the needs of their customers. Everybody wants to ship new products and new features to their customers as fast as possible. They want to be agile by iterating often, and they want to ship, ship, and ship again.

With thousands of customers using your service simultaneously, it is considered good practice to push an experimental feature to production and remove it again if needed, instead of waiting for months to publish it and many other features at the same time.

Companies such, as Netflix, are promoting their...


The origins of service-oriented architecture

There is no official standard for microservices, so it is helpful to look at a bit of the history in this area of software design. When discussing microservices, Service-Oriented Architecture (SOA) is often used as a starting point. SOA is a way of thinking about software architecture that encourages reusable software components that provide well-defined interfaces. This allows those components to be reused and applied to new situations.

Each unit in the preceding definition is a self-contained service that implements one facet of a business and provides its feature through some interface.

While SOA clearly states that services should be standalone processes, it does not enforce what protocols should be used for those processes to interact with each other and is quite vague about how you deploy and organize your application.

If you read the SOA Manifesto (, first published on the web circa...


The monolithic approach

With a monolith, everything about the service is in one place – the API, database, and all associated tools are managed as part of one code base. Let's take a very simple example of a traditional monolithic application: a hotel booking website.

Besides the static HTML content, the website has a booking feature that will let its users book hotels in any city in the world. Users can search for hotels, then book them with their credit cards.

When a user performs a search on the hotel website, the application goes through the following steps:

  1. It runs a couple of SQL queries against its hotel database.
  2. An HTTP request is made to a partner's service to add more hotels to the list.
  3. Results are sent to the JavaScript embedded in the web page, to render the information for the viewer.

From there, once the user has found the perfect hotel and selected the booking option, the application performs these steps:


The microservice approach

If we were to build the same application using microservices, we would organize the code into several separate components that run in separate processes. We have already discussed the PDF report generator, and we can examine the rest of the application and see where we could split it into different microservices, as shown in the following diagram:

Figure 1.2: A sample microservice architecture

Don't be afraid of the number of components displayed in this diagram. The internal interactions of the monolithic application are just being made visible by separate pieces. We've shifted some of the complexity and ended up with these seven standalone components:

  1. Booking UI: A frontend service that generates the web user interface, and interacts with all the other microservices.
  2. PDF reports: A very simple service that will create PDFs for receipts or any other document given a template and some data. Also known as the PDF...

Microservice benefits

While the microservices architecture looks more complicated than its monolithic counterpart, it offers multiple advantages. It offers the following:

  • Separation of concerns
  • Smaller projects to deal with
  • More scaling and deployment options

We will discuss them in more detail in the following sections.

Separation of concerns

First of all, each microservice can be developed independently by a separate team. For instance, building a reservation service can be a full project on its own. The team in charge can code it in the programming language and database of their choice, as long as it has a well-documented HTTP API.

That also means the evolution of the app is more under control than with monoliths. For example, if the payment system changes its underlying interactions with the bank, the impact is localized inside that service, and the rest of the application stays stable and is probably unaffected.

This is known as...


Pitfalls of microservices

As discussed earlier, building an application with microservices has many benefits, but it's not a silver bullet by any means.

You need to be aware of these main problems you might have to deal with when coding microservices:

  • Illogical splitting
  • More network interactions
  • Data storing and sharing
  • Compatibility issues
  • Testing

These issues will be covered in detail in the following sections.

Illogical splitting

The first issue of a microservice architecture is how it gets designed. There's no way a team can come up with the perfect microservice architecture on their first shot. Some microservices like the PDF generator are an obvious use case. But as soon as you deal with the business logic, there's a good chance that your code will move around before you get a good grasp of how to split things into the right set of microservices.

The design needs to mature with some try-and...


Implementing microservices with Python

Python is an amazingly versatile language. As you probably already know, Python is used to build many different kinds of applications – from simple system scripts that perform tasks on a server to large object-oriented applications that run services for millions of users. Python is also used in machine learning and data analysis tools.

Python sits comfortably in the top five languages in the TIOBE index (, and has reached as high as number two. It's probably even bigger in the web development world since languages like C are rarely used as main languages to build web applications.

This book makes the assumption that you are already familiar with the Python programming language. If you are not an experienced Python developer, you can read the book Expert Python Programming, Third Edition, where you will learn advanced programming skills in Python.

However, some developers...



In this chapter, we've compared the monolithic and microservice approaches to building web applications, and it became apparent that there's not a binary choice where you have to pick one model on day one and stick with it.

You should see microservices as an improvement of an application that started its life as a monolith. As the project matures, parts of the service logic should migrate into microservices. It is a useful approach, as we've learned in this chapter, but it should be done carefully to avoid falling into some common traps.

Another important lesson is that Python is considered to be one of the best languages to write web applications and, therefore, microservices. For the same reasons, it's a language of choice in other areas, and also because it provides many mature frameworks and packages to do the work.

Python can be a slow language, and that can be a problem in very specific cases. Knowing what makes it slow, and the different...

About the Authors
  • Simon Fraser

    Simon Fraser works as a Lead Site Reliability Engineer at Cisco Meraki. Previously, he worked at Mozilla as a Senior Release Engineer and Systems Administrator in a high-performance computing environment, which is where he started using Python to automate systems administration tasks. He has more than 20 years of experience in writing software tools, automating and deploying large-scale systems, and using data to drive the flow of work.

    Browse publications by this author
  • Tarek Ziadé

    Tarek Ziadé is a Software Engineer, located in Burgundy, France. He works at Elastic, building tools for developers. Before Elastic, he worked at Mozilla for 10 years, and he founded a French Python User group called Afpy. Tarek has written several articles on the Python language for various magazines, and also a few books in French and English.

    Browse publications by this author
Python Microservices Development - Second Edition
Unlock this book and the full library FREE for 7 days
Start now