LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Python

Python - Microservices Architecture

Microservices split a large monolithic app into small independent services. Message brokers like RabbitMQ or Redis allow these services to communicate asynchronously without blocking each other.

Microservices Architecture in Python – Decoupling Monoliths and Managing Inter-Service Communication

Quick Answer: Microservices split a large monolithic app into small independent services. Message brokers like RabbitMQ or Redis allow these services to communicate asynchronously without blocking each other.

Imagine Priya runs a busy food delivery platform in Bengaluru processing 10,000 lunch orders every hour.

Her entire backend lives in a single monolithic Python script that handles user logins, order placement, payment processing, and kitchen dispatch all at once.

When the payment gateway slows down during peak rush hours, the entire application freezes, preventing customers from even browsing the menu.

How would you redesign this architecture so that payment delays never block kitchen orders or menu browsing?

1. Why Do We Need Microservices Instead of a Monolith?

The problem with monolithic applications

Monolith = a single unified application where all business logic runs together in one process.

Example 1: A tightly coupled monolithic order script

This approach works for small apps, but a failure in payment locks up the entire system.

def process_order(item, payment_gateway):
    print("Validating user...")
    print("Processing payment via gateway...")
    payment_gateway.charge() # If this hangs, kitchen gets no data
    print("Notifying kitchen for:", item)

if __name__ == "__main__":
    process_order("Masala Dosa", None)

Expected output

Validating user...
Processing payment via gateway...
Notifying kitchen for: Masala Dosa

This code runs fine locally, but it has severe production limitations.

  1. A bottleneck in one feature (like payment processing) crashes or slows down all other features.
  2. Scaling requires duplicating the entire application rather than just the busy service.
  3. Developers cannot deploy updates to the menu without restarting the payment engine.

We need to break the application into independent microservices.

The solution: microservices and message brokers

Microservice = a small, independent service focused on a single business capability.

Message Broker = an intermediary software that routes messages between decoupled services asynchronously.

Priya's Microservices & Message Broker Flow
Decoupling services using an asynchronous message queue
Order API Service Receives customer orders
RabbitMQ / Redis Broker
Payment Service Handles gateway charges
Kitchen Service Prints order tickets
Notification Service Sends SMS updates
Publisher Active Broker Consumer Service

Figure 1: The Order API publishes events to a broker, allowing consumer microservices to process tasks independently.

2. Implementing Inter-Service Communication with Python and RabbitMQ

Publisher = a service that sends messages to a broker queue.

Consumer = a service that listens to the broker queue and processes incoming messages.

Let us write a complete runnable Python concept using message queues for Priya's delivery app.

Example 2: Producer and consumer pattern for decoupled services

This code simulates how an Order service publishes a task and a Kitchen service consumes it.

import json
import queue

# Simulating a message broker queue (like RabbitMQ or Redis Pub/Sub)
message_broker = queue.Queue()

def order_service_publish_order(order_data):
    payload = json.dumps(order_data)
    message_broker.put(payload)
    print($"[Order Service] Published order to queue: {payload}")

def kitchen_service_consume_orders():
    while not message_broker.empty():
        raw_message = message_broker.get()
        order = json.loads(raw_message)
        print($"[Kitchen Service] Received ticket & started cooking: {order['item']}")

if __name__ == "__main__":
    # Customer places an order
    order_service_publish_order({"order_id": 101, "item": "Benne Dosa"})
    order_service_publish_order({"order_id": 102, "item": "Filter Coffee"})

    # Kitchen worker processes the queue independently
    kitchen_service_consume_orders()

Expected output

[Order Service] Published order to queue: {"order_id": 101, "item": "Benne Dosa"}
[Order Service] Published order to queue: {"order_id": 102, "item": "Filter Coffee"}
[Kitchen Service] Received ticket & started cooking: Benne Dosa
[Kitchen Service] Received ticket & started cooking: Filter Coffee

Notice how the order service places messages into the broker without waiting for the kitchen to finish cooking.

In production, this queue is managed by external brokers like RabbitMQ or Redis.

Comparing Monolithic Architecture with Microservices
Monolith Architecture Microservices Architecture
Single large codebase and shared database Independent services with dedicated data stores
Scaling requires duplicating the whole app Scale specific high-load services independently
Failure in one module brings down everything Fault isolation keeps other services running

The important terms to remember

Monolith = a single unified codebase containing all features.
Microservice = a small independent service focused on one domain.
Message Broker = middleware that routes asynchronous messages between services.
Publisher/Consumer = the sender and receiver pattern in message queues.

Design tip: Use asynchronous message queues (RabbitMQ/Redis) for background tasks so API response times remain fast.

3. Common Mistakes to Avoid

1. Sharing a single database across microservices

Allowing multiple microservices to read and write directly to the same database tables creates tight coupling and defeats the purpose of decoupling.

Better approach: Each microservice must own its private database, communicating data changes exclusively through APIs or message events.

4. Interview Questions & Answers

Question 1: When should you choose microservices over a monolith?

What the interviewer expects: Understanding of team scaling, deployment independence, and operational complexity.

Strong answer: "We should start with a modular monolith for early-stage products. We transition to microservices when independent scaling, separate team deployment cycles, and fault isolation become mandatory business requirements – such as when Priya's order volume scales to thousands of concurrent users."

What interviewers expect at different levels

  • Junior developer: Understands basic API calls between services.
  • Senior developer: Knows event-driven patterns, message brokers, and failure handling.
  • Technical architect: Designs distributed tracing, eventual consistency, and data isolation boundaries.

5. Practice Exercises

Beginner: Redis pub-sub simulator

Write a simple Python script using Redis client (`redis-py`) to publish and subscribe to a channel.

Expected result: Subscriber prints messages published by the publisher in real time.

Intermediate: RabbitMQ task worker

Set up a basic RabbitMQ producer using `pika` library to dispatch order notifications.

Hint: Ensure message acknowledgment (`ack`) is enabled so messages are not lost if a worker crashes.

6. Frequently Asked Questions (FAQ)

What is the difference between RabbitMQ and Redis?

RabbitMQ is a dedicated message broker with advanced queuing and routing features, while Redis is an in-memory data store often used for caching and lightweight pub/sub messaging.

What is eventual consistency in microservices?

Eventual consistency is a consistency model where data synchronized across distributed databases becomes consistent across all services after a short delay rather than instantly.

7. Next Steps

Now that you have mastered microservices architecture and message brokers, you are ready to explore automated testing and production deployment in the next article.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.