How shail2001/distributed-flash-sale-system works
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: purchase.go Purchase · main.tf Network · service.go Worker service · dynamodb.go Dynamo store
Components 23
Services
Runtime components that handle traffic, queueing, and order persistence
- Waiting room · Planned front-door traffic shaping and token issuance layer
services/waiting-room/.gitkeep - API main · Bootstraps the flash-sale API process
services/flash-sale-api/main.go - Handlers · Inventory, purchase, and reset request handling
services/flash-sale-api/handlers/handler.go - Inventory · Inventory reads and reset-style state operations
services/flash-sale-api/handlers/inventory.go - Purchase · Synchronously decides whether a purchase can be accepted
services/flash-sale-api/handlers/purchase.go - Redis client · Redis-backed inventory counter and fast concurrency control
services/flash-sale-api/redis/client.go - SQS publisher · Publishes accepted orders to the queue for background processing
services/flash-sale-api/sqs/publisher.go - Worker main · Bootstraps the order-worker process
services/order-worker/main.go - Worker service · Consumes SQS messages and coordinates order completion
services/order-worker/internal/service/service.go - Retry · Retry handling for transient worker failures
services/order-worker/internal/retry/retry.go - Dynamo store · Idempotent order writes and transactional inventory audit updates
services/order-worker/internal/store/dynamodb.go
Infrastructure
Terraform-managed cloud resources and deployment plumbing
- ALB · AWS Application Load Balancer entry point for external requests
terraform/modules/alb/main.tf - SQS queue · Decouples bursty purchase traffic from downstream fulfillment
terraform/modules/sqs/main.tf - DynamoDB · Durable storage for completed orders and audit state
terraform/modules/dynamodb/main.tf - ECS · Runs the containerized services on Fargate
terraform/modules/ecs/main.tf - Network · Base VPC and connectivity layer for the deployment
terraform/modules/network/main.tf - Redis · Managed Redis used for atomic inventory reduction
terraform/modules/redis/main.tf - Logging · 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 · Builds, pushes, and rolls out the order-worker image
scripts/deploy_order_worker.sh - Smoke test · Exercises the end-to-end purchase-to-database path
scripts/run_order_flow_smoke_test.sh - API tests · Benchmark, load, and quantity validation for flash-sale API behavior
tests/flash-sale-api/load_test.go - Order flow · AWS-backed smoke test for the full order-processing path
tests/order-flow/smoke_test.go - Worker tests · Worker config and model tests
tests/order-worker/config_test.go
Connections 28
- 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