Files
sa-lessons/modules/module8/sections/08-cloud-economics-cost-calculator.md
T
Alexander Abakumov 15e744da49 Initial commit: Complete Software Architecture course with 11 modules
- Added comprehensive course structure with 11 modules
- Included theoretical content, practical examples, and tasks
- Multi-language code examples (Java, Rust, Node.js, Python)
- Detailed analysis of architectural approaches and trade-offs
- Complete caching strategies documentation
- Architecture examples from real-world systems (Twitter, Netflix, Uber)
- Structured navigation and learning path
- Ready for educational use
2025-09-09 22:08:07 +03:00

19 KiB

Экономика облака и калькулятор затрат

Навигация


Основы экономики облака

1. Модели ценообразования

Pay-as-you-go (PAYG)

  • Оплата только за используемые ресурсы
  • Гибкость в масштабировании
  • Низкие начальные затраты
// Калькулятор затрат для Pay-as-you-go
class PayAsYouGoCalculator {
    constructor() {
        this.resources = {
            compute: {
                instances: [],
                storage: [],
                networking: []
            },
            database: {
                instances: [],
                storage: []
            },
            services: {
                monitoring: [],
                security: [],
                backup: []
            }
        };
    }

    addComputeInstance(instance) {
        this.resources.compute.instances.push({
            type: instance.type,
            region: instance.region,
            hours: instance.hours,
            pricePerHour: instance.pricePerHour,
            monthlyCost: instance.hours * instance.pricePerHour * 30
        });
    }

    addStorage(storage) {
        this.resources.compute.storage.push({
            type: storage.type,
            size: storage.size, // GB
            pricePerGB: storage.pricePerGB,
            monthlyCost: storage.size * storage.pricePerGB
        });
    }

    calculateMonthlyCost() {
        let totalCost = 0;
        
        // Compute costs
        totalCost += this.resources.compute.instances.reduce((sum, instance) => sum + instance.monthlyCost, 0);
        totalCost += this.resources.compute.storage.reduce((sum, storage) => sum + storage.monthlyCost, 0);
        
        // Database costs
        totalCost += this.resources.database.instances.reduce((sum, instance) => sum + instance.monthlyCost, 0);
        totalCost += this.resources.database.storage.reduce((sum, storage) => sum + storage.monthlyCost, 0);
        
        // Services costs
        totalCost += this.resources.services.monitoring.reduce((sum, service) => sum + service.monthlyCost, 0);
        totalCost += this.resources.services.security.reduce((sum, service) => sum + service.monthlyCost, 0);
        totalCost += this.resources.services.backup.reduce((sum, service) => sum + service.monthlyCost, 0);
        
        return totalCost;
    }

    generateReport() {
        const monthlyCost = this.calculateMonthlyCost();
        const yearlyCost = monthlyCost * 12;
        
        return {
            monthlyCost: monthlyCost,
            yearlyCost: yearlyCost,
            breakdown: {
                compute: this.resources.compute.instances.reduce((sum, instance) => sum + instance.monthlyCost, 0) +
                        this.resources.compute.storage.reduce((sum, storage) => sum + storage.monthlyCost, 0),
                database: this.resources.database.instances.reduce((sum, instance) => sum + instance.monthlyCost, 0) +
                         this.resources.database.storage.reduce((sum, storage) => sum + storage.monthlyCost, 0),
                services: this.resources.services.monitoring.reduce((sum, service) => sum + service.monthlyCost, 0) +
                         this.resources.services.security.reduce((sum, service) => sum + service.monthlyCost, 0) +
                         this.resources.services.backup.reduce((sum, service) => sum + service.monthlyCost, 0)
            }
        };
    }
}

// Использование калькулятора
const calculator = new PayAsYouGoCalculator();

// Добавление compute instances
calculator.addComputeInstance({
    type: 't3.medium',
    region: 'us-east-1',
    hours: 24,
    pricePerHour: 0.0416
});

calculator.addComputeInstance({
    type: 't3.large',
    region: 'us-east-1',
    hours: 24,
    pricePerHour: 0.0832
});

// Добавление storage
calculator.addStorage({
    type: 'gp3',
    size: 1000, // 1TB
    pricePerGB: 0.08
});

// Генерация отчета
const report = calculator.generateReport();
console.log('Monthly Cost:', report.monthlyCost);
console.log('Yearly Cost:', report.yearlyCost);
console.log('Breakdown:', report.breakdown);

Reserved Instances

  • Предоплата за 1-3 года
  • Скидка до 75%
  • Предсказуемые затраты
// Калькулятор для Reserved Instances
class ReservedInstanceCalculator {
    constructor() {
        this.instances = [];
    }

    addReservedInstance(instance) {
        this.instances.push({
            type: instance.type,
            region: instance.region,
            term: instance.term, // 1 or 3 years
            paymentOption: instance.paymentOption, // 'No Upfront', 'Partial Upfront', 'All Upfront'
            upfrontCost: instance.upfrontCost,
            monthlyCost: instance.monthlyCost,
            totalCost: instance.upfrontCost + (instance.monthlyCost * instance.term * 12)
        });
    }

    calculateSavings(ondemandCost) {
        const reservedCost = this.instances.reduce((sum, instance) => sum + instance.totalCost, 0);
        const savings = ondemandCost - reservedCost;
        const savingsPercentage = (savings / ondemandCost) * 100;
        
        return {
            ondemandCost: ondemandCost,
            reservedCost: reservedCost,
            savings: savings,
            savingsPercentage: savingsPercentage
        };
    }
}

// Пример использования
const reservedCalculator = new ReservedInstanceCalculator();

reservedCalculator.addReservedInstance({
    type: 't3.medium',
    region: 'us-east-1',
    term: 1,
    paymentOption: 'No Upfront',
    upfrontCost: 0,
    monthlyCost: 30
});

const ondemandCost = 50; // Monthly on-demand cost
const savings = reservedCalculator.calculateSavings(ondemandCost * 12);
console.log('Savings:', savings);

Spot Instances

  • Использование неиспользуемых ресурсов
  • Скидка до 90%
  • Риск прерывания
// Калькулятор для Spot Instances
class SpotInstanceCalculator {
    constructor() {
        this.instances = [];
    }

    addSpotInstance(instance) {
        this.instances.push({
            type: instance.type,
            region: instance.region,
            spotPrice: instance.spotPrice,
            ondemandPrice: instance.ondemandPrice,
            interruptionRate: instance.interruptionRate, // Percentage
            monthlyCost: instance.spotPrice * 24 * 30,
            ondemandCost: instance.ondemandPrice * 24 * 30
        });
    }

    calculateSavings() {
        const totalSpotCost = this.instances.reduce((sum, instance) => sum + instance.monthlyCost, 0);
        const totalOndemandCost = this.instances.reduce((sum, instance) => sum + instance.ondemandCost, 0);
        const savings = totalOndemandCost - totalSpotCost;
        const savingsPercentage = (savings / totalOndemandCost) * 100;
        
        return {
            spotCost: totalSpotCost,
            ondemandCost: totalOndemandCost,
            savings: savings,
            savingsPercentage: savingsPercentage
        };
    }

    calculateRisk() {
        const totalInterruptionRate = this.instances.reduce((sum, instance) => sum + instance.interruptionRate, 0) / this.instances.length;
        
        return {
            averageInterruptionRate: totalInterruptionRate,
            riskLevel: totalInterruptionRate > 20 ? 'High' : totalInterruptionRate > 10 ? 'Medium' : 'Low',
            recommendation: totalInterruptionRate > 20 ? 'Use for non-critical workloads only' : 'Suitable for most workloads'
        };
    }
}

2. Компоненты затрат

Compute Costs

  • EC2 instances, Azure VMs, GCP Compute Engine
  • CPU, Memory, Storage
  • Operating System licensing

Storage Costs

  • Block storage (EBS, Azure Disks, GCP Persistent Disk)
  • Object storage (S3, Azure Blob, GCP Cloud Storage)
  • Database storage

Networking Costs

  • Data transfer
  • Load balancers
  • CDN

Services Costs

  • Monitoring and logging
  • Security services
  • Backup and disaster recovery

3. Оптимизация затрат

Right-sizing

  • Выбор правильного размера инстансов
  • Мониторинг использования ресурсов
  • Автоматическое масштабирование
// Right-sizing рекомендации
class RightSizingAnalyzer {
    constructor() {
        this.metrics = [];
    }

    addMetrics(instanceId, metrics) {
        this.metrics.push({
            instanceId: instanceId,
            cpuUtilization: metrics.cpuUtilization,
            memoryUtilization: metrics.memoryUtilization,
            networkUtilization: metrics.networkUtilization,
            storageUtilization: metrics.storageUtilization,
            timestamp: new Date()
        });
    }

    analyzeUtilization() {
        const recommendations = [];
        
        this.metrics.forEach(metric => {
            const recommendation = {
                instanceId: metric.instanceId,
                currentUtilization: {
                    cpu: metric.cpuUtilization,
                    memory: metric.memoryUtilization,
                    network: metric.networkUtilization,
                    storage: metric.storageUtilization
                },
                recommendations: []
            };

            // CPU recommendations
            if (metric.cpuUtilization < 20) {
                recommendation.recommendations.push({
                    type: 'CPU',
                    action: 'Downsize',
                    reason: 'CPU utilization is below 20%',
                    potentialSavings: '30-50%'
                });
            } else if (metric.cpuUtilization > 80) {
                recommendation.recommendations.push({
                    type: 'CPU',
                    action: 'Upsize',
                    reason: 'CPU utilization is above 80%',
                    potentialCost: '20-30% increase'
                });
            }

            // Memory recommendations
            if (metric.memoryUtilization < 30) {
                recommendation.recommendations.push({
                    type: 'Memory',
                    action: 'Downsize',
                    reason: 'Memory utilization is below 30%',
                    potentialSavings: '25-40%'
                });
            } else if (metric.memoryUtilization > 90) {
                recommendation.recommendations.push({
                    type: 'Memory',
                    action: 'Upsize',
                    reason: 'Memory utilization is above 90%',
                    potentialCost: '15-25% increase'
                });
            }

            recommendations.push(recommendation);
        });

        return recommendations;
    }
}

Reserved Instances Planning

  • Анализ использования ресурсов
  • Планирование покупки RI
  • Оптимизация портфеля RI
// Reserved Instances планировщик
class ReservedInstancePlanner {
    constructor() {
        this.usageHistory = [];
    }

    addUsageHistory(instanceType, region, hours, months) {
        this.usageHistory.push({
            instanceType: instanceType,
            region: region,
            hours: hours,
            months: months,
            utilizationRate: hours / (months * 30 * 24)
        });
    }

    calculateRIRecommendations() {
        const recommendations = [];
        
        // Группировка по типу инстанса и региону
        const groupedUsage = this.usageHistory.reduce((acc, usage) => {
            const key = `${usage.instanceType}-${usage.region}`;
            if (!acc[key]) {
                acc[key] = {
                    instanceType: usage.instanceType,
                    region: usage.region,
                    totalHours: 0,
                    totalMonths: 0,
                    utilizationRate: 0
                };
            }
            acc[key].totalHours += usage.hours;
            acc[key].totalMonths += usage.months;
            acc[key].utilizationRate = acc[key].totalHours / (acc[key].totalMonths * 30 * 24);
            return acc;
        }, {});

        Object.values(groupedUsage).forEach(usage => {
            if (usage.utilizationRate > 0.7) { // 70% utilization
                recommendations.push({
                    instanceType: usage.instanceType,
                    region: usage.region,
                    utilizationRate: usage.utilizationRate,
                    recommendation: 'Strong candidate for Reserved Instance',
                    potentialSavings: '30-60%',
                    term: usage.utilizationRate > 0.9 ? '3 years' : '1 year'
                });
            }
        });

        return recommendations;
    }
}

Spot Instances Strategy

  • Анализ толерантности к прерываниям
  • Выбор подходящих workload
  • Гибридная стратегия
// Spot Instances стратегия
class SpotInstanceStrategy {
    constructor() {
        this.workloads = [];
    }

    addWorkload(workload) {
        this.workloads.push({
            name: workload.name,
            type: workload.type,
            criticality: workload.criticality, // 'low', 'medium', 'high'
            interruptionTolerance: workload.interruptionTolerance, // 'low', 'medium', 'high'
            duration: workload.duration, // hours
            spotEligibility: this.calculateSpotEligibility(workload)
        });
    }

    calculateSpotEligibility(workload) {
        let score = 0;
        
        // Criticality scoring
        if (workload.criticality === 'low') score += 3;
        else if (workload.criticality === 'medium') score += 2;
        else score += 1;

        // Interruption tolerance scoring
        if (workload.interruptionTolerance === 'high') score += 3;
        else if (workload.interruptionTolerance === 'medium') score += 2;
        else score += 1;

        // Duration scoring (shorter is better for spot)
        if (workload.duration < 1) score += 3;
        else if (workload.duration < 4) score += 2;
        else score += 1;

        return {
            score: score,
            recommendation: score >= 7 ? 'Highly recommended' : score >= 5 ? 'Recommended' : 'Not recommended',
            potentialSavings: score >= 7 ? '60-90%' : score >= 5 ? '40-60%' : '20-40%'
        };
    }

    generateStrategy() {
        const strategy = {
            spotWorkloads: [],
            ondemandWorkloads: [],
            hybridWorkloads: []
        };

        this.workloads.forEach(workload => {
            if (workload.spotEligibility.score >= 7) {
                strategy.spotWorkloads.push(workload);
            } else if (workload.spotEligibility.score >= 5) {
                strategy.hybridWorkloads.push(workload);
            } else {
                strategy.ondemandWorkloads.push(workload);
            }
        });

        return strategy;
    }
}

Калькуляторы затрат

1. AWS Pricing Calculator

  • Детальный расчет затрат
  • Различные типы инстансов
  • Региональные различия

2. Azure Pricing Calculator

  • Комплексный калькулятор
  • Различные сервисы
  • Скидки и предложения

3. GCP Pricing Calculator

  • Интерактивный калькулятор
  • Детализация по сервисам
  • Экспорт в различные форматы

4. Third-party Tools

  • CloudHealth
  • Cloudyn
  • Cloudability

TCO (Total Cost of Ownership)

1. On-premises vs Cloud

  • Сравнение затрат
  • Учет всех компонентов
  • Долгосрочная перспектива
// TCO калькулятор
class TCOCalculator {
    constructor() {
        this.onPremisesCosts = {
            hardware: 0,
            software: 0,
            maintenance: 0,
            power: 0,
            cooling: 0,
            space: 0,
            personnel: 0,
            security: 0,
            backup: 0,
            disasterRecovery: 0
        };
        
        this.cloudCosts = {
            compute: 0,
            storage: 0,
            networking: 0,
            services: 0,
            support: 0,
            migration: 0,
            training: 0
        };
    }

    setOnPremisesCosts(costs) {
        this.onPremisesCosts = { ...this.onPremisesCosts, ...costs };
    }

    setCloudCosts(costs) {
        this.cloudCosts = { ...this.cloudCosts, ...costs };
    }

    calculateTCO(years = 3) {
        const onPremisesTotal = Object.values(this.onPremisesCosts).reduce((sum, cost) => sum + cost, 0) * years;
        const cloudTotal = Object.values(this.cloudCosts).reduce((sum, cost) => sum + cost, 0) * years;
        
        return {
            onPremises: {
                total: onPremisesTotal,
                yearly: onPremisesTotal / years,
                breakdown: this.onPremisesCosts
            },
            cloud: {
                total: cloudTotal,
                yearly: cloudTotal / years,
                breakdown: this.cloudCosts
            },
            comparison: {
                difference: cloudTotal - onPremisesTotal,
                percentage: ((cloudTotal - onPremisesTotal) / onPremisesTotal) * 100,
                recommendation: cloudTotal < onPremisesTotal ? 'Cloud is more cost-effective' : 'On-premises is more cost-effective'
            }
        };
    }
}

// Пример использования
const tcoCalculator = new TCOCalculator();

// On-premises costs (per year)
tcoCalculator.setOnPremisesCosts({
    hardware: 50000,
    software: 20000,
    maintenance: 15000,
    power: 5000,
    cooling: 3000,
    space: 10000,
    personnel: 80000,
    security: 10000,
    backup: 5000,
    disasterRecovery: 8000
});

// Cloud costs (per year)
tcoCalculator.setCloudCosts({
    compute: 30000,
    storage: 5000,
    networking: 2000,
    services: 8000,
    support: 5000,
    migration: 15000,
    training: 3000
});

const tco = tcoCalculator.calculateTCO(3);
console.log('TCO Comparison:', tco);

2. ROI Analysis

  • Анализ возврата инвестиций
  • Временные рамки
  • Риски и возможности

3. Cost Optimization

  • Постоянная оптимизация
  • Автоматизация процессов
  • Мониторинг затрат

Best Practices

1. Cost Monitoring

  • Регулярный мониторинг затрат
  • Алерты при превышении бюджета
  • Анализ трендов

2. Resource Optimization

  • Right-sizing ресурсов
  • Автоматическое масштабирование
  • Удаление неиспользуемых ресурсов

3. Budget Management

  • Установка бюджетных лимитов
  • Контроль расходов
  • Планирование затрат

4. Cost Allocation

  • Тегирование ресурсов
  • Центры затрат
  • Отчетность по подразделениям