# shail2001/distributed-flash-sale-system architecture

> Architecture diagram of the GitHub repository shail2001/distributed-flash-sale-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shail2001/distributed-flash-sale-system
- Repository: https://github.com/shail2001/distributed-flash-sale-system

## Overview

1. **Overall shape**
   This repo implements a distributed flash-sale pipeline, not a single monolith. The core runtime path is: external traffic enters through an AWS Application Load Balancer, is optionally shaped by a waiting-room service, then flows into a purchase API, an SQS queue, a background worker, and finally DynamoDB for durable order storage.

2. **Request-facing service: `flash-sale-api`**
   The `services/flash-sale-api` Go service is the synchronous edge of the system. Its handlers are split by function: inventory reads, purchase submission, and reset/admin-style inventory reset. It exposes the user-facing HTTP API and owns the logic that decides whether a purchase can be accepted.

3. **Inventory state and concurrency control**
   The API uses Redis as the inventory counter/cache layer through a dedicated Redis client package. The README indicates the main architectural goal here is atomic inventory reduction with zero oversell under high concurrency. That makes Redis the critical coordination point for fast acceptance/rejection decisions before any durable order write occurs.

4. **Asynchronous order handoff**
   After a successful purchase decision, the API publishes an order message to SQS via its `sqs/publisher.go` component. This is the key decoupling boundary: acceptance of an order is handled synchronously, while durable fulfillment is handed off asynchronously to smooth load and isolate downstream latency.

5. **Background processing: `order-worker`**
   The `services/order-worker` Go service is the consumer side of the pipeline. Its internal package layout shows clear sub-boundaries for config, metrics, retry logic, domain model, business service, and a DynamoDB store. Operationally, it long-polls SQS in batches, processes messages concurrently, and persists orders to DynamoDB.

6. **Durability and idempotency**
   The worker writes each order using idempotent behavior: the README says it uses a conditional insert on `order_id`, and updates an inventory audit table in the same DynamoDB transaction. That means DynamoDB is the system of record for completed orders, and the worker is designed to tolerate retries without duplicating orders.

7. **Queue semantics and failure handling**
   SQS is the buffer between the stateless API and the durable worker. The repo also provisions a dead-letter queue in Terraform, which implies failed messages are isolated for later inspection rather than blocking the main queue. The worker deletes messages only after the DynamoDB transaction succeeds, preserving at-least-once delivery semantics with application-level idempotency.

8. **Waiting-room boundary**
   `services/waiting-room` exists as a separate service but is marked in the README as in progress. Architecturally, it sits in front of the flash-sale API to shape traffic and issue tokens before requests reach the purchase path. It is a planned admission-control layer rather than part of the completed order-processing core.

9. **Infrastructure layer**
   Terraform is the control plane for the entire system. Modules are clearly separated into network, ALB, ECS, Redis, SQS, DynamoDB, ECR, and logging. That means the architecture is intended to be reproducible and fully cloud-native: containers run on ECS Fargate, images are stored in ECR, logs go to CloudWatch, and core state lives in managed AWS services.

10. **Operational scripts and deployment flow**
   The `scripts/` directory contains deployment and smoke-test helpers, especially for rolling out the order worker and validating the end-to-end purchase flow. The deployment script builds/pushes the worker image and updates ECS; the smoke-test script exercises the full purchase-to-DynamoDB path in AWS.

11. **Testing and experimentation**
   The `tests/` tree is organized around the major boundaries of the system. There are API load/benchmark/quantity tests, an AWS-backed order-flow smoke test, and worker unit tests for config and model behavior. The presence of benchmark charts and results files shows this repo is also being used to evaluate throughput and oversell behavior under controlled load.

12. **Key architectural constraint**
   The most important design idea in the repo is separation between fast admission and durable completion: Redis and the API decide whether inventory can be consumed, SQS absorbs bursty writes, and the worker/DynamoDB layer finalizes orders safely. That separation is what lets the system handle flash-sale concurrency without overselling while keeping the API responsive.

Main entry points: [`services/flash-sale-api/handlers/purchase.go`](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/handlers/purchase.go) (Purchase), [`terraform/modules/network/main.tf`](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/network/main.tf) (Network), [`services/order-worker/internal/service/service.go`](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/service/service.go) (Worker service), [`services/order-worker/internal/store/dynamodb.go`](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/store/dynamodb.go) (Dynamo store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_services["Services"]
  node_waiting_room["Waiting room<br/>admission gate<br/>[.gitkeep]"]
  node_api_main["API main<br/>go service<br/>[main.go]"]
  node_api_handlers["Handlers<br/>http endpoints<br/>[handler.go]"]
  node_api_inventory["Inventory<br/>read/reset logic<br/>[inventory.go]"]
  node_api_purchase["Purchase<br/>acceptance logic<br/>[purchase.go]"]
  node_redis_client[("Redis client<br/>cache coordination<br/>[client.go]")]
  node_sqs_pub["SQS publisher<br/>async handoff<br/>[publisher.go]"]
  node_worker_main["Worker main<br/>go consumer<br/>[main.go]"]
  node_worker_service["Worker service<br/>batch processor<br/>[service.go]"]
  node_worker_retry["Retry<br/>resilience<br/>[retry.go]"]
  node_worker_store[("Dynamo store<br/>persistent write<br/>[dynamodb.go]")]
end

subgraph group_grp_infra["Infrastructure"]
  node_alb["ALB<br/>traffic entry<br/>[main.tf]"]
  node_sqs_queue["SQS queue<br/>buffer<br/>[main.tf]"]
  node_dynamodb[("DynamoDB<br/>system of record<br/>[main.tf]")]
  node_ecs["ECS<br/>runtime platform<br/>[main.tf]"]
  node_network["Network<br/>[main.tf]"]
  node_redis[("Redis<br/>inventory cache<br/>[main.tf]")]
  node_logging["Logging<br/>observability<br/>[main.tf]"]
end

subgraph group_grp_ops["Ops & Tests"]
  node_deploy_script["Deploy worker<br/>deployment script"]
  node_smoke_script["Smoke test<br/>validation script"]
  node_api_tests["API tests<br/>load tests<br/>[load_test.go]"]
  node_order_flow_tests["Order flow<br/>integration test<br/>[smoke_test.go]"]
  node_worker_tests["Worker tests<br/>unit tests<br/>[config_test.go]"]
end

node_alb -->|"fronts"| node_waiting_room
node_waiting_room -->|"forwards"| node_api_main
node_alb -->|"routes"| node_api_main
node_api_main -->|"serves"| node_api_handlers
node_api_handlers -->|"reads/resets"| node_api_inventory
node_api_handlers -->|"submits"| node_api_purchase
node_api_purchase -->|"checks stock"| node_redis_client
node_redis_client -->|"stores"| node_redis
node_api_purchase -->|"enqueues"| node_sqs_pub
node_sqs_pub -->|"publishes"| node_sqs_queue
node_sqs_queue -->|"delivers"| node_worker_main
node_worker_main -->|"starts"| node_worker_service
node_worker_service -->|"wraps"| node_worker_retry
node_worker_service -->|"persists"| node_worker_store
node_worker_store -->|"writes"| node_dynamodb
node_worker_store -->|"emits"| node_logging
node_ecs -->|"runs"| node_api_main
node_ecs -->|"runs"| node_worker_main
node_network -->|"supports"| node_alb
node_network -->|"supports"| node_ecs
node_network -->|"supports"| node_redis
node_network -->|"integrates"| node_dynamodb
node_deploy_script -->|"updates"| node_ecs
node_smoke_script -->|"exercises"| node_api_purchase
node_smoke_script -->|"verifies"| node_worker_store
node_api_tests -->|"measures"| node_api_purchase
node_order_flow_tests -->|"passes through"| node_sqs_queue
node_worker_tests -->|"covers"| node_worker_service

click node_alb "https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/alb/main.tf"
click node_waiting_room "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/waiting-room/.gitkeep"
click node_api_main "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/main.go"
click node_api_handlers "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/handlers/handler.go"
click node_api_inventory "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/handlers/inventory.go"
click node_api_purchase "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/handlers/purchase.go"
click node_redis_client "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/redis/client.go"
click node_sqs_pub "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/sqs/publisher.go"
click node_sqs_queue "https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/sqs/main.tf"
click node_worker_main "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/main.go"
click node_worker_service "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/service/service.go"
click node_worker_retry "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/retry/retry.go"
click node_worker_store "https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/store/dynamodb.go"
click node_dynamodb "https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/dynamodb/main.tf"
click node_ecs "https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/ecs/main.tf"
click node_network "https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/network/main.tf"
click node_redis "https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/redis/main.tf"
click node_logging "https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/logging/main.tf"
click node_deploy_script "https://github.com/shail2001/distributed-flash-sale-system/blob/main/scripts/deploy_order_worker.sh"
click node_smoke_script "https://github.com/shail2001/distributed-flash-sale-system/blob/main/scripts/run_order_flow_smoke_test.sh"
click node_api_tests "https://github.com/shail2001/distributed-flash-sale-system/blob/main/tests/flash-sale-api/load_test.go"
click node_order_flow_tests "https://github.com/shail2001/distributed-flash-sale-system/blob/main/tests/order-flow/smoke_test.go"
click node_worker_tests "https://github.com/shail2001/distributed-flash-sale-system/blob/main/tests/order-worker/config_test.go"

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_waiting_room,node_api_main,node_api_handlers,node_api_inventory,node_api_purchase,node_redis_client,node_sqs_pub,node_worker_main,node_worker_service,node_worker_retry,node_worker_store toneBlue
class node_alb,node_sqs_queue,node_dynamodb,node_ecs,node_network,node_redis,node_logging toneAmber
class node_deploy_script,node_smoke_script,node_api_tests,node_order_flow_tests,node_worker_tests toneMint
```

## Components

### Services

Runtime components that handle traffic, queueing, and order persistence

- [Waiting room](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/waiting-room/.gitkeep): admission gate · Planned front-door traffic shaping and token issuance layer · `services/waiting-room/.gitkeep`
- [API main](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/main.go): go service · Bootstraps the flash-sale API process · `services/flash-sale-api/main.go`
- [Handlers](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/handlers/handler.go): http endpoints · Inventory, purchase, and reset request handling · `services/flash-sale-api/handlers/handler.go`
- [Inventory](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/handlers/inventory.go): read/reset logic · Inventory reads and reset-style state operations · `services/flash-sale-api/handlers/inventory.go`
- [Purchase](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/handlers/purchase.go): acceptance logic · Synchronously decides whether a purchase can be accepted · `services/flash-sale-api/handlers/purchase.go`
- [Redis client](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/redis/client.go): cache coordination · Redis-backed inventory counter and fast concurrency control · `services/flash-sale-api/redis/client.go`
- [SQS publisher](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/flash-sale-api/sqs/publisher.go): async handoff · Publishes accepted orders to the queue for background processing · `services/flash-sale-api/sqs/publisher.go`
- [Worker main](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/main.go): go consumer · Bootstraps the order-worker process · `services/order-worker/main.go`
- [Worker service](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/service/service.go): batch processor · Consumes SQS messages and coordinates order completion · `services/order-worker/internal/service/service.go`
- [Retry](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/retry/retry.go): resilience · Retry handling for transient worker failures · `services/order-worker/internal/retry/retry.go`
- [Dynamo store](https://github.com/shail2001/distributed-flash-sale-system/blob/main/services/order-worker/internal/store/dynamodb.go): persistent write · Idempotent order writes and transactional inventory audit updates · `services/order-worker/internal/store/dynamodb.go`

### Infrastructure

Terraform-managed cloud resources and deployment plumbing

- [ALB](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/alb/main.tf): traffic entry · AWS Application Load Balancer entry point for external requests · `terraform/modules/alb/main.tf`
- [SQS queue](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/sqs/main.tf): buffer · Decouples bursty purchase traffic from downstream fulfillment · `terraform/modules/sqs/main.tf`
- [DynamoDB](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/dynamodb/main.tf): system of record · Durable storage for completed orders and audit state · `terraform/modules/dynamodb/main.tf`
- [ECS](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/ecs/main.tf): runtime platform · Runs the containerized services on Fargate · `terraform/modules/ecs/main.tf`
- [Network](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/network/main.tf): cloud network · Base VPC and connectivity layer for the deployment · `terraform/modules/network/main.tf`
- [Redis](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/redis/main.tf): inventory cache · Managed Redis used for atomic inventory reduction · `terraform/modules/redis/main.tf`
- [Logging](https://github.com/shail2001/distributed-flash-sale-system/blob/main/terraform/modules/logging/main.tf): observability · CloudWatch-style centralized logging and operational visibility · `terraform/modules/logging/main.tf`

### Ops & Tests

Scripts and test harnesses used to deploy and validate the system

- [Deploy worker](https://github.com/shail2001/distributed-flash-sale-system/blob/main/scripts/deploy_order_worker.sh): deployment script · Builds, pushes, and rolls out the order-worker image · `scripts/deploy_order_worker.sh`
- [Smoke test](https://github.com/shail2001/distributed-flash-sale-system/blob/main/scripts/run_order_flow_smoke_test.sh): validation script · Exercises the end-to-end purchase-to-database path · `scripts/run_order_flow_smoke_test.sh`
- [API tests](https://github.com/shail2001/distributed-flash-sale-system/blob/main/tests/flash-sale-api/load_test.go): load tests · Benchmark, load, and quantity validation for flash-sale API behavior · `tests/flash-sale-api/load_test.go`
- [Order flow](https://github.com/shail2001/distributed-flash-sale-system/blob/main/tests/order-flow/smoke_test.go): integration test · AWS-backed smoke test for the full order-processing path · `tests/order-flow/smoke_test.go`
- [Worker tests](https://github.com/shail2001/distributed-flash-sale-system/blob/main/tests/order-worker/config_test.go): unit tests · Worker config and model tests · `tests/order-worker/config_test.go`

## Connections

- ALB → Waiting room: fronts · External traffic can be shaped before reaching the purchase path
- Waiting room → API main: forwards · Admission control forwards accepted requests into the API
- ALB → API main: routes · Direct path into the flash-sale API
- API main → Handlers: serves · API process exposes HTTP handlers
- Handlers → Inventory: reads/resets · Inventory endpoints manage stock visibility and reset operations
- Handlers → Purchase: submits · Purchase endpoint performs acceptance logic
- Purchase → Redis client: checks stock · Uses Redis to atomically coordinate inventory reduction
- Redis client → Redis: stores · Redis is the fast coordination layer for stock counters
- Purchase → SQS publisher: enqueues · Accepted purchases are handed off asynchronously
- SQS publisher → SQS queue: publishes · Order messages enter the buffer queue
- SQS queue → Worker main: delivers · Worker long-polls and consumes queue messages
- Worker main → Worker service: starts · Worker process delegates processing to the service layer
- Worker service → Retry: wraps · Transient failures are handled through retry logic
- Worker service → Dynamo store: persists · Completed orders are written through the DynamoDB store
- Dynamo store → DynamoDB: writes · DynamoDB is the durable system of record
- Dynamo store → Logging: emits · Worker operations are surfaced through centralized logs
- ECS → API main: runs · API container is deployed on ECS
- ECS → Worker main: runs · Worker container is deployed on ECS
- Network → ALB: supports · Load balancer sits on the provisioned network
- Network → ECS: supports · Service runtime depends on the shared network layer
- Network → Redis: supports · Managed cache is provisioned inside the network boundary
- Network → DynamoDB: integrates · Core state services are wired through the cloud network setup
- Deploy worker → ECS: updates · Deployment script pushes a new worker image and updates ECS
- Smoke test → Purchase: exercises · Smoke test drives the purchase flow end to end
- Smoke test → Dynamo store: verifies · Confirms downstream persistence path
- API tests → Purchase: measures · Load and quantity tests focus on API acceptance behavior
- Order flow → SQS queue: passes through · Integration test covers the queue-mediated workflow
- Worker tests → Worker service: covers · Worker tests validate service and model behavior
