In modern web development, microservices have become a popular architectural pattern. They allow different parts of an application to be built, deployed, and maintained independently. In a Django project, structuring your apps as independent microservices can help you achieve better scalability, maintainability, and performance.
In this blog, we will walk through how to build a demo Django Microservices project with a clean folder structure, modular apps, and best microservices communication practices. We will also show how to optimize performance using custom serializers and direct database queries, avoiding the overhead of HTTP requests.
To demonstrate the benefits of Django Microservices, we will divide our project into two distinct yet interconnected applications (microservices):
• app_1: Handles products.
• app_2: Manages orders that depend on product data from app_1.
Related read: How We Created Scalable And Secure Microservices-Based Architecture For A Finance Application
Django, by default, encourages a monolithic structure, but you can structure your project as microservices by splitting it into smaller, independent apps. Each app should focus on a specific domain and can communicate with other apps via APIs or direct database queries.
Here’s the general structure for our demo project:
django_microservices_project/
│
├── apps/
│ ├── app_1/ # Product microservice
│ │ ├── migrations/
│ │ ├── models.py
│ │ ├── serializers.py
│ │ ├── views.py
│ │ ├── urls.py
│ │ └── tests.py
│ │
│ └── app_2/ # Order microservice
│ ├── migrations/
│ ├── models.py
│ ├── serializers.py
│ ├── views.py
│ ├── urls.py
│ └── tests.py
│
├── config/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
│
├── manage.py
└── requirements.txt
This folder structure ensures a clear separation between different microservices (apps) while maintaining a shared project configuration under the config/ folder.
When structuring your Django project, follow these guidelines for a maintainable and scalable codebase:
➡️ Group by Domain
Instead of placing all files related to views, models, and serializers in the same folder, group them based on their respective domains (e.g., app_1 for products, app_2 for orders). This helps in managing the app’s functionality more effectively.
➡️ Keep App Independence
Each microservice app should be able to function independently. Keep models, views, serializers, and tests related to the domain within the app folder.
➡️ Modularize Shared Logic
If you have shared functionality, like database queries between apps, abstract this logic into separate files (e.g., microservice_communication.py) to avoid duplication and promote reusability.
➡️ Direct Database Queries
While it’s common for microservices to communicate over HTTP, in a Django project with shared databases, we can bypass HTTP calls and directly query the database of another app. This avoids the overhead of network requests and is more performant.
In our project, app_2 (Orders) needs to retrieve product data from app_1 (Products). Instead of using HTTP requests, we will import models directly and perform queries via Django’s ORM.
Here’s how we set up direct communication:
File: app_2/microservice_communication.py
# app_2/microservice_communication.py
from apps.app_1.models import Product # Import models directly from app_1
def get_products_by_name(name_filter=None):
"""
Function to query the Product model in app_1 and return filtered products.
Optionally accepts a name filter to search for products by name.
"""
queryset = Product.objects.all()
if name_filter:
queryset = queryset.filter(name__icontains=name_filter)
return queryset
def get_product_by_id(product_id):
"""
Fetch a specific product from app_1 by its ID.
"""
try:
product = Product.objects.get(id=product_id)
return product
except Product.DoesNotExist:
return None
In the above code:
• get_products_by_name filters products by name and returns a queryset.
• get_product_by_id retrieves a product by its ID.
This file acts as a communication layer between the two microservices, allowing app_2 to directly access data from app_1.
➡️ Models for app_1 (Product Microservice)
File: app_1/models.py
# app_1/models.py
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)
stock_quantity = models.IntegerField()
def __str__(self):
return self.name
@classmethod
def create_product(cls, name, description, price, stock_quantity):
"""
Custom method to create a product instance.
"""
product = cls.objects.create(
name=name,
description=description,
price=price,
stock_quantity=stock_quantity
)
return product
def update_product(self, name=None, description=None, price=None, stock_quantity=None):
"""
Custom method to update product information.
"""
if name:
self.name = name
if description:
self.description = description
if price:
self.price = price
if stock_quantity:
self.stock_quantity = stock_quantity
self.save()
return self
In this Product model:
🔹Fields: We define basic fields like name, description, price, and stock_quantity that represent each product.
🔹Methods: We include two methods:
• create_product: A class method to handle the creation of new products. This allows us to easily create products via a method rather than relying on Django’s default manager.
• update_product: An instance method to update product details, such as price or stock quantity.
➡️ Models for app_2 (Order Microservice)
In app_2, we will define an Order model that keeps track of customer orders. Each order will be associated with a product and contain information like the order quantity, the product, and the status.
File: app_2/models.py
# app_2/models.py
from django.db import models
from apps.app_1.models import Product # Direct import from app_1
class Order(models.Model):
PENDING = 'pending'
SHIPPED = 'shipped'
DELIVERED = 'delivered'
CANCELED = 'canceled'
STATUS_CHOICES = [
(PENDING, 'Pending'),
(SHIPPED, 'Shipped'),
(DELIVERED, 'Delivered'),
(CANCELED, 'Canceled'),
]
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField()
status = models.CharField(
max_length=10,
choices=STATUS_CHOICES,
default=PENDING
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"Order #{self.id} - {self.product.name}"
@classmethod
def create_order(cls, product_name, quantity):
"""
Custom method to create an order for a product.
"""
product = Product.objects.filter(name=product_name).first()
if product:
order = cls.objects.create(
product=product,
quantity=quantity,
status=cls.PENDING
)
return order
return None
def update_order(self, quantity=None, status=None):
"""
Custom method to update order details.
"""
if quantity:
self.quantity = quantity
if status:
self.status = status
self.save()
return self
In the Order model:
🔹ForeignKey: We link the Order model to the Product model from app_1 using a ForeignKey relationship. This allows each order to be associated with a specific product.
🔹Status Choices: The order can have multiple states (pending, shipped, delivered, canceled), which are defined as STATUS_CHOICES.
🔹Methods:
• create_order: A class method that creates an order for a given product name and quantity. It checks whether the product exists in app_1 and then creates the order.
• update_order: An instance method to update the order’s quantity and status.
➡️Database Relationship Between app_1 and app_2
• The Order model in app_2 has a foreign key relationship with the Product model in app_1. This means that every order in app_2 will reference a product from app_1.
• This relationship allows app_2 to access the product’s data and manage orders based on product information.
Django REST Framework’s ModelSerializer is convenient but can introduce performance overhead because it automatically introspects the model and generates validation code. For performance reasons, we can avoid using ModelSerializer and instead use regular serializers, where we manually handle object creation and updates.
🔹Performance: Regular serializers are faster because they don’t use the extra functionality of ModelSerializer.
🔹Custom Logic: You can define custom create and update methods that interact directly with the model’s methods.
File: app_2/serializers.py
# app_2/serializers.py
from rest_framework import serializers
from .models import Order
from app_2.microservice_communication import get_product_by_id
class OrderSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
product_id = serializers.IntegerField()
quantity = serializers.IntegerField()
def create(self, validated_data):
product_id = validated_data['product_id']
product = get_product_by_id(product_id)
if product is None:
raise serializers.ValidationError("Product does not exist.")
return Order.create_order(
product_name=product.name,
quantity=validated_data['quantity']
)
def update(self, instance, validated_data):
product_id = validated_data.get('product_id', instance.product_id)
product = get_product_by_id(product_id)
if product is None:
raise serializers.ValidationError("Product does not exist.")
return instance.update_order(
product_name=product.name,
quantity=validated_data.get('quantity', instance.quantity)
)
In this example:
🔹We directly check if the product exists by querying app_1’s product data before creating or updating an order.
🔹By using regular serializers, we gain full control over how objects are created or updated and avoid the overhead of ModelSerializer.
Now let’s define the views that will handle API requests in app_2 (Order microservice).
File: app_2/views.py
# app_2/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .serializers import OrderSerializer
class OrderCreateView(APIView):
def post(self, request, *args, **kwargs):
serializer = OrderSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Here, we use the OrderSerializer to handle the creation of orders. If the product ID exists in app_1, the order will be created successfully. Otherwise, it raises a ValidationError.
File: app_2/urls.py
# app_2/urls.py
from django.urls import path
from .views import OrderCreateView
urlpatterns = [
path('orders/', OrderCreateView.as_view(), name='order-create'),
]
This route listens for POST requests to create orders.
🔹Clear Separation of Concerns
Each microservice (app_1 and app_2) has a clear domain responsibility. This modularity makes it easier to scale and maintain different parts of the application.
🔹Performance
By using regular serializers and direct database queries, you minimize overhead. There’s no need for HTTP requests between services or automatic model serialization, which improves response times.
🔹Flexibility
You can easily add more microservices or change the database schema without disrupting the entire system. Each app remains independent and follows the Single Responsibility Principle.
🔹Simplified Communication
Instead of relying on external HTTP APIs for inter-service communication, direct database access through Django’s ORM reduces complexity and increases performance.
Adopting a microservices architecture in Django enhances scalability, maintainability, and performance. Structuring the project into independent apps with a well-organized folder hierarchy ensures a clear separation of concerns. Direct database queries between microservices reduce the overhead of HTTP requests, optimizing communication. Additionally, using custom serializers over ModelSerializer improves performance by eliminating unnecessary processing.
Implementing these best practices allows developers to build robust Django microservices that scale efficiently. Keeping each microservice independent yet seamlessly connected ensures flexibility and modularity. A structured approach enhances code reusability, simplifies debugging, and improves testing. Ultimately, this leads to a high-performing application that is easier to maintain and expand.
The team at Mindbowser was highly professional, patient, and collaborative throughout our engagement. They struck the right balance between offering guidance and taking direction, which made the development process smooth. Although our project wasn’t related to healthcare, we clearly benefited...
Founder, Texas Ranch Security
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork