Python Microservices Development
eBook - ePub

Python Microservices Development

Build efficient and lightweight microservices using the Python tooling ecosystem, 2nd Edition

Simon Fraser, Tarek Ziadé

Share book
  1. 310 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Python Microservices Development

Build efficient and lightweight microservices using the Python tooling ecosystem, 2nd Edition

Simon Fraser, Tarek Ziadé

Book details
Book preview
Table of contents
Citations

About This Book

Use Python microservices to craft applications that are built as small standard units using proven best practices and avoiding common errors

Key Features

  • Become well versed with the fundamentals of building, designing, testing, and deploying Python microservices
  • Identify where a monolithic application can be split, how to secure it, and how to scale it once ready for deployment
  • Use the latest framework based on asynchronous programming to write effective microservices with Python

Book Description

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.

What you will learn

  • Explore what microservices are and how to design them
  • Configure and package your code according to modern best practices
  • Identify a component of a larger service that can be turned into a microservice
  • Handle more incoming requests, more effectively
  • Protect your application with a proxy or firewall
  • Use Kubernetes and containers to deploy a microservice
  • Make changes to an API provided by a microservice safely and keep things working
  • Identify the factors to look for to get started with an unfamiliar cloud provider

Who this book is for

This book is for developers who want to learn how to build, test, scale, and manage Python microservices. Readers will require basic knowledge of the Python programming language, the command line, and HTTP-based application principles. No prior experience of writing microservices in Python is assumed.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Python Microservices Development an online PDF/ePUB?
Yes, you can access Python Microservices Development by Simon Fraser, Tarek Ziadé in PDF and/or ePUB format, as well as other popular books in Computer Science & Web Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2021
ISBN
9781801079372
Edition
2

2

Discovering Quart

Quart was started in 2017 as an evolution of the popular Flask framework. Quart shares many of the same design decisions as Flask, and so a lot of the advice for one will work with the other. This book will focus on Quart to allow us to support asynchronous operations and to explore features such as WebSockets and HTTP/2 support.
Quart and Flask are not the only Python frameworks. There is a long history of projects aimed at providing services on the web, such as Bottle, cherrypy, and Django. All of these tools are used around the web, and they all share a similar goal: to offer the Python community simple tools for building web applications quickly.
The smaller frameworks, such as Quart and Bottle, are often called microframeworks; however, the term can be a bit misleading. It does not mean you can only create micro-applications. Using those tools, you can build any application, large or small. The prefix "micro" means that the framework tries to make as few decisions as possible. It lets you freely organize your application code and use whichever libraries you want.
A microframework acts as the glue code that delivers requests to your system and sends back responses. It does not enforce any particular paradigm on your project.
A typical example of this philosophy is when you need to interact with a SQL database. A framework such as Django is batteries-included and provides everything you need to build your web app, including an Object-Relational Mapper (ORM) to bind objects with database query results.
If you want to use an alternative ORM such as SQLAlchemy in Django to benefit from some of its great features, you'd be choosing a difficult path that would involve rewriting a lot of the Django library you are hoping to make use of, because of the tight integration Django has with the ORM it comes with. For certain applications, that's a good thing, but not necessarily for producing a microservice.
Quart, on the other hand, does not have a built-in library to interact with your data, leaving you free to choose your own. The framework will only attempt to make sure it has enough hooks to be extended by external libraries to provide various kinds of features. In other words, using an ORM in Quart, and making sure you're doing the right thing with SQL sessions and transactions, will mostly consist of adding a package such as SQLAlchemy to your project. If you don't like how a particular library integrates, you're free to use another one or to build your own integration. Quart can also make use of the more common Flask extensions, although there is a performance risk there as they are unlikely to be asynchronous and could block your application's work.
Of course, that's not a silver bullet. Being completely free in your choices also means that it is easier to make poor decisions and build an application that relies on defective libraries, or one that is not well designed. But fear not! This chapter will make sure you know what Quart has to offer, and how to organize your code for building microservices.
This chapter covers the following topics:
  • Making sure we have Python
  • How Quart handles requests
  • Quart's built-in features
  • A microservice skeleton
The goal of this chapter is to give you all the information needed to build microservices with Quart. By doing so, it inevitably duplicates some of the information you can find in Quart's official documentation, but focuses on providing interesting details and anything relevant when building microservices. Quart and Flask have good online documentation.
Make sure you take a look at Quart's and Flask's documentation, listed respectively:
  • https://pgjones.gitlab.io/quart/index.html
  • https://flask.palletsprojects.com/
Both should serve as a great complement to this chapter. The source code is located at https://gitlab.com/pgjones/quart.
This is worth being aware of, as the source code is always the ultimate truth when you need to understand how the software works.

Making sure we have Python

Before we start digging into its features, we should make sure that we have Python installed and working!
You might see some documentation or posts online that mention Python version 2. There was a long transition from Python 2 to Python 3, and had this book been written a few years earlier, we would be discussing the merits of each. However, Python 3 is fully capable of everything the majority of people need to do, and Python 2 stopped being supported by the core Python team in 2020. This book uses the latest Python 3.9 stable release for all its code examples, but they are likely to work on Python 3.7 or later, as that's the minimum version that Quart requires in order to work.
If your computer does not have at least Python 3.7, you can download a new version from Python's own website, where installation instructions are provided: https://www.python.org/downloads/.
You will find it easier if all the code examples in this book are run in a virtual environment, or virtualenv (https://docs.python.org/3/library/venv.html). A virtual environment is Python's way of keeping each project separate, as it means you can install Quart and any other libraries you need; it will only affect the application you are currently working on. Other applications and projects can have different libraries, or different versions of the same library, without them getting in the way of each other. Using a virtualenv also means that you can easily recreate your project's dependencies somewhere else, which will be very useful when we deploy a microservice in a later chapter.
Some code editors, such as PyCharm or Visual Stu...

Table of contents