FastAPI Microservice Patterns
By: Florian Kromer
Tags: Python, Skaffold, Fastapi, Design Patterns, Microservices

FastAPI Microservice Patterns

The complexity involved in microservice based application design in underestimated!

The design of microservice based applications requires fundamentally different design approaches than in the development of monolithic applications. The best book I discovered so far to learn about why the design patterns of monolithic applications do not work for microservice based applications and how to implement microservice patterns is Microservice Patterns: With examples in Java.

Why is it hard to learn how to implement microservice patterns in Python frameworks?

Unfortunately the examples in the book depend on the Java’s Spring Boot framework. This means most Python programmers will probably have a hard time to implement these patterns straight away or adapt the examples from Java to Python.

One reason for this is that a lot Python programmers do not have experience with “harder to learn” programming languages.

Another reason is that the Spring Boot framework and other frameworks like ASP.NET Core help to implement a lot of the microservice patterns by providing abstraction. Examples are the client side discovery pattern implemented with Eureka via decorators like @EnableEurekaClient or the application metrics pattern implemente with Prometheus via Spring Metrics ( @EnablePrometheusMetrics ). Many of these abstractions are not available in Python frameworks suitable for creating microservice based applications like FastAPI, Flask or Django. (Yes, it’s possible to use Django for microservice based applications!) Finding resources about microservice pattern implementations Spring Boot (e.g. ddd-strategic-design-spring-boot) for ASP.NET Core (NET Microservices: Architecture for Containerized .NET Applications →eShopOnContainers, Awesome-Microservices-NetCore →e.g. Distributed .NET Core → DNC-DShop) is easier than finding resources for Python frameworks as well.

A third reason is that Spring Boot and ASP.NET Core help to implement patterns w.r.t. domain driven design (DDD) like e.g. value objects (Spring Boot: @Embeddable ), entities (Spring boot: @Entity ), etc. . Knowing what’s supported in other de-facto frameworks helps to understand what’s potentially missing in Python frameworks. In Python frameworks there seems to be less “builtin support” to implement the DDD patterns. To learn more about this refer to e.g. ddd-python-django for Django or ddd-flask-example for Flask. Considering awesome-ddd there seem to be more third party libraries for JVM (Spring Boot) as well as for .NET (ASP.NET Core) as well.

This post series aims at closing the gap!

I’m planning to publish a series of blog posts which explain the implementation of microservice and DDD patterns with de-facto standard technologies: Kubernetes and FastAPI. I decided to choose FastAPI instead of Flask and Django because it’s the framework which results in the least verbose code which makes it easier to focus on the pattern implementation and prevents from cognitive overload. However adapting the patterns to Flask or Django should be easier than adapting Spring Boot or ASP.NET Core examples to these other Python frameworks.

In the next post I’ll introduce the development environment in which the examples may be executed.

The post series content