Majestic Monolith Django
Published: 2022-04-17 07:34:50


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ap1h3bvum61mdt1fgewd.png)

## Introduction

---

[https://github.com/kokospapa8/majestic-monolith-django](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](https://m.signalvnoise.com/the-majestic-monolith/) and [Two Scoops of Django](https://www.feldroy.com/books/two-scoops-of-django-3-x), 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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmk4cmagfwbud9fi2o6v.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mynjyd1p79g1sh9z9hvq.png)

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/se52p037mz29o6a6lwr4.png)
- 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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/garae5k9auxj7y6wlqom.png)
- 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.

```jsx
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/