Majestic Monolith Django

kokospapa
딜리버스
Published in
3 min readApr 17, 2022

Introduction

https://github.com/kokospapa8/majestic-monolith-django

This project is a starter django repo aiming to achieve majestic modular monolith architecture. Main purpose is to provide scaffolding skeleton and sample architecture for rapid prototype structure that can scale to mid-range size application. I have complied useful techniques and libraries to help build backend API server.

Inspired by Majestic monolith and Two Scoops of Django, this starter code will help you build scalable application for a small team of developers.

Majestic monolith

MicroService is everywhere and no doubt that they are the next big thing, for a company with many developers and in need for concurrent feature releases.

However, MSA needs a lot of coordination and preparation to make is work. If you are the only developer in the team or developing ina a relatively small to medium scale architecture, MSA can be overwhelming.

You can reduce cognitive load by following DDD practice. With code isolation, data isolation and some cloud architecture help, majestic monolith django(MMD) can prepare for the scale and bigger team coordination.

Example application

This repo provides sample application illustrating following usecase.

I have broken down the application into four modules(auth, user, shipping, distribution) using techniques I used for application prototyping.

Module Structure

Code

Each domain consists of following structure.

  • admin: django admin
  • apps: Django App Config
  • choices: Enum files used in module
  • docs: yasg doc
  • events: events emitted from django module
  • exceptions: Custom exceptions
  • models: Ordinary django models
  • manager: Queryset managers
  • serializers: DRF serialiers
  • selectors: Query that requires join
  • services: Biz logics for domain
    domain services (domain specific biz logic)
    application services (UoW)
  • schema: api or model schema for testing
  • urls: django url resolver
  • utils_*: utils method
  • views: DRF views

Infrastructure

In order to achieve modular structure in single monolith, we need to use eventbus for inter-module communications. Also we are fully utilizing lambda compute to unburden load of api servers. Python application typically utilizes celery beat for heartbeat (cron) process but it is much easier to use eventbridge schedule with lambda calling heartbeat api.

  • Eventbus
  • In order to decouple models, mmd uses serverless event bus(Eventbridge)
  • SAM (lambda) for async compute
  • notification
  • long runnign tasks
  • Heartbeat with lambda to call api

Cookie cutter

You can use cookie cutter to start the repo.

pip install cookiecutter
cookiecutter https://github.com/kokospapa8/majestic-monolith-django.git --checkout cookie-cutter

Future todo

  • Async support
  • Dependency injection
  • SQS for batch POST request

TL;DR

Try out my sample application!

https://github.com/kokospapa8/majestic-monolith-django/

--

--

kokospapa
딜리버스

10x AWS Certified | 5x GCP Certified | 4x Azure Certified | CKAD | Cloud Native | Data Engineer | BigQuery