Skip to main content

A Toolkit for Building Microservices using the Hexagonal Architecture

Project description

tests PyPI version shields.io PyPI pyversions Coverage Status

hexkit

A chassis library for building domain-focused, infrastructure-agnostic, and event-driven microservices in Python.

In a Nutshell

This project implements the Triple Hexagonal Architecture pattern which is an optimization of the ordinary Hexagonal Architecture for use with microservices. In brief, adapters (as per the hexagonal architecture) are divided into two parts. One part called translator is specific to one individual microservice and its domain-oriented ports. The other part called provider is service-agnostic but specific to one technology of the surrounding infrastructure. Both parts interact through a high-level interface called protocol (not to be confused with Python's typing.Protocol) which is neither specific to the technology nor to the microservice. For more details on the Triple Hexagonal Architecture, refer to the concept paper that can be found here.

As a chassis lib, hexkit tries to reduce the redundancy and boilerplate across microservices. It does so by providing ready-to-use triple-hexagonal building blocks that are service-independent - hence protocols and providers. The only task that remains for an individual service is to implement service-specific translators between the service's ports and the general-purpose protocols (in addition to implementing the domain functionality of the service, of course).

Hexkit is designed as a general-purpose library. However, it currently contains only a limited collection of protocol-provider pairs that are of immediate interest to the authors. We like to add support for more protocols and technologies over time.

The following protocols and providers are currently available:

Protocol Providers
Event Publishing Apache Kafka
Event Subscription Apache Kafka
Object Storage S3(-compatible)
Data Access Object MongoDB

Hexkit does not force you to go all-in on the idea of Triple Hexagonal Architecture. You can use it just for the technologies where you see benefits and use another approach for the rest. For example, you could use hexkit for simplifying the exchange of events between microservices but use a classical web framework such as FastAPI for designing REST API and an ORM like SQLAlchemy for interacting with databases.

Feel free to develop hexagonal components yourself, whether or not you use the base classes of hexkit. Not every protocol or provider has to be general-purpose, however, if they are, please consider contributing them to hexkit.

Getting started

Please have a look at the example application described in ./examples/stream_calc.

We put a lot of effort into making the code self-documenting, so please do not be afraid of jumping directly into it. You can find the protocols being defined at ./src/hexkit/protocols and the providers being implemented at ./src/hexkit/providers.

A more elaborate documentation and tutorial is on the way.

Installation

This package is available at PyPI: https://pypi.org/project/hexkit

You can install it from there using:

pip install hexkit

The following extras are available:

  • akafka: when using the Apache Kafka-based event publishing or subscription
  • s3: when interacting with S3-compatible Object Storages
  • mongodb: when using mongodb as backend for the DAO protocol
  • test-akafka: testing utils for Apache Kafka
  • test-s3: testing utils for S3-compatible Object Storages
  • test-mongodb: testing utils for MongoDB
  • all: a union of all the above

Development

For setting up the development environment, we rely on the devcontainer feature of vscode.

To use it, you need Docker and VS Code with the "Remote - Containers" extension (ms-vscode-remote.remote-containers) installed. Then, you just have to open this repo in vscode and run the command Remote-Containers: Reopen in Container from the vscode "Command Palette".

This will give you a full-fledged, pre-configured development environment including:

  • infrastructural dependencies of the service (databases, etc.)
  • all relevant vscode extensions pre-installed
  • pre-configured linting and auto-formatting
  • a pre-configured debugger
  • automatic license-header insertion

Additionally, the following convenience command is available inside the devcontainer (type it in the integrated terminal of VS Code):

  • dev_install - install the lib with all development dependencies and pre-commit hooks (please run that if you are starting the devcontainer for the first time or if added any python dependencies to the ./pyproject.toml)

If you prefer not to use vscode, you could get a similar setup (without the editor specific features) by running the following commands:

# Execute in the repo's root dir:
cd ./.devcontainer

# build and run the environment with docker-compose
docker build -t hexkit .
docker run -it hexkit /bin/bash

License

This repository is free to use and modify according to the Apache 2.0 License.

Project details


Release history Release notifications | RSS feed

This version

8.4.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hexkit-8.4.0.tar.gz (114.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hexkit-8.4.0-py3-none-any.whl (169.0 kB view details)

Uploaded Python 3

File details

Details for the file hexkit-8.4.0.tar.gz.

File metadata

  • Download URL: hexkit-8.4.0.tar.gz
  • Upload date:
  • Size: 114.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hexkit-8.4.0.tar.gz
Algorithm Hash digest
SHA256 78a9f163de79d5c324b0511dd47e82e50ccd61dfde31094768258ae9fc8ae8b7
MD5 07a9d73563cc6b5b9bbadfaf3a255df8
BLAKE2b-256 75b596be53dd7156ea7c15310926ed9a430fd557c887829c266f32ab0c809ef8

See more details on using hashes here.

File details

Details for the file hexkit-8.4.0-py3-none-any.whl.

File metadata

  • Download URL: hexkit-8.4.0-py3-none-any.whl
  • Upload date:
  • Size: 169.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hexkit-8.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ff865fbc4646286a168850cfe4701c4d990570aab72a5762fe5c042db3a70e9
MD5 3b56e7f67a873cd182b50e4bc13d56a9
BLAKE2b-256 b5263488e51d799682a39a8121e4757b12dd863dd89f26b92f70a4282cada126

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page