Multi-Store Intelligence Layer for Retail Pharmacy Chains
End-to-end architecture for 313 pharmacies โ real-time KPI dashboards, demand forecasting, AI chatbot, and cloud cost optimisation across AWS, Azure, and GCP.
313
Pharmacies
7 GB
Per Store MySQL
~2.2 TB
Total Data
266K
Max Daily Txns
54.5M
Max Yearly Records
10 yrs
Historical Depth
๐ Data Volume Profile
Single Store (Average Performer)
Table
Daily Min
Daily Max
Yearly Max
vw_all_sales
282
850
174,030
sale_items
282
850
174,030
sales
132
313
60,649
purchase
โ
16
2,029
Current MySQL size: 7 GB ยท 10 years of history
All 313 Pharmacies (Chain-Wide)
Table
Daily Min
Daily Max
Yearly Max
vw_all_sales
88,266
266,050
54,471,390
sale_items
88,266
266,050
54,471,390
sales
41,316
97,969
18,983,137
purchase
โ
5,008
635,077
Total data: ~2.2 TB ยท Annual growth: ~220 GB/yr
Ingestion Rate
~3 rows/sec
Peak across all stores
ETL Approach
CDC + Batch
Change data capture nightly
Storage Tier
Hot / Warm
2yr hot, 8yr warm/archive
Format at Rest
Parquet
~70% compression vs raw SQL
๐ System Architecture
Five-layer architecture: Store Edge โ Ingestion โ Data Lake โ Intelligence โ Presentation. Designed to be cloud-agnostic at the storage and compute layers.
Layer 1 โ Store Edge (313 Stores)
๐ช
MySQL / POS
Sales, Inventory, Purchase
โ
๐
CDC Agent
Debezium / custom connector
โ
๐
VPN / TLS
Encrypted tunnel to cloud
โ
Layer 2 โ Ingestion & Streaming
๐จ
Kinesis / Kafka
AWS MSK / Azure Event Hub / GCP Pub/Sub
โ
โก
Lambda / Functions
Validate, deduplicate, enrich
โ
๐
Raw Landing Zone
S3 / ADLS / GCS โ Parquet/ORC
โ
Layer 3 โ Data Lake & Warehouse
๐
Bronze Layer
Raw CDC events
โ
๐ช
Silver Layer
Cleaned, store-partitioned
โ
๐ฅ
Gold Layer
KPI aggregates, mart tables
โ
๐
Query Engine
Athena / Synapse / BigQuery
โ
Layer 4 โ Intelligence (AI/ML)
๐
Demand Forecast
Prophet / XGBoost per SKU
๐จ
Anomaly Detection
Isolation Forest โ stock outs
๐
Basket Analysis
Apriori / FP-Growth rules
๐ค
AI Chatbot
LLM + RAG on KPI store
๐ฆ
Reorder Engine
Auto PO suggestions
โ
Layer 5 โ Presentation
๐
KPI Dashboard
React + Recharts (your current)
๐ฑ
Mobile App
Store manager view
๐ฌ
Chatbot UI
Web widget + WhatsApp
๐ง
Alerts
Email / SMS / WhatsApp
๐
Reports
Scheduled PDF exports
โ๏ธ Data Pipeline Design
๐ Ingestion Strategy
1
Nightly Batch Pull (Primary) โ Cron at 2 AM pulls delta from each store's MySQL using last-modified timestamps. Safest for existing POS systems.
2
CDC via Debezium โ For stores with binlog enabled, real-time change capture gives near-realtime dashboard updates (15โ30 min lag).
3
Manual Upload Fallback โ Stores with connectivity issues upload CSV dumps via secure portal. ETL handles dedup.
๐งน Transformation Rules
1
Deduplication โ Hash on (store_id, bill_no, item_id, txn_date) to prevent double-counting during retries.
2
Drug Master Mapping โ Normalise SKU names across stores (generic vs branded, spelling variants).
3
Partitioning โ Partition Parquet files by (year / month / store_id) for optimal Athena / BigQuery scan costs.
A RAG-powered chatbot that lets pharmacists, store managers, and HQ staff query business data in plain English โ via web widget or WhatsApp.
Query Flow
1
User Input โ "Show me top 10 slow-moving drugs in store #45 this month" via chat widget or WhatsApp.
2
Intent Classification โ LLM classifies: KPI query / forecast query / reorder alert / general drug info.
3
SQL Generation (Text-to-SQL) โ GPT-4o / Gemini converts natural language to SQL against your Gold layer schema.
4
Query Execution โ Generated SQL runs on Athena / BigQuery against pre-aggregated KPI tables. Results in <1 sec for Gold layer.
5
Response Synthesis โ LLM formats result as natural language + optional chart data payload.
6
Delivery โ Response sent back via web widget with inline mini-chart, or WhatsApp text summary.
Chatbot Capabilities
๐ Analytics Queries
"What are my top 5 selling brands across all stores today?" ยท "Which store has the highest average basket size?"
โ ๏ธ Alerts & Anomalies
"Any stores with unusually low sales today?" ยท "Which drugs are about to go out of stock in Ernakulam stores?"
๐ฎ Forecasts
"What will Paracetamol demand look like next 30 days?" ยท "Suggest reorder quantities for store #12."
๐ Reports
"Give me a daily summary for all stores" ยท "Export this month's purchase report as PDF."
๐ง Tech Stack Options
LLM Core
GPT-4o / Gemini 1.5
Text-to-SQL
Langchain + few-shot
Vector Store
Pinecone / pgvector
Framework
FastAPI backend
WhatsApp
Meta Cloud API
๐ก Safety & Guardrails
โ Role-based: Store managers see only their store data
โ SQL injection prevention via parameterised queries
โ Max row limit: 10K per query to prevent abuse
โ Query cost cap via Athena workgroup limits
โ Audit log of all chatbot queries per user
๐ฐ Chatbot Cost/Month
GPT-4o (1K queries/day)
~$90
Gemini 1.5 Flash (alt)
~$12
Vector DB (Pinecone)
~$70
WhatsApp API (500 msg)
~$3
Hosting (FastAPI)
included
๐ฐ Multi-Cloud Cost Estimator
Adjust the sliders to model cost for different scale scenarios. All prices are based on ap-south-1 (AWS Mumbai), Central India (Azure), and asia-south1 (GCP Mumbai) regions.
313
566
500
100
Detailed Breakdown
Component
AWS ($/mo)
Azure ($/mo)
GCP ($/mo)
Notes
TOTAL / MONTH
โ
โ
โ
TOTAL / YEAR
โ
โ
โ
โ ๏ธ Estimation Notes: Costs are approximations based on public pricing (June 2025, South Asia regions). Actual costs vary with reserved instance discounts (up to 60%), committed use discounts, negotiated enterprise agreements, and data egress patterns. AWS and Azure prices based on ap-south-1 and Central India respectively. GCP based on asia-south1. Always run cloud provider calculators for production budgeting.
โ๏ธ Cloud Provider Comparison
Capability
AWS
Azure
GCP
Data Lake
S3 + Glue + Athena
ADLS Gen2 + Synapse
GCS + Dataflow + BigQuery
Serverless Compute
Lambda + Step Functions
Azure Functions + Logic Apps
Cloud Functions + Workflows
ML Platform
SageMaker
Azure ML
Vertex AI
Streaming
Kinesis / MSK
Event Hubs
Pub/Sub + Dataflow
Managed DB
Aurora Serverless
Azure SQL Serverless
AlloyDB / Cloud SQL
CDN / API GW
CloudFront + API GW
Azure CDN + APIM
Cloud CDN + Apigee
India Presence
Mumbai (ap-south-1)
Central India
Mumbai (asia-south1)
MySQL Migration
DMS (native MySQL)
Database Migration Service
Database Migration Service
AI/LLM Access
Bedrock (Claude, Titan)
Azure OpenAI (GPT-4o)
Vertex AI (Gemini)
Best For This Project
โ Strong all-round, mature ecosystem
โ ๏ธ Best if already in Microsoft stack
โ Best BI / BigQuery pricing
๐ Recommendation
Recommended for Scale
AWS โ Primary Choice
Lambda + S3 + Athena + Aurora Serverless. Mumbai region. Best mature ecosystem for this use case. Your existing AWS cost calculator shows $300/mo for prediction layer alone โ add ~$200 for ingestion, storage, and dashboard = ~$500/mo total.
โ Recommended: Lambda architecture (Design A) at your current scale
Best for Analytics
GCP โ Analytics-Heavy Builds
BigQuery is significantly cheaper for ad-hoc analytics vs Athena at scale. Vertex AI + Gemini gives native LLM integration. Consider GCP if your primary workload is querying and BI, not heavy ML training.
BigQuery: $5/TB scanned vs Athena $5/TB โ similar, but BQ has flat-rate options
If Microsoft-First
Azure โ Microsoft Ecosystem
Best if client already uses Microsoft 365, Dynamics, or Power BI. Azure Synapse + Power BI Embedded gives excellent out-of-box dashboards. Higher baseline cost but good enterprise support in India.
โ ๏ธ Typically 15โ25% more expensive than AWS/GCP for this workload
๐บ Implementation Roadmap
Phase 1
Foundation
Cloud account + VPC setup
Data migration: 10yr MySQL โ S3 Parquet
Bronze/Silver/Gold lake structure
ETL pipeline (nightly batch)
Basic dashboard API
โฑ 6โ8 weeks
Phase 2
KPI Layer
KPI aggregation tables
Dashboard (your existing UI)
Multi-store filtering
User auth + RBAC
Alerting (email/SMS)
โฑ 4โ6 weeks
Phase 3
Intelligence
Demand forecast (Prophet)
Stockout prediction
Reorder suggestion engine
Basket analysis
Anomaly detection
โฑ 6โ8 weeks
Phase 4
AI Chatbot
LLM + Text-to-SQL pipeline
Web widget integration
WhatsApp Business API
Store-level RBAC in bot
Feedback + fine-tuning loop
โฑ 4โ6 weeks
๐ก Total Timeline: 20โ28 weeks (5โ7 months) for full platform.
Phase 1+2 alone delivers immediate ROI โ stakeholders can see chain-wide KPIs within 10โ14 weeks.
Budget Phase 1โ2: ~โน15โ20 lakhs development + $500/mo cloud. Full platform: ~โน35โ50 lakhs + $800โ1200/mo cloud.