Cost-aware backend engineering
We learn where a backend’s cloud bill comes from and how to read it. A service that works but costs ten times what it should is a design problem, not an accounting one. We will cover the main cost drivers with Wren’s illustrative numbers, and how to find and cut the expensive parts of a bill.
#Section 25What a backend costs
An interviewer asks: “It works. Why is the cloud bill twenty lakh rupees a month?” The question tests whether a backend engineer knows what their design consumes. Every unit in this series made choices that show up on a bill, and the drivers are a fairly short list. The prices below are illustrative on-demand list prices from a large cloud provider, used for arithmetic, and real bills vary by provider, region and discount.
Compute is usually the largest line. Wren’s cluster runs 12 nodes of 8 vCPU and 32 GiB at about 3,364. What matters is not the nodes but how full they are. Pods are packed by their requests, Part III, so a service that requests 500m of CPU and uses 150m reserves capacity that nobody else can use.
Databases come next, and they are expensive per unit because they need memory, fast disks and replicas. Wren's PostgreSQL primary and one replica, managed, cost about 1,168 a month, before storage and backups. Storage for objects and disks is cheap per gigabyte, but grows forever unless lifecycle rules expire old data, Unit XII.
Network charges are where architecture surprises people. Data leaving the cloud to the internet, egress, costs about 233. Traffic between availability zones costs about 0.02 per GB for the round trip, about $346. Cross-region traffic and NAT gateways, which charge per gigabyte processed, can be far larger. A chatty microservice design, Part VIII, pays in exactly this line.
Observability is often the surprise. Unit XIV's 86.4 GB of logs a day is 2,592 GB a month, and at an illustrative 1,296, a fifth of the whole bill, before indexing and retention. Hosted metrics charge per active series, so a cardinality explosion from a user id label, Unit XIV, can multiply the metrics bill overnight. Traces cost by volume and sampling rate. Caches, queue retention and search clusters are sized by memory and disk: a Redis cluster sized for every key ever written, a Kafka topic retaining 30 days when consumers need 2, and a search cluster with replicas for an index nobody queries.
Wren's illustrative total is about 4.94 per million requests. That number, cost per unit of business, matters more than the total.
#Section 26Finding and cutting the expensive parts
A bill is investigated like an incident, with data rather than guesses. The first tool is cost allocation. Every cloud resource carries tags for service, team and environment, and Kubernetes costs are split by namespace and workload using requests and usage, with tools such as OpenCost or Kubecost. Without tags, the bill says "EC2: 9,000", and someone can ask why staging needs a full-size search cluster.
The second tool is unit economics: cost per request, per order, per active user or per tenant. Wren tracks cost per thousand orders monthly. Total cost rising with traffic is healthy. Cost per order rising means something got less efficient, a new feature, a design change or waste, and the per-service breakdown says where. It also lets Wren price enterprise plans with known margins.
The usual savings come from a familiar list. Rightsizing sets requests from measured usage, Part III. If orders requests 500m and its p95 is 150m, lowering the request to 200m raises the pods that fit on a node by CPU from 15 to 38, and memory, at 29 pods, becomes the limit, so nearly twice as many pods share each node. Autoscaling removes pods and nodes in quiet hours, since paying for peak capacity all day wastes most of it at a four-to-one peak ratio. Commitments and spot capacity cut unit prices: reserved instances or savings plans for the steady baseline, often 30% to 60% cheaper, and spot or preemptible nodes for interruptible work such as batch jobs and CI, which tolerate being stopped.
The architecture-level savings are larger and take longer. Keep traffic local: topology-aware routing that prefers pods in the same zone, Unit XI, cuts cross-zone charges, and a CDN, Unit VII, cuts egress and origin load at once. Compress responses, since egress is billed per byte. Log less: sample routine logs, drop debug logs in production, cap retention and archive to object storage, Unit XIV. Bound cardinality in metrics. Expire data: lifecycle rules on objects, retention on topics and indexes sized for what is queried. Delete idle resources: unattached disks, old snapshots, forgotten load balancers and test environments left running, which a weekly report of untagged and idle resources finds.
Cost is one of the trade-offs a backend engineer makes alongside latency and reliability. Three replicas across three zones cost more than one, and Wren pays for them because its SLO needs them. A cache costs memory and saves database capacity. The skill is knowing what each choice consumes, measuring it, and spending deliberately rather than by accident.