Hirestack
Hirestack
Chapter 13

Cost engineering

📖 5 min read · 11 sections · May 2026

Why this matters

Cost is a design dimension, not an afterthought. Junior engineers think about correctness; senior engineers add performance; Staff engineers add cost as a first-class concern. Production systems are evaluated on cost-per-user, cost-per-request, cost-per-transaction — not just whether they work.

Cost engineering is also where you most clearly demonstrate Staff judgment. Building something for $X/month when it could be built for $X/10 with a different architecture is a Staff-level call. The math matters.

The cloud cost mental model

Major cost categories on any cloud provider:

CategoryExamplesPricing model
ComputeEC2, GCE, Cloud Run, Lambda$/hour while running; sometimes $/invocation
StorageS3, EBS, GCS$/GB-month; often tiered by access frequency
Network egressOut-of-region or out-of-cloud traffic$/GB transferred; frequently the silent killer
Managed servicesRDS, DynamoDB, Aurora, CloudFront$/operation + provisioned capacity
Data warehouseBigQuery, Snowflake, Redshift$/query (data scanned) or $/credit-hour
SpecialtyGPU instances, dedicated hostsMuch higher than commodity

For a typical SaaS, the cost breakdown is usually:

Egress — the silent killer

Egress is bandwidth leaving the cloud (to internet, to another region, to another provider).

AWS egress to internet: ~$0.09/GB. At 1 TB/day of traffic: $90/day, $2,700/month. Seems small. But:

This is why Cloudflare exists (free egress relative to AWS), why CloudFront is much cheaper than direct S3 serving, why Discord built their own CDN, why Backblaze and competitors have flat-rate egress.

Senior insight: every cross-region call and every external integration costs you twice — once in latency, once in egress dollars. Co-locate aggressively. Cache aggressively. Compress aggressively. Each megabyte saved is roughly $0.1 of revenue freed.

Capacity vs cost vs latency triangle

You can pick two:

Most Staff engineers operate on the first axis: tight capacity planning to balance cost and latency.

Per-request / per-user cost

The number every Staff engineer should be able to compute for their service:

cost_per_request = (total_monthly_infra_cost) / (monthly_requests)
cost_per_user = (total_monthly_infra_cost) / (monthly_active_users)

Knowing this:

Worked example: LLM-backed feature

A chat feature uses Claude Sonnet 4 at $3/M input tokens, $15/M output tokens. Average chat turn: 2K input tokens (cached prefix + recent history), 500 output tokens. So per turn:

cost_per_turn = (2000 × $3 + 500 × $15) / 1M = $0.0135 per turn

With caching at ~50% discount on cached input: ~$0.008 per turn.

If your free tier averages 20 turns/user/month: $0.16/user/month for LLM. At 1M MAU, that's $160K/month. Your business model must support this.

If switching to GPT-4o-mini ($0.15/M input, $0.60/M output): per turn $0.0006. Per user: $0.012/month. At 1M MAU: $12K/month. 10× cost reduction by picking a different model.

This is the kind of math that decides product viability.

Reserved vs spot vs on-demand

Pricing modelDiscountTradeoff
On-demand0%Pay full rate; full flexibility
Reserved (1-year)~30-40%Commit to 1 year of usage; refund if you don't use
Reserved (3-year)~50-60%Long commitment; usually only for baseline infra
Savings plansSimilar to reservedMore flexible (instance type can vary); easier commit
Spot / preemptible60-90%Can be terminated with minutes notice; only for stateless / fault-tolerant workloads

A well-tuned cloud bill: ~70% reserved/savings plans (baseline) + ~20% spot (batch / stateless scale-out) + ~10% on-demand (spike capacity). Typical savings: 40-50% vs all-on-demand.

Auto-scaling economics

Auto-scaling sounds free but isn't. Each scale-out event has a delay (new instance boot time, warm-up). Scale-in is also fraught (don't kill instances with in-flight requests).

For predictable load (daily peaks): pre-scale before peak. Use scheduled scaling, not reactive.

For unpredictable load: react fast enough to absorb spikes, but not so fast that you flap (scale up, then scale down, then up).

For latency-sensitive: don't auto-scale on average load; scale on p99 latency or queue depth. Average can be fine while tails are awful.

When caching is more expensive than the DB it caches

A surprising production reality. Run the numbers before adding cache.

Example: a 16GB Redis cluster costs ~$300-500/month. The DB queries it caches cost maybe $0.0001 each. Cache pays off if you're hitting cache > 3-5M times/month and DB queries would otherwise be that many.

For low-traffic services with expensive caches: the DB hit it would have been cheaper. Reason about hit rate × DB cost × volume vs cache cost.

FinOps practices

FinOps = bringing financial accountability to cloud spending.

Applied scenarios

Scenario: Your service's cloud bill jumped 40% last month. Where do you start?
(1) Cost explorer by service: which line item grew? Usually one or two big ones. (2) For the biggest grower, find the resource: is it a specific instance type, specific region, specific managed service? (3) Correlate with usage: did traffic grow 40%? If yes, that's the cause and you need optimisation or capacity decision. If not — is there a runaway process? Misconfigured auto-scaler? Unintended cross-region traffic? Forgotten test cluster? (4) Fix the cause. (5) Set up an alert at this threshold to catch next anomaly faster.
Scenario: A team wants to add Redis cache for a low-traffic feature (1K requests/day). What's your response?
Check the math. 1K requests/day × 30 days = 30K requests/month. Even a tiny Redis costs $100/month. That's $0.003/cached request vs maybe $0.0001/DB request. Cache is 30× more expensive in this scenario. Either skip the cache (DB query is fine for low traffic), or use an in-process cache (free), or wait until traffic justifies it. Most cache decisions don't justify themselves at low volume.

Further reading

Cost engineering & FinOps