Cloud ComputingUnit 410 min read

Cloud Programming Models: APIs, Serverless, Containers & Workflows

Unit 4 of Cloud Computing explores how applications are designed and deployed in the cloud, covering REST APIs, serverless architectures, containerization (Docker/Kubernetes), and workflow orchestration (AWS Step Functions, Azure Logic Apps). It explains their inner workings, real-world use cases, and trade-offs for sc

Key Concepts and Models

1. Cloud Programming Paradigms

Cloud computing introduces new ways to structure applications, shifting from monolithic architectures to modular, distributed, and event-driven designs. The three dominant paradigms are:

REST (Stateless, HTTP, JSON/XML)SOAP (WSDL, XML, WS-* standards)APIsAWS Lambda (Event-triggered, auto-scaling)Azure Functions (Pay-per-use, multi-language)Serverless (FaaS)Docker (Immutable, Dockerfile)Kubernetes (Auto-scaling, service discovery)ContainersAWS Step Functions (State machines, retries)Azure Logic Apps (Low-code workflows)WorkflowsCloud Programming Models
Hierarchy of cloud programming paradigms with real-world examples

APIs (Application Programming Interfaces)

APIs are the contracts that allow cloud services to communicate. Two primary types:

  • REST (Representational State Transfer): Uses HTTP methods (GET, POST, PUT, DELETE) and stateless interactions. Preferred for cloud due to scalability.
  • SOAP (Simple Object Access Protocol): Uses XML, WSDL, and relies on WS-* standards (e.g., WS-Security). More rigid but stricter for enterprise.

How REST Works (Example: eSewa API) When you pay a bill via eSewa:

  1. Your app sends a POST /payments request with JSON payload (amount, payer ID, merchant ID).
  2. eSewa’s cloud backend validates the request, checks balance, and processes the transaction.
  3. It returns a 200 OK with a transaction ID or 402 Payment Required if funds are insufficient.
sequenceDiagram
    participant User
    participant eSewaApp
    participant eSewaAPI
    participant BankAPI
    User->>eSewaApp: Initiates payment (₹500)
    eSewaApp->>eSewaAPI: POST /payments {amount:500, payer:"12345"}
    eSewaAPI->>BankAPI: Check balance (payer="12345")
    BankAPI-->>eSewaAPI: Balance: ₹1000
    eSewaAPI->>BankAPI: Deduct ₹500
    BankAPI-->>eSewaAPI: Success
    eSewaAPI-->>eSewaApp: 200 OK {txnId: "ABC123"}
    eSewaApp-->>User: Show success

Serverless Computing

Serverless abstracts infrastructure management, charging only for execution time. Key features:

  • Event-driven: Triggered by HTTP requests, database changes, or queues (e.g., AWS S3 uploads).
  • Auto-scaling: Zero to thousands of instances instantly.
  • Pay-per-use: Billed per millisecond (e.g., AWS Lambda charges $0.00001667 per GB-second).

Example: Pathao’s Ride Request Handling When you request a ride on Pathao:

  1. Your app sends a location update to Pathao’s AWS API Gateway.
  2. API Gateway triggers a Lambda function to:
    • Query DynamoDB for nearby drivers.
    • Calculate fare using a pricing algorithm.
    • Send push notifications to drivers via AWS SNS.
  3. Lambda scales automatically during peak hours (e.g., 7–9 PM in Kathmandu).
stateDiagram-v2
    [*] --> UserRequestsRide
    UserRequestsRide --> APIGateway: POST /ride-request
    APIGateway --> Lambda: Invoke "find_drivers"
    Lambda --> DynamoDB: Query drivers[location="nearby"]
    DynamoDB --> Lambda: Return 3 drivers
    Lambda --> SNS: Publish to "driver-notifications"
    SNS --> DriverApp: Push notification
    DriverApp --> [*]

Advantages/Disadvantages

Serverless Pros Cons
AWS Lambda No server management Cold starts (~100ms latency)
Azure Functions Built-in CI/CD Vendor lock-in
Google Cloud Functions Pay-per-use Limited execution time (15 min)

2. Containerization and Orchestration

Containers package an app and its dependencies into isolated, portable units. Unlike VMs, they share the host OS kernel.

Docker: The Container Standard

  • Dockerfile: Defines the container’s environment (e.g., FROM python:3.9, COPY app.py /, CMD ["python", "app.py"]).
  • Images: Immutable templates (e.g., nginx:latest, postgres:13).
  • Containers: Running instances of images.

Example: Deploying a Daraz Order Processing Service Daraz uses Docker to run its order processing microservice:

# Dockerfile for Daraz Order Service
FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "order_app:app"]
  • Image: Built once, deployed anywhere (on-prem or AWS ECS).
  • Orchestration: Kubernetes manages scaling (e.g., 10 containers during Black Friday).

Kubernetes (K8s): Container Orchestration

Kubernetes automates deployment, scaling, and operations of containerized apps. Key components:

  • Pods: Smallest deployable units (1+ containers sharing storage/network).
  • Services: Stable IP/DNS for pods (e.g., order-service:8000).
  • Deployments: Ensures desired pod count (e.g., 5 replicas).
  • Ingress: Manages external HTTP/HTTPS routes (e.g., daraz.com/orders).
UserServiceDeploymentPodContainer
Kubernetes object hierarchy from top to bottom

Example: Ncell’s Traffic Monitoring with K8s Ncell’s traffic monitoring dashboard runs on Kubernetes:

  1. Prometheus containers scrape metrics from base stations.
  2. Grafana pods visualize data.
  3. Horizontal Pod Autoscaler (HPA) scales Grafana during peak hours (e.g., 4–6 PM).
MetricsDataScaling RequestPod AdjustmentPrometheusGrafanaHPABase Stations
Kubernetes traffic monitoring system architecture

Advantages/Disadvantages

Kubernetes Pros Cons
Self-healing Auto-restarts failed pods Steep learning curve
Auto-scaling Scales to thousands of pods Complex YAML configurations
Multi-cloud Runs on AWS EKS, GCP GKE, Azure AKS High operational overhead

3. Cloud Workflow Orchestration

Workflows automate multi-step processes (e.g., order fulfillment, data pipelines). Two approaches:

  1. Orchestration (Centralized): A master process controls steps (e.g., AWS Step Functions).
  2. Choreography (Decentralized): Services communicate via events (e.g., Apache Kafka).

Example: Khalti’s Payment Workflow When you transfer money via Khalti:

  1. Step 1: User app sends POST /transfer to Khalti API.
  2. Step 2: Step Function triggers:
    • Lambda A: Validate sender balance.
    • Lambda B: Deduct funds from sender’s account.
    • Lambda C: Credit receiver’s account.
    • Lambda D: Send SMS notification.
  3. Error Handling: If Lambda B fails, Step Function retries 3 times before notifying support.
sequenceDiagram
    participant User
    participant KhaltiAPI
    participant StepFunction
    participant LambdaA
    participant LambdaB
    participant LambdaC
    participant SMSGateway
    User->>KhaltiAPI: POST /transfer {amount:1000}
    KhaltiAPI->>StepFunction: Start "payment_workflow"
    StepFunction->>LambdaA: Validate balance
    LambdaA-->>StepFunction: Success
    StepFunction->>LambdaB: Deduct funds
    LambdaB-->>StepFunction: Success
    StepFunction->>LambdaC: Credit receiver
    LambdaC-->>StepFunction: Success
    StepFunction->>SMSGateway: Send notification
    SMSGateway-->>User: "₹1000 transferred"

Tools Comparison

Tool Use Case Cloud Provider
AWS Step Functions State machines, retries AWS
Azure Logic Apps Low-code workflows Azure
Google Workflows Event-driven automation Google Cloud
Apache Airflow Batch data pipelines Open-source

In the Real World

  1. eSewa’s API-Driven Payments

    • Idea Used: REST APIs + Serverless (AWS Lambda).
    • How: eSewa’s backend uses REST APIs for merchant transactions and Lambda functions to process payments in real-time. During Dashain, Lambda scales to handle 10,000+ transactions per second.
  2. Pathao’s Serverless Ride Matching

    • Idea Used: Serverless (AWS Lambda) + Event-driven architecture.
    • How: When you request a ride, Pathao’s Lambda function queries DynamoDB for nearby drivers and pushes notifications via SNS. This avoids over-provisioning servers during off-peak hours.
  3. Ncell’s Kubernetes-Based Traffic Monitoring

    • Idea Used: Container orchestration (Kubernetes) + Auto-scaling.
    • How: Ncell’s Prometheus + Grafana dashboards run in Kubernetes pods. During network congestion (e.g., festivals), the Horizontal Pod Autoscaler adds more Grafana instances to handle increased monitoring requests.
  4. Daraz’s Containerized Microservices

    • Idea Used: Docker + Kubernetes.
    • How: Daraz’s order processing, inventory, and payment services run in Docker containers, orchestrated by Kubernetes. During sales events (e.g., 11.11), Kubernetes scales these services to handle 10x traffic.

Exam Tip

This unit is heavily tested on:

  1. API Design: Know the difference between REST and SOAP, HTTP methods, and status codes (e.g., 200, 404, 500).
  2. Serverless: Be able to draw a sequence diagram for a serverless workflow (e.g., Lambda + DynamoDB + SNS).
  3. Containers: Explain Dockerfiles, images vs. containers, and Kubernetes components (Pods, Services, Deployments).
  4. Workflows: Compare orchestration (Step Functions) vs. choreography (Kafka) with examples.
  5. Real-World Mapping: Relate concepts to Nepali companies (e.g., Khalti’s Lambda functions, Daraz’s Kubernetes).

Common Pitfalls:

  • Confusing virtualization (VMs) with containerization (Docker).
  • Forgetting that serverless functions have cold starts.
  • Mixing up Kubernetes Services (networking) with Deployments (scaling).

High-Score Strategy:

  • Draw mermaid diagrams for workflows (e.g., Pathao’s ride request).
  • Compare REST vs. SOAP in a table with pros/cons.
  • Explain one real-world example (e.g., eSewa API) in detail with a sequence diagram.

Based on the TU BIT syllabus for Cloud Computing, unit 4.

Discussion

Loading…