System DesignMastery
--Distributed Systems — ডিস্ট্রিবিউটেড সিস্টেম

Microservices Architecture

Duration৬০-৯০ মিনিট
LevelIntermediate
FocusArchitecture Strategy
001Why It Matters

Microservices কেন শিখতে হবে?

আপনি একটা বড় e-commerce app বানাচ্ছো। User login, product catalog, payment, order tracking, notification — সব একটা codebase এ। এক জায়গায় bug হলে পুরো system down। Team বড় হলে merge conflict। Deploy করতে পুরো app restart।

এই সমস্যার সমাধান হলো Microservices Architecture। আজকাল Amazon, Netflix, Uber, Spotify সহ প্রায় সব বড় company এই approach ব্যবহার করে।

DEFINITION

Microservices হলো একটা architectural style যেখানে একটা বড় application কে ছোট ছোট independent services এ ভাগ করা হয়। প্রতিটা service তার নিজের কাজ করে, নিজের database রাখে, এবং network (HTTP/gRPC) এর মাধ্যমে অন্য services এর সাথে communicate করে।

002Monolith vs Microservices

আগে কী ছিল, এখন কী হয়েছে?

দুটো approach এর মূল পার্থক্য বোঝা দরকার — কারণ interview তে এই comparison প্রায়ই আসে।

MONOLITHUser ServiceProduct ServiceOrder ServicePayment ServiceNotification ServiceSingle DBMICROSERVICESUserServiceProductServiceOrderServicePaymentServiceNotificationServiceDBDBDBDB
বিষয়MonolithMicroservices
Deployপুরো app একসাথেপ্রতিটা service আলাদা
Scaleপুরো app scale করতে হয়শুধু দরকারী service scale করুন
Teamএকটা team সব কাজ করেপ্রতিটা service এর আলাদা team
Technologyএকটাই language/frameworkপ্রতিটা service ভিন্ন tech
Failureএকটা bug সব ভাঙেএকটা service fail হলে বাকিগুলো চলে
DatabaseShared single databaseপ্রতিটার নিজের database
ComplexitySimpleHigh (Network, tracing)
StartupIdeal for small teamsOverkill for small apps

IMPORTANT

Microservices মানে এই না যে সবসময় ভালো। Small startup এ monolith দিয়ে শুরু করা অনেক বেশি sensible। System জটিল হলে, team বড় হলে, তখন microservices এ migrate করুন। Amazon নিজেও monolith দিয়ে শুরু করেছেনিল!

003How It Works

Microservices কীভাবে কাজ করে?

একটা real e-commerce order flow দেখি — user order দিলে কী হয়:

ClientAPIGatewayOrderServiceInventoryServicePaymentServiceMessage Bus(Kafka/RabbitMQ)NotificationService1. Request2. Route3. Event4. Notify

Key Components

API GATEWAY

Single entry point। Client সব request এখানে পাঠায়। Rate limiting, auth, routing সব এখানে handle হয়। (Kong, AWS API Gateway, Nginx)

MESSAGE BUS

Services এর মধ্যে async communication। Direct call না করে event publish করুন। (Kafka, RabbitMQ, AWS SQS)

SERVICE REGISTRY

কোন service কোথায় আছে তার directory। Services নিজেদের register করে। (Consul, Eureka, Kubernetes DNS)

DISTRIBUTED TRACING

Request কোন কোন service এ গেছে track করা। একটা request এর পুরো journey দেখা। (Jaeger, Zipkin, Datadog)

004Design Patterns

Microservices Design Patterns

1. Database per Service Pattern

প্রতিটা service এর নিজের database থাকবেন। কোনো service অন্য service এর database সরাসরি access করবেন না। এতে loose coupling নিশ্চিত হয়।

WHY IT'S GOOD

User service PostgreSQL use করতে পারে, Product service MongoDB use করতে পারে, Cart service Redis use করতে পারে — প্রতিটা service তার কাজের জন্য best database choose করতে পারে।

2. Saga Pattern (Short Overview)

Multiple services এ একটা transaction। Order service → Payment service → Inventory service। কোনো step fail করলেন rollback। Topic 6 এ বিস্তারিত।

3. CQRS (Command Query Responsibility Segregation)

Read এবং Write এর জন্য আলাদা model। Write operations যায় Command side এ, Read operations যায় Query side এ। High traffic systems এ খুব useful।

CQRS PATTERNClientCommand HandlerQuery HandlerWrite DB (SQL)Read DB (Redis)Event Bus SyncWriteRead
005Code Examples

Practical Code — Python & Node.js

Python: FastAPI দিয়ে Order Microservice

order_service/main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import httpx  # অন্য service কে call করার জন্য
import asyncio

app = FastAPI(title="Order Service")

# অন্য services এর URL — real এ এটা env variable থেকে আসবেন
PAYMENT_SERVICE_URL = "http://payment-service:8001"
INVENTORY_SERVICE_URL = "http://inventory-service:8002"

class OrderRequest(BaseModel):
    user_id: int
    product_id: int
    quantity: int
    total_amount: float

# In-memory DB (real এ PostgreSQL হবে)
orders_db = {}
order_counter = 1

@app.post("/orders")
async def create_order(order: OrderRequest):
    global order_counter
    async with httpx.AsyncClient() as client:
        # Step 1: Inventory check করুন
        inv_response = await client.get(
            f"{INVENTORY_SERVICE_URL}/check/{order.product_id}/{order.quantity}"
        )
        if inv_response.status_code != 200:
            raise HTTPException(400, "Insufficient inventory")

        # Step 2: Payment process করুন
        pay_response = await client.post(
            f"{PAYMENT_SERVICE_URL}/charge",
            json={"user_id": order.user_id, "amount": order.total_amount}
        )
        if pay_response.status_code != 200:
            raise HTTPException(402, "Payment failed")

    # Step 3: Order save করুন
    order_id = order_counter
    orders_db[order_id] = {"id": order_id, "status": "confirmed", **order.dict()}
    order_counter += 1

    return {"order_id": order_id, "status": "confirmed"}

@app.get("/orders/{order_id}")
async def get_order(order_id: int):
    order = orders_db.get(order_id)
    if not order:
        raise HTTPException(404, "Order not found")
    return order

Node.js: Express দিয়ে Payment Microservice

payment_service/index.js
const express = require('express');
const app = express();
app.use(express.json());

// Mock payment processor
const payments = new Map();

app.post('/charge', async (req, res) => {
    const { user_id, amount } = req.body;

    // Simulate payment processing
    if (amount <= 0) {
        return res.status(400).json({ error: 'Invalid amount' });
    }

    const payment_id = `pay_${Date.now()}`;
    payments.set(payment_id, {
        user_id,
        amount,
        status: 'success',
        timestamp: new Date().toISOString()
    });

    // Real এ এখানে Stripe/bKash API call হবে
    res.json({
        payment_id,
        status: 'success',
        message: `Payment of ৳${amount} processed`
    });
});

app.get('/payment/:id', (req, res) => {
    const payment = payments.get(req.params.id);
    if (!payment) return res.status(404).json({ error: 'Not found' });
    res.json(payment);
});

app.listen(8001, () => console.log('Payment service running on :8001'));

Docker Compose: Services একসাথে চালানো

docker-compose.yml
version: '3.8'
services:
  api-gateway:
    image: nginx:alpine
    ports: ["80:80"]
    volumes: ["./nginx.conf:/etc/nginx/nginx.conf"]
    depends_on: [order-service, payment-service, inventory-service]

  order-service:
    build: ./order_service
    ports: ["8000:8000"]
    environment:
      - DATABASE_URL=postgresql://user:pass@order-db:5432/orders
    depends_on: [order-db, kafka]

  payment-service:
    build: ./payment_service
    ports: ["8001:8001"]

  inventory-service:
    build: ./inventory_service
    ports: ["8002:8002"]

  order-db:
    image: postgres:15
    environment:
      POSTGRES_DB: orders
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass

  kafka:
    image: confluentinc/cp-kafka:latest
    environment:
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
006Real World Use Cases

বাস্তব জীবনে Microservices

Netflix

৫০০+ microservices। প্রতিটা Netflix feature আলাদা service। Recommendation, streaming, billing সব আলাদা। প্রতি মাসে ১ billion+ API calls।

Amazon

2-pizza rule: প্রতিটা team এত ছোট হবে যে দুটো pizza তে খাওয়া যাবেন। প্রতিটা team তার service নিজেই deploy করে।

Uber

Monolith থেকে migrate করেছেনে। Rider, Driver, Maps, Payment সব আলাদা service। Peak hour এ শুধু Matching service scale করে।

Spotify

Squads system — প্রতিটা Squad একটা service own করে। Search, Playlist, Social সব আলাদা। ৩০+ countries এ simultaneously deploy।

Tools Comparison

Tool/PlatformTypeBest Forবাংলাদেশে ব্যবহার
Kubernetes (K8s)OrchestrationProduction microservicesGrowing
Docker ComposeLocal DevDevelopment ও testingVery Common
AWS ECS/EKSManagedCloud deploymentMedium
Kong / NGINXAPI GatewayRequest routingCommon
KafkaMessage QueueAsync communicationGrowing
Jaeger / ZipkinTracingDebugging distributed callsRare
007Interview Preparation

Common Interview Questions

Q1: Monolith vs Microservices কখন কোনটা choose করবেন?

Answer:Small team, early startup, simple domain — Monolith দিয়ে শুরু করুন। Complex domain, multiple teams, different scaling needs, independent deployments দরকার হলে Microservices। "Don't start with microservices" — Martin Fowler এর বিখ্যাত পরামর্শ।

Q2: Microservices এ inter-service communication কীভাবে হয়?

Synchronous: REST (HTTP), gRPC। Real-time response দরকার হলে।
Asynchronous: Message Queue (Kafka, RabbitMQ)। Event-driven, decoupled। Payment notification, email পাঠানো এ ধরনের কাজে।

Q3: Service কতটা ছোট হওয়া উচিত?

Answer:Single Responsibility Principle follow করুন। একটা service একটা business capability handle করবেন। "Microservice" মানে code এর size না — responsibility এর size। User management একটা service, Payment একটা service।

Q4: Distributed system এ data consistency কীভাবে manage করুন?

Answer: Eventual consistency। Strong consistency সব সময় possible না distributed এ। Saga pattern, event sourcing, compensating transactions ব্যবহার করুন। CAP theorem মনে রাখুন।
008Lesson Summary

SUMMARY — আজকে যা শিখলাম

Conceptএক লাইনে
Microservicesবড় app কে ছোট independent services এ ভাগ করুন
API GatewaySingle entry point, routing, auth, rate limiting
DB per Serviceপ্রতিটা service এর নিজের DB — loose coupling
Sync CommunicationREST/gRPC — real-time response দরকার হলে
Async CommunicationKafka/RabbitMQ — decoupled event-driven
CQRSRead ও Write আলাদা করুন — performance boost
When to useLarge teams, complex domain, independent scaling দরকার হলে
009Knowledge Check
010Assignments
011Practical Lab