Skip to content
advanced Phase 100 · Senior Practices

Capacity Planning for Magento

Capacity planning including traffic estimation, resource sizing, growth projections, and performance forecasting for Magento stores

45m
0 problems
Topic Progress 0%

Traffic Estimation

Traffic Metrics

## Key Metrics

- Requests per second (RPS)
- Concurrent users
- Page views per day
- Unique visitors per day
- Session duration
- Bounce rate
- Conversion rate

Estimation Formula

// Calculate RPS from daily visitors
$dailyVisitors = 100000;
$avgPageViews = 5;
$peakFactor = 3;  // Peak is 3x average

$avgRPS = ($dailyVisitors * $avgPageViews) / 86400;
$peakRPS = $avgRPS * $peakFactor;

// Example:
// 100,000 visitors * 5 pages / 86400 = ~6 RPS average
// Peak: 6 * 3 = 18 RPS

Traffic Patterns

// Traffic distribution
$trafficPatterns = [
    'hourly' => [
        '00:00' => 0.2,  // 20% of average
        '09:00' => 0.8,
        '12:00' => 1.5,  // 150% of average
        '18:00' => 1.2,
        '22:00' => 0.5,
    ],
    'daily' => [
        'monday' => 1.0,
        'tuesday' => 1.1,
        'wednesday' => 1.2,
        'thursday' => 1.1,
        'friday' => 1.0,
        'saturday' => 0.8,
        'sunday' => 0.7,
    ],
    'seasonal' => [
        'normal' => 1.0,
        'holiday' => 3.0,  // 3x during holidays
        'sale' => 5.0,    // 5x during sales
    ],
];

Load Testing Results

## Load Test Results

| Scenario | RPS | Avg Response | P95 Response | Error Rate |
|----------|-----|--------------|--------------|------------|
| Normal | 10 | 200ms | 500ms | 0.1% |
| Peak | 30 | 500ms | 1500ms | 0.5% |
| Sale | 50 | 1000ms | 3000ms | 2.0% |

## Capacity Needed
- Normal: 10 RPS
- Peak: 30 RPS
- Sale: 50 RPS
- Safety margin: 20%
- Required capacity: 60 RPS

Key Takeaway

Estimate traffic from daily visitors, page views, and peak factors. Account for hourly, daily, and seasonal patterns. Use load testing to validate estimates.

Resource Sizing

CPU Sizing

// CPU requirements calculation
$requestsPerSecond = 30;
$avgCpuPerRequest = 0.05;  // 50ms CPU time
$cpuUtilizationTarget = 0.7;  // 70% target utilization

$requiredCpu = ($requestsPerSecond * $avgCpuPerRequest) / $cpuUtilizationTarget;
// 30 * 0.05 / 0.7 = 2.14 CPU cores needed

// Recommendation: 4 cores (2x headroom)

Memory Sizing

// Memory requirements
$phpProcessMemory = 756;  // MB per PHP-FPM process
$phpProcesses = 50;        // Max children
$mysqlMemory = 2048;       // MB for MySQL
$redisMemory = 1024;       // MB for Redis
$osMemory = 1024;          // MB for OS

$totalMemory = ($phpProcessMemory * $phpProcesses) + $mysqlMemory + $redisMemory + $osMemory;
// (756 * 50) + 2048 + 1024 + 1024 = 41,896 MB = ~41 GB

// Recommendation: 48 GB (15% headroom)

Disk Sizing

// Disk requirements
$productData = 10;         // GB
$orderData = 50;           // GB (growing)
$mediaFiles = 100;         // GB
$logs = 20;                // GB
$database = 50;            // GB
$totalDisk = $productData + $orderData + $mediaFiles + $logs + $database;
// 10 + 50 + 100 + 20 + 50 = 230 GB

// Recommendation: 500 GB SSD (with growth)

Network Sizing

// Bandwidth requirements
$avgPageSize = 500;        // KB
$requestsPerSecond = 30;
$avgPageViews = 5;

$bandwidthRequired = ($requestsPerSecond * $avgPageSize * $avgPageViews) / 1000;  // Mbps
// 30 * 500 * 5 / 1000 = 75 Mbps

// Recommendation: 1 Gbps network

Key Takeaway

Size CPU, memory, disk, and network based on traffic estimates. Include headroom for peaks and growth. Consider PHP-FPM, MySQL, and Redis requirements.

Growth Projections

Growth Estimation

// Annual growth projection
class GrowthProjections
{
    public function project(int $currentTraffic, float $growthRate, int $years): array
    {
        $projections = [];
        
        for ($year = 0; $year <= $years; $year++) {
            $projections[] = [
                'year' => $year,
                'traffic' => $currentTraffic * pow(1 + $growthRate, $year),
                'estimated_rps' => $this->calculateRps($currentTraffic * pow(1 + $growthRate, $year)),
            ];
        }
        
        return $projections;
    }
}

// Example: 30% annual growth
$projections = $projector->project(100000, 0.30, 5);
// Year 0: 100,000 visitors
// Year 1: 130,000 visitors
// Year 2: 169,000 visitors
// Year 3: 219,700 visitors
// Year 4: 285,610 visitors
// Year 5: 371,293 visitors

Scaling Triggers

## Scaling Triggers

### CPU
- Average > 70% for 5 minutes
- Peak > 90% for 1 minute

### Memory
- Average > 80% for 5 minutes
- Swap usage > 0

### Disk
- Usage > 80%
- IOPS > 80% of provisioned

### Database
- Connections > 80% of max
- Slow queries > 10% of total
- Replication lag > 1 second

### Response Time
- P95 > 2 seconds for 5 minutes
- Error rate > 1%

Scaling Plan

## Scaling Milestones

| Metric | Current | Trigger | Action |
|--------|---------|---------|--------|
| Traffic | 100K/day | 150K/day | Add web node |
| Traffic | 200K/day | 250K/day | Add web node + Redis cluster |
| Traffic | 500K/day | 600K/day | Add DB replica + CDN optimization |
| Traffic | 1M/day | 1.2M/day | Multi-region + CDN edge |

Key Takeaway

Project growth with annual rates. Define scaling triggers for each metric. Create scaling milestones with specific actions.

Performance Forecasting

Performance Model

// Performance forecasting model
class PerformanceForecaster
{
    public function forecast(array $currentMetrics, float $growthRate): array
    {
        return [
            'response_time' => $this->forecastResponseTime($currentMetrics, $growthRate),
            'throughput' => $this->forecastThroughput($currentMetrics, $growthRate),
            'resource_usage' => $this->forecastResourceUsage($currentMetrics, $growthRate),
        ];
    }
    
    private function forecastResponseTime(array $metrics, float $growth): float
    {
        // Response time increases with load
        $currentRps = $metrics['rps'];
        $projectedRps = $currentRps * (1 + $growth);
        
        // Simple model: response time = base + (rps * factor)
        $baseTime = 100;  // ms
        $factor = 5;      // ms per RPS
        
        return $baseTime + ($projectedRps * $factor);
    }
}

Capacity Report

## Capacity Planning Report

### Current State
- Traffic: 100,000 visitors/day
- RPS: 10 (avg), 30 (peak)
- CPU: 4 cores, 50% utilization
- Memory: 16 GB, 60% utilization
- Disk: 200 GB, 40% utilization

### Growth Projection (12 months)
- Expected traffic: 150,000 visitors/day (50% growth)
- Required RPS: 15 (avg), 45 (peak)
- Required CPU: 6 cores
- Required Memory: 24 GB
- Required Disk: 300 GB

### Recommendations
1. Add 2 web nodes (current: 2, needed: 4)
2. Upgrade database to 8 GB RAM
3. Add Redis cluster for caching
4. Implement CDN for static content
5. Add monitoring for scaling triggers

Cost Forecasting

// Cost projection
class CostForecaster
{
    private array $pricing = [
        'web_node' => 200,     // $/month
        'db_server' => 500,     // $/month
        'redis' => 100,         // $/month
        'cdn' => 0.10,          // $/GB transferred
        'storage' => 0.10,      // $/GB/month
    ];
    
    public function forecast(array $requirements): float
    {
        $totalCost = 0;
        
        $totalCost += $requirements['web_nodes'] * $this->pricing['web_node'];
        $totalCost += $requirements['db_servers'] * $this->pricing['db_server'];
        $totalCost += $requirements['redis_nodes'] * $this->pricing['redis'];
        $totalCost += $requirements['cdn_gb'] * $this->pricing['cdn'];
        $totalCost += $requirements['storage_gb'] * $this->pricing['storage'];
        
        return $totalCost;
    }
}

Performance Budgets

## Performance Budgets

| Metric | Budget | Current | Status |
|--------|--------|---------|--------|
| Homepage Load | < 2s | 1.5s | OK |
| Product Page | < 3s | 2.2s | OK |
| Checkout | < 4s | 3.1s | OK |
| API Response | < 500ms | 350ms | OK |
| TTI | < 3.5s | 2.8s | OK |

Key Takeaway

Forecast performance with growth models. Create capacity reports with recommendations. Track performance budgets and costs.

Quiz

1. How to calculate RPS from daily visitors?

Question 1 options

2. What is the peak factor?

Question 2 options

3. When to scale web nodes?

Question 3 options

4. What is a performance budget?

Question 4 options

5. How to project growth?

Question 5 options

Flashcards

Question

How to calculate RPS?

Answer

(visitors * page_views) / 86400

Question

What is peak factor?

Answer

Peak traffic / average traffic (typically 2-5x)

Question

CPU scaling trigger?

Answer

Average > 70% for 5 minutes

Question

Memory sizing formula?

Answer

PHP processes * memory + MySQL + Redis + OS

Question

What is performance budget?

Answer

Maximum allowed response time for different page types

Question

Growth projection method?

Answer

Apply annual growth rate to current metrics

Revision Notes

Key Takeaways

  • 1. Calculate RPS from daily visitors and page views
  • 2. Account for peak factors and traffic patterns
  • 3. Size CPU, memory, disk, and network with headroom
  • 4. Define scaling triggers for each metric
  • 5. Project growth and plan scaling milestones

Interview Tips

  • Explain traffic estimation methodology
  • Discuss resource sizing calculations
  • Describe growth projection approaches
  • Explain capacity planning process

Cheat Sheet

Capacity Planning

Traffic Estimation:
RPS = (visitors * page_views) / 86400
Peak = avg * peak_factor (2-5x)

Resource Sizing:
CPU: requests * cpu_per_request / utilization
Memory: php_processes * memory + mysql + redis + os
Disk: data + growth + logs + media

Scaling Triggers:
CPU > 70% for 5 min
Memory > 80% for 5 min
Disk > 80%
Response > 2s for 5 min

Growth:
Apply annual growth rate
Plan scaling milestones
Track performance budgets