Skip to content

trungthudo13/sysops-circuit-breaker-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Circuit Breaker Lab (Django + DRF + Daphne)

This demo environment showcases Circuit Breaker behavior implemented with Django and Django REST framework (DRF), running under the Daphne ASGI webserver. The system includes three services (frontier, midline, backline) that call one another to simulate successful and timeout scenarios and observe application‑level Circuit Breaker reactions.

Tech Stack

  • Django + DRF: Build APIs via rest_framework.views.APIView.
  • Daphne: ASGI webserver, entrypoint CircuitBreaker.asgi:application.
  • Nginx: Reverse proxy to the frontier service (host port 8001).
  • Docker/Compose: Packaging and running multiple services.

Build and Run

  • Build/run script: ./dockerc.sh
    • Builds the image circuit-breaker:latest from docker/Dockerfile.
    • Starts services via docker/docker-compose.yaml (frontier, midline, backline, nginx).

Execute:

./dockerc.sh

Once running, access via Nginx at: http://localhost:8001/

Key APIs

Routes are mounted per app. Quick reference:

  • Frontier (public via Nginx, port 8001):
    • GET /api/success-request
    • GET /api/timeout-request
  • Midline and Backline (internal in the compose network) expose the same routes to form a call chain.

Disable Circuit Breaker per request via query param:

  • ?non_circuit_breaker=1

Examples:

curl "http://localhost:8001/api/success-request"
curl "http://localhost:8001/api/timeout-request"
curl "http://localhost:8001/api/timeout-request?non_circuit_breaker=1"

API Docs (OpenAPI/Swagger)

  • OpenAPI schema: GET /api/schema/
  • Swagger UI: GET /api/schema/swagger-ui/

Via Nginx (host):

  • http://localhost:8001/api/schema/
  • http://localhost:8001/api/schema/swagger-ui/

Relevant Configuration

  • ASGI: src/CircuitBreaker/asgi.py and Daphne entrypoint in docker/entrypoint.sh.
  • Base DRF view: src/apps/sharedapp/apis/proxyrequest.py (handles non_circuit_breaker, returns 503 when circuit is open, 502 for transient errors).
  • Compose: docker/docker-compose.yaml (defines the 3 services and Nginx proxy on port 8001).
  • Nginx config: configs/nginx.conf (upstream to frontier:8000).

Ops Notes

  • On first start, the container runs migrate and collectstatic automatically (see docker/entrypoint.sh).
  • Environment variables that link service calls are set in compose (e.g., FRONTIER_DOMAIN, MIDLINE_DOMAIN, BACKLINE_DOMAIN).

Istio Circuit Breaker (Kind)

  • Goal: Run the same services on a local Kind cluster with Istio and apply network‑level circuit breaking to backline using DestinationRule and VirtualService.

Prerequisites

  • docker, kind, kubectl, istioctl available in PATH.

Build image and initialize cluster

  1. Build the Docker image locally:
./dockerc.sh
  1. Create Kind cluster, install Istio, deploy manifests, and apply CB policies:
./k8sinitialrc.sh

Expose Istio Ingress (Kind)

  • Port‑forward the Istio ingressgateway to host port 8002:
kubectl -n istio-system port-forward svc/istio-ingressgateway 8002:80
  • Access Frontier via Istio at: http://localhost:8002/

Apply updates after code changes

  • Re‑apply manifests and reload the image into Kind:
./k8sreloadrc.sh

Istio CB config in this repo

  • docker/k8s/circuit-breaker.yaml defines:
    • DestinationRule for backline with connection pool limits and outlier detection.
    • VirtualService for backline with timeout and retries.
  • Traffic flow: frontier -> (midline) -> backline via cluster DNS. The CB applies to calls targeting backline.circuit-breaker.svc.cluster.local.

Load testing

  • Use the provided script to send requests and observe HTTP codes/latency:
    • For Docker Compose (Nginx, port 8001), default in curlspam.sh is already set.
    • For Istio/Kind (port 8002), edit curlspam.sh to use:
      • URL="http://localhost:8002/api/timeout-request?non_circuit_breaker=true"
./curlspam.sh
  • You can also run one‑offs:
curl -i http://localhost:8002/api/timeout-request
curl -i "http://localhost:8002/api/timeout-request?non_circuit_breaker=true"

Local Development (optional)

  • Requires Python 3.13 if running outside Docker.
  • Install dependencies from src/requirements.txt and run python src/manage.py runserver, or start Daphne manually.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published