Traffic Modeling
Traffic Metrics
Key Metrics
Metric | Definition
------------------------|------------------------------------------
Page Views (PV) | Total pages loaded
Unique Visitors (UV) | Distinct users
Sessions | Visits (includes returning)
Requests per Second | Real-time load
Concurrent Users | Users active at same time
Bandwidth | Data transferred (GB)
Traffic Patterns
Daily Pattern:
00:00-06:00: Low (10% of average)
06:00-09:00: Ramp up (50%)
09:00-12:00: Peak (100%)
12:00-14:00: Slight dip (80%)
14:00-18:00: High (90%)
18:00-22:00: Evening peak (95%)
22:00-00:00: Decline (40%)
Weekly Pattern:
Monday: 110% of average
Tuesday-Thursday: 100%
Friday: 90%
Saturday: 80%
Sunday: 70%
Calculating ConcurrentUser
// Formula for concurrent users
$concurrentUsers = (
$dailyUniqueVisitors
× $averageSessionDuration
× $peakHourPercentage
) / (24 × 60);
// Example:
$uv = 50000;
$sessionDuration = 5; // minutes
$peakPercentage = 0.25; // 25% of traffic in peak hour
$concurrent = (50000 × 5 × 0.25) / (24 × 60);
// = 87 concurrent users at peak
Traffic Growth Modeling
// Linear growth
function linearGrowth($currentTraffic, $growthRate, $months) {
return $currentTraffic * (1 + ($growthRate * $months));
}
// Compound growth
function compoundGrowth($currentTraffic, $monthlyGrowthRate, $months) {
return $currentTraffic * pow(1 + $monthlyGrowthRate, $months);
}
// Example: 5% monthly growth
$current = 100000; // daily PV
$future = compoundGrowth($current, 0.05, 12);
// 100000 × 1.05^12 = 179,586 daily PV in 12 months
Peak Traffic Estimation
Peak Traffic Calculation
Black Friday Estimation
// Historical Black Friday multiplier
$multipliers = [
'normal_day' => 1.0,
'black_friday' => 5.0, // 5x normal
'cyber_monday' => 4.5, // 4.5x normal
'christmas_week' => 3.0, // 3x normal
'flash_sale' => 8.0, // 8x normal (1 hour)
];
// Calculate peak
$normalTraffic = 100000; // daily PV
$blackFridayTraffic = $normalTraffic * $multipliers['black_friday'];
// = 500,000 PV
// Requests per second
$peakRPS = $blackFridayTraffic / (24 * 3600) * 10; // 10x average RPS
// = 500000 / 86400 × 10 = 57.8 RPS
Peak RPS Calculation
function calculatePeakRPS($dailyPV, $peakMultiplier = 10) {
$averageRPS = $dailyPV / 86400;
$peakRPS = $averageRPS * $peakMultiplier;
// Add buffer for spikes
$burstRPS = $peakRPS * 1.5;
return [
'average' => round($averageRPS, 2),
'peak' => round($peakRPS, 2),
'burst' => round($burstRPS, 2)
];
}
// Example: 500K daily PV
$traffic = calculatePeakRPS(500000);
// average: 5.79 RPS
// peak: 57.87 RPS
// burst: 86.81 RPS
Response Time Impact
Concurrent Users vs Response Time:
Users | Response Time | Status
---------|---------------|--------
10 | 200ms | OK
50 | 250ms | OK
100 | 350ms | OK
200 | 800ms | Warning
500 | 2000ms | Slow
1000 | 5000ms | Critical
Plan for 2x expected peak.
Capacity Buffer
Recommended buffers:
- Normal operations: 1.5x average
- Peak periods: 2x expected peak
- Critical events: 3x expected peak
Example:
Expected peak: 100 RPS
Required capacity: 200 RPS (2x buffer)
Critical capacity: 300 RPS (3x buffer)
Load Testing
k6 Script
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
stages: [
{ duration: '2m', target: 100 }, // Ramp up
{ duration: '5m', target: 100 }, // Stay at 100
{ duration: '2m', target: 200 }, // Peak
{ duration: '5m', target: 200 }, // Stay at peak
{ duration: '2m', target: 0 }, // Ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.1'],
},
};
export default function () {
http.get('https://example.com/');
sleep(1);
}
Seasonal Patterns
E-Commerce Seasonality
Annual Calendar
Month | Traffic Index | Key Events
------------|---------------|----------------------------------
January | 0.7 | Post-holiday lull
February | 0.8 | Valentine's Day
March | 0.9 | Spring collection
April | 1.0 | Average
May | 1.1 | Mother's Day
June | 1.0 | Average
July | 0.9 | Summer lull
August | 1.0 | Back to school
September | 1.1 | Fall collection
October | 1.2 | Pre-holiday ramp
November | 2.5 | Black Friday, Cyber Monday
December | 2.0 | Holiday shopping
Planning for Seasonality
// Infrastructure scaling plan
$seasonalPlan = [
'baseline' => [
'servers' => 4,
'db_replicas' => 1,
'cache_nodes' => 2
],
'peak' => [
'servers' => 8, // 2x baseline
'db_replicas' => 3, // 3x baseline
'cache_nodes' => 4 // 2x baseline
],
'black_friday' => [
'servers' => 12, // 3x baseline
'db_replicas' => 5, // 5x baseline
'cache_nodes' => 6 // 3x baseline
]
];
// Cost impact
$baselineCost = 2000; // monthly
$peakCost = $baselineCost * 2;
$blackFridayCost = $baselineCost * 3;
// Annual cost
$annualCost = (
$baselineCost * 8 + // 8 normal months
$peakCost * 3 + // 3 peak months
$blackFridayCost * 1 // 1 event month
);
// = 16000 + 6000 + 6000 = $28,000
Auto-Scaling Configuration
# AWS Auto Scaling Group
auto_scaling:
min: 4
max: 12
desired: 4
scaling_policies:
- name: scale_up
metric: CPUUtilization
threshold: 70
adjustment: +2
cooldown: 300
- name: scale_down
metric: CPUUtilization
threshold: 30
adjustment: -1
cooldown: 600
# Scheduled scaling for known events
scheduled_actions:
- name: black_friday_ramp
schedule: "0 6 25 11 *" # 6 AM Nov 25
min: 8
desired: 10
max: 12
- name: black_friday_ramp_down
schedule: "0 0 27 11 *" # Midnight Nov 27
min: 4
desired: 4
max: 12
Monitoring Traffic Patterns
// Track traffic patterns
$trafficMetrics = [
'daily_pv' => $this->getDailyPageViews(),
'hourly_distribution' => $this->getHourlyDistribution(),
'weekly_pattern' => $this->getWeeklyPattern(),
'monthly_trend' => $this->getMonthlyTrend(),
'year_over_year' => $this->getYearOverYearGrowth()
];
// Forecast next month
$forecast = $this->forecastTraffic($trafficMetrics);
// Expected: 120,000 daily PV
// Peak: 180,000 daily PV (1.5x)
// Required capacity: 360,000 daily PV (2x buffer)
Infrastructure Sizing
Server Sizing Based on Traffic
Web Server Sizing
// Rule of thumb: 1 server per 50 concurrent users
$concurentUsers = 200;
$serversNeeded = ceil($concurrentUsers / 50);
// = 4 servers
// Or per RPS: 1 server per 100 RPS
$peakRPS = 300;
$serversNeeded = ceil($peakRPS / 100);
// = 3 servers
// Use the higher estimate
$webServers = max(4, 3);
// = 4 servers
Database Sizing
Traffic Level | DB Servers | Read Replicas | Connection Pool
-------------------|------------|---------------|-----------------
< 50K daily PV | 1 | 0 | 50
50K-200K daily PV | 1 | 1 | 100
200K-500K daily PV | 1 | 2 | 200
500K-1M daily PV | 2 (cluster)| 3 | 300
> 1M daily PV | 2 (cluster)| 5+ | 500+
Cache Sizing
Redis Memory Calculation:
- Session data: 1KB × concurrent users
- Cache entries: avg 5KB × cache keys
- Full page cache: 50KB × cached pages
Example:
- 200 concurrent users: 200KB sessions
- 10,000 cache keys: 50MB cache
- 1,000 cached pages: 50MB FPC
- Total: ~100MB
- Recommended: 2GB (20x headroom)
CDN Sizing
Static Content Bandwidth:
- Average page: 2MB (images, CSS, JS)
- Daily PV × 2MB = Daily bandwidth
Example:
- 100K daily PV × 2MB = 200GB daily
- Monthly: 6TB
- With 50% cache hit: 3TB origin
CDN Plan:
- 3TB monthly origin transfer
- 100GB storage
- 10M requests
Infrastructure Cost Estimation
$infraCosts = [
'web' => [
'type' => 'c5.xlarge',
'count' => 4,
'hourly' => 0.17,
'monthly' => 0.17 * 730 * 4
],
'db' => [
'type' => 'r5.xlarge',
'count' => 2,
'hourly' => 0.25,
'monthly' => 0.25 * 730 * 2
],
'cache' => [
'type' => 'cache.r5.large',
'count' => 2,
'hourly' => 0.126,
'monthly' => 0.126 * 730 * 2
]
];
$totalMonthly = array_sum(array_column($infraCosts, 'monthly'));
// = 496.4 + 365 + 183.96 = $1,045.36
Practice Problems
Estimate peak traffic and required infrastructure for a Magento store expecting 200K daily page views on Black Friday.
Create an infrastructure scaling plan for an e-commerce store with seasonal traffic patterns.
Quiz
1. What is the typical Black Friday traffic multiplier?
2. How many concurrent users can one server typically handle?
3. What buffer should be planned for peak traffic?
4. Which month typically has highest e-commerce traffic?
Flashcards
Question
What is the concurrent user formula?
Click to reveal answer
Answer
UV × session duration × peak % / (24 × 60)
Question
What is Black Friday multiplier?
Click to reveal answer
Answer
5x normal traffic, plan for 2x peak (10x total buffer)
Question
How many servers per concurrent users?
Click to reveal answer
Answer
1 server per 50 concurrent users
Question
What is the capacity buffer?
Click to reveal answer
Answer
2x expected peak for normal, 3x for critical events
Question
When is peak e-commerce traffic?
Click to reveal answer
Answer
November (Black Friday, Cyber Monday)
Revision Notes
Key Takeaways
- 1. Concurrent users = UV × session duration × peak % / (24 × 60)
- 2. Black Friday: 5x normal traffic, plan for 2x peak buffer
- 3. 1 server per 50 concurrent users or 100 RPS
- 4. Auto-scaling with scheduled actions for known events
- 5. Seasonal planning: baseline, peak, and event-specific configurations
- 6. Monitor traffic patterns and adjust capacity proactively
Interview Tips
- • How do you estimate peak traffic for a Magento store?
- • Explain the concurrent user calculation
- • How do you plan for Black Friday traffic?
- • What is the recommended capacity buffer?
- • How do you configure auto-scaling for seasonal traffic?
Cheat Sheet
Traffic Estimation Cheat Sheet
Concurrent Users:
UV × session × peak% / (24×60)
Peak Multipliers:
- Normal: 1x
- Black Friday: 5x
- Flash sale: 8x
Server Sizing:
- 1 server per 50 concurrent
- 1 server per 100 RPS
- 2x buffer for peak
Seasonal:
- Baseline: 4 servers
- Peak: 8 servers
- Black Friday: 12 servers
Auto-Scaling:
- Scale up: CPU > 70%
- Scale down: CPU < 30%
- Scheduled for events