Hamid's blog

Micro-frameworks in Python, Go, Java, and C++ for microservices age

August 19, 2020

Introduction

Though initially MVC pattern was mostly used as server-side MVC and used with server side rendering (SSR), with advent of rich client side front-end frameworks, a shift in paradigm took place: backend api + rich client

One reason for this paradigm shift was due to the fact that companies and developers wanted to use (almost ) the same backend for applications on multiple platforms, like web, mobile and desktop. Today lots of web apps use Angular, Rectjs, Vuejs, React Native, Flutter, Electron or other front-end frameworks, and they rely on backend REST, Graphql, or gRPC API provided by backend micro-frameworks that try to be fast and efficient.

Micro-frameworks are usually less opinionated, more plugable with the ability to respond to changes in requirements.

Few important points about micro-frameworks

When choosing a micro-framework, there are few important things to consider:

  1. Programming language of choice
  2. Binary size of the app: Especially important for microservices and containers that have to scale-out.
  3. Cold start time: How fast/slow it starts up initially, has significant effect on delay of services to scale-out as load changes
  4. Efficiency of resource usage: Has direct effect on cost of infrastructure.
  5. Developer productivity

These points have clear effect on the total cost of app (design, development, deployment, maintenance, ...).

Cold start is much more visible especially when app is being run in serverless setting (e.g. AWS Lambda, Fargate, ECS, EKS or GCP Cloud Functions, Cloud Run, App Engine).

Python

Followings are modern and very useful micro-frameworks of Python:

  1. FastAPI: modern, efficient, and fast. Suitable for many use-cases. Supports HTTP/1.1, HTTP/2, WebSockets, GraphQL thanks to Uvicorn, Hypercorn, Graphene.
  2. Django REST framework (DRF): Already well-known.
  3. Quarts: Modern Flask alternative.
  4. Starlette: Basis for FastAPI, but could be used alone, as well.

Go / Golang

Go has is modern and already has lots of cool features. Support for http/2 (golang.org/x/net/http2) in golang is straightforward. Go also has a very good concurrency models supported build-in. You can use Go without any micro-framework, though there area very good micro-frameworks available in go as well.

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Java & Kotlin

Java has been around for a long time. Spring boot is widely used in Java community, though some do not consider it a micro-framework.

There are some interesting attempts towards micro-framework area:

  1. Quarkus: Developed by Red Hat is a full-stack, Kubernetes-native Java framework made for Java virtual machines (JVMs) and native compilation, optimizing Java specifically for containers and enabling it to become an effective platform for serverless, cloud, and Kubernetes environments. Supports Kotlin as well.
  2. Micronaut: Another micro-framework. Supports Java and Kotlin
  3. ktor: Framework for quickly creating connected applications in Kotlin with minimal effort

You can find a comprehensive review of java micro-frameworks here.

In Java world, there are basically two techniques that most of these micro-frameworks use:

  1. Using is a tool that generates a custom Java runtime image that contains only the platform modules that are required for a given application, such as jlink.
  2. GraalVM for Microservices uses AOT. Might not work for all situations.

C++

In order to design and develop services in C++, you can use:

  1. Drogon: Drogon: A C++14/17 based HTTP web application framework running on Linux/macOS/Unix/Windows. It is super fast, and probably the fastest.
  2. µWebSockets: Simple, secure & standards compliant web server for the most demanding of applications
  3. cpprestsdk: The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
  4. proxygen: A collection of C++ HTTP libraries including an easy to use HTTP server, by Facebook. Most feature-rich HTTP library collection, not only in C++ community, but overall.