# asheriff-bot/17-647-assignment-a4 architecture

> Architecture diagram of the GitHub repository asheriff-bot/17-647-assignment-a4, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/asheriff-bot/17-647-assignment-a4
- Repository: https://github.com/asheriff-bot/17-647-assignment-a4

## Overview

This repository implements a small bookstore platform as a set of Python microservices with two client-facing BFF layers, an internal routing tier, and Kubernetes deployment assets for AWS EKS.

1. **Top-level architecture**
   The system is split into:
   - **Web BFF** and **Mobile BFF** at the edge
   - **Backend services** for books, customers, and CRM
   - A shared **nginx backend-router** inside the cluster
   - A **book data sync** job for read-model synchronization
   - A **shared** Python library for common BFF logic

2. **Client entry points**
   `web_bff/` and `mobile_bff/` are the public HTTP entry points. They authenticate requests with JWT, require an `X-Client-Type` header, and then proxy downstream calls. The mobile BFF also reshapes some responses differently from the web BFF.

3. **BFF responsibilities**
   The BFFs are not just pass-through proxies:
   - they validate JWTs using shared auth helpers
   - they enforce client type routing rules (`Web` vs `iOS`/`Android`)
   - they apply client-specific response transformations
   - they forward requests to the backend router rather than directly to services

4. **Internal routing boundary**
   `k8s/backend-router.yaml` and the nginx config in `scripts/nginx-backend.conf` define an internal routing layer. The BFFs talk to this single backend endpoint, which routes book-related paths and customer-related paths to the appropriate service. This creates a clean boundary between edge concerns and backend service topology.

5. **Book domain**
   The book domain is split into `book_command_service/`, `book_query_service/`, and `book_data_sync/`. This strongly suggests a CQRS-style separation:
   - the **command service** handles writes and mutation endpoints
   - the **query service** handles read endpoints
   - the **sync job** keeps the read side aligned with the write side or indexed data
   The README also indicates a related-books flow that depends on an external recommendation API and includes timeout/circuit-breaker behavior.

6. **Customer domain**
   `customer_service/` owns customer data and CRUD-style customer APIs. It is backed by a relational database and is the source of customer events for downstream processing. The repository includes SQL initialization and truncation scripts for the customer and books databases.

7. **CRM/event processing**
   `crm_service/` is an event-driven consumer. It receives customer events via Kafka and sends outbound email using SMTP settings provided through deployment environment variables. This makes CRM a side-effect service rather than a synchronous request handler.

8. **Data flow across services**
   A typical request path is:
   client → BFF → backend-router → domain service → database.
   For customer creation, the flow extends to:
   customer_service → Kafka → crm_service → email delivery.
   For book reads, the flow can involve:
   client → BFF → backend-router → book query service → read model/index.

9. **Shared code and cross-cutting concerns**
   `shared/` contains code reused by both BFFs:
   - JWT validation helpers
   - client-type and auth checks
   - response formatting helpers
   - book/customer transformation logic
   This avoids duplicating edge behavior across the two frontends.

10. **Infrastructure and deployment**
   The repo is designed for both local and AWS deployment:
   - `docker-compose.yml` runs the full stack locally
   - `k8s/*.yaml` contains Kubernetes manifests for EKS
   - `scripts/render_k8s_from_env.sh` generates concrete manifests from environment values
   - `CF-A4.yml` / `CF-A4-cmu.yml` provide optional AWS infrastructure provisioning
   The Kubernetes manifests include a namespace, services, and a CronJob for data sync.

11. **Runtime and operational model**
   All services are Python/Flask-based and containerized with per-service Dockerfiles. Health checking is standardized with `/status`, and the manifests are set up for Kubernetes probes and service exposure. Secrets and environment-specific endpoints are injected through deploy-time env files rather than hardcoded values.

12. **Local vs production dependencies**
   In local compose, the stack includes MySQL, Kafka, the services, and a mock recommendation API. In EKS, the services depend on externally provisioned infrastructure such as Aurora writer access and Kafka/MSK connectivity, which are configured through the deployment environment and manifests.

13. **How to draw the architecture**
   A good diagram should show:
   - two client apps entering through two BFFs
   - both BFFs going to a single backend-router
   - backend-router splitting traffic to book and customer services
   - customer service emitting Kafka events to CRM
   - book command/query separation with a sync process between them
   - shared JWT/transformation code used only by the BFFs
   - databases and SMTP/Kafka as external dependencies

Main entry points: [`docker-compose.yml`](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/docker-compose.yml) (Compose Stack), [`web_bff/app.py`](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/web_bff/app.py) (Web BFF), [`mobile_bff/app.py`](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/mobile_bff/app.py) (Mobile BFF), [`k8s/backend-router.yaml`](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/k8s/backend-router.yaml) (Router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Edge BFFs"]
  node_web_bff["Web BFF<br/>Flask BFF<br/>[app.py]"]
  node_mobile_bff["Mobile BFF<br/>Flask BFF<br/>[app.py]"]
end

subgraph group_backend["Domain services"]
  node_book_command_service["Book Command<br/>write API<br/>[app.py]"]
  node_book_query_service["Book Query<br/>read API<br/>[app.py]"]
  node_book_data_sync["Book Sync<br/>cron sync<br/>[sync.py]"]
  node_customer_service["Customer Service<br/>CRUD API<br/>[app.py]"]
  node_crm_service["CRM Service<br/>event consumer<br/>[app.py]"]
end

subgraph group_shared["Shared code"]
  node_bff_auth["JWT Auth<br/>shared helper<br/>[bff_auth.py]"]
  node_client_headers["Client Types<br/>shared helper<br/>[client_headers.py]"]
  node_bff_transform["Transforms<br/>shared helper"]
end

subgraph group_infra["Infra assets"]
  node_backend_router["Router<br/>nginx route"]
  node_k8s_assets["K8s Assets<br/>deploy manifests<br/>[namespace.yaml]"]
  node_render_script["Render Script<br/>deploy script"]
  node_compose["Compose Stack<br/>local deploy<br/>[docker-compose.yml]"]
end

subgraph group_external["External systems"]
  node_books_db[("Books DB<br/>database<br/>[init_db.sql]")]
  node_customers_db[("Customers DB<br/>database<br/>[init_db.sql]")]
  node_kafka["Kafka<br/>event bus"]
  node_smtp["SMTP<br/>email relay"]
  node_recommend_api["Reco API<br/>external API"]
end

node_web_bff -->|"validate"| node_bff_auth
node_mobile_bff -->|"validate"| node_bff_auth
node_web_bff -->|"route"| node_client_headers
node_mobile_bff -->|"route"| node_client_headers
node_web_bff -->|"shape"| node_bff_transform
node_mobile_bff -->|"shape"| node_bff_transform
node_web_bff -->|"proxy"| node_backend_router
node_mobile_bff -->|"proxy"| node_backend_router
node_backend_router -->|"books write"| node_book_command_service
node_backend_router -->|"books read"| node_book_query_service
node_backend_router -->|"customers"| node_customer_service
node_book_command_service -->|"write"| node_books_db
node_book_query_service -->|"read"| node_books_db
node_book_data_sync -->|"sync"| node_books_db
node_customer_service -->|"write"| node_customers_db
node_customer_service -->|"emit"| node_kafka
node_crm_service -->|"consume"| node_kafka
node_crm_service -->|"send"| node_smtp
node_book_command_service -->|"related books"| node_recommend_api
node_k8s_assets -.->|"deploy"| node_backend_router
node_k8s_assets -.->|"deploy"| node_web_bff
node_k8s_assets -.->|"deploy"| node_mobile_bff
node_k8s_assets -.->|"deploy"| node_book_data_sync
node_render_script -.->|"render"| node_k8s_assets
node_compose -.->|"run"| node_web_bff
node_compose -.->|"run"| node_mobile_bff
node_compose -.->|"run"| node_book_command_service
node_compose -.->|"run"| node_book_query_service
node_compose -.->|"run"| node_customer_service
node_compose -.->|"run"| node_crm_service

click node_web_bff "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/web_bff/app.py"
click node_mobile_bff "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/mobile_bff/app.py"
click node_backend_router "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/k8s/backend-router.yaml"
click node_book_command_service "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/book_command_service/app.py"
click node_book_query_service "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/book_query_service/app.py"
click node_book_data_sync "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/book_data_sync/sync.py"
click node_customer_service "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/customer_service/app.py"
click node_crm_service "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/crm_service/app.py"
click node_bff_auth "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/shared/bff_auth.py"
click node_client_headers "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/shared/client_headers.py"
click node_bff_transform "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/shared/bff_book_transform.py"
click node_k8s_assets "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/k8s/namespace.yaml"
click node_render_script "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/scripts/render_k8s_from_env.sh"
click node_compose "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/docker-compose.yml"
click node_books_db "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/scripts/init_db.sql"
click node_customers_db "https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/scripts/init_db.sql"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web_bff,node_mobile_bff toneBlue
class node_book_command_service,node_book_query_service,node_book_data_sync,node_customer_service,node_crm_service toneAmber
class node_bff_auth,node_client_headers,node_bff_transform toneMint
class node_backend_router,node_k8s_assets,node_render_script,node_compose toneRose
class node_books_db,node_customers_db,node_kafka,node_smtp,node_recommend_api toneIndigo
```

## Components

### Edge BFFs

Public client-facing request handlers

- [Web BFF](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/web_bff/app.py): Flask BFF · Public HTTP entry point for web clients · `web_bff/app.py`
- [Mobile BFF](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/mobile_bff/app.py): Flask BFF · Public HTTP entry point for mobile clients · `mobile_bff/app.py`

### Domain services

Core bookstore and customer services

- [Book Command](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/book_command_service/app.py): write API · Book mutation and write-side endpoints · `book_command_service/app.py`
- [Book Query](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/book_query_service/app.py): read API · Book read-side endpoints and query model · `book_query_service/app.py`
- [Book Sync](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/book_data_sync/sync.py): cron sync · Keeps the read model aligned with source data · `book_data_sync/sync.py`
- [Customer Service](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/customer_service/app.py): CRUD API · Owns customer records and emits customer events · `customer_service/app.py`
- [CRM Service](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/crm_service/app.py): event consumer · Consumes customer events and sends outbound email · `crm_service/app.py`

### Shared code

Reusable BFF helpers

- [JWT Auth](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/shared/bff_auth.py): shared helper · JWT validation and auth checks used by BFFs · `shared/bff_auth.py`
- [Client Types](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/shared/client_headers.py): shared helper · Client-type header validation and routing rules · `shared/client_headers.py`
- [Transforms](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/shared/bff_book_transform.py): shared helper · Response shaping and book/customer mapping helpers · `shared/bff_book_transform.py`

### Infra assets

Deployment and routing configuration

- [Router](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/k8s/backend-router.yaml): nginx route · Internal routing boundary for book and customer traffic · `k8s/backend-router.yaml`
- [K8s Assets](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/k8s/namespace.yaml): deploy manifests · Namespace, service, and CronJob manifests for EKS · `k8s/namespace.yaml`
- [Render Script](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/scripts/render_k8s_from_env.sh): deploy script · Generates manifests from environment values · `scripts/render_k8s_from_env.sh`
- [Compose Stack](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/docker-compose.yml): local deploy · Local multi-service runtime definition · `docker-compose.yml`

### External systems

Datastores and outbound integrations

- [Books DB](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/scripts/init_db.sql): database · Persistent store for book data and read/write state · `scripts/init_db.sql`
- [Customers DB](https://github.com/asheriff-bot/17-647-assignment-a4/blob/main/scripts/init_db.sql): database · Persistent store for customer data · `scripts/init_db.sql`
- **Kafka**: event bus · Carries customer events to CRM
- **SMTP**: email relay · Outbound email delivery for CRM
- **Reco API**: external API · Related-books recommendation dependency

## Connections

- Web BFF → JWT Auth: validate · Checks JWTs before forwarding requests
- Mobile BFF → JWT Auth: validate · Checks JWTs before forwarding requests
- Web BFF → Client Types: route · Enforces web client type rules
- Mobile BFF → Client Types: route · Enforces mobile client type rules
- Web BFF → Transforms: shape · Applies web-facing response formatting
- Mobile BFF → Transforms: shape · Applies mobile-facing response formatting
- Web BFF → Router: proxy · Forwards API traffic to internal router
- Mobile BFF → Router: proxy · Forwards API traffic to internal router
- Router → Book Command: books write · Routes book mutation paths
- Router → Book Query: books read · Routes book query paths
- Router → Customer Service: customers · Routes customer paths
- Book Command → Books DB: write · Persists book mutations
- Book Query → Books DB: read · Serves book reads from database
- Book Sync → Books DB: sync · Refreshes read-side data from source state
- Customer Service → Customers DB: write · Persists customer records
- Customer Service → Kafka: emit · Publishes customer events
- CRM Service → Kafka: consume · Consumes customer events
- CRM Service → SMTP: send · Delivers outbound email
- Book Command → Reco API: related books · Calls recommendation service with timeout protection
- K8s Assets → Router: deploy · Defines cluster routing boundary
- K8s Assets → Web BFF: deploy · Defines web BFF workload
- K8s Assets → Mobile BFF: deploy · Defines mobile BFF workload
- K8s Assets → Book Sync: deploy · Defines CronJob for sync
- Render Script → K8s Assets: render · Produces environment-specific manifests
- Compose Stack → Web BFF: run · Starts local web stack
- Compose Stack → Mobile BFF: run · Starts local mobile stack
- Compose Stack → Book Command: run · Starts local book write service
- Compose Stack → Book Query: run · Starts local book read service
- Compose Stack → Customer Service: run · Starts local customer service
- Compose Stack → CRM Service: run · Starts local CRM service
