Course contentsShow
← C#
Lesson 1908 of 1,90817. Deployment and Cloud ArchitectureFree lesson

Optimizing egress costs

Minimize inter-service cloud data transfer costs through architectural placement decisions.

In this lesson, you will learn how to reduce your cloud bill by strategically placing services to minimize data transfer across network boundaries.

The Cost of Moving Bytes

In cloud environments, data ingress (inbound) is typically free, but egress (outbound) often carries a significant price tag. When your C# services communicate across Availability Zones (AZs) or different cloud regions, the cloud provider charges for every gigabyte transferred. At scale, these "invisible" costs can quickly dwarf your compute budget.

Architectural Placement Decisions

To optimize these costs, the golden rule is: keep chatty services as close as possible.

  1. Zone Affinity: Ensure your C# microservices, caches (like Redis), and databases reside in the same Availability Zone whenever possible. If Service A performs high-frequency requests to Database B, placing them in different zones incurs per-GB transfer fees that add up rapidly.
  2. Regional Containment: Avoid cross-region calls for routine operations. If you have a primary processing engine in us-east-1, your data store and supporting API workers should also live in us-east-1. Use global replication only for high-availability requirements, not for standard service-to-service communication.
  3. Local Aggregation: If you are processing massive telemetry data, aggregate or filter the data at the source before sending it over the network.

Implementation Example: Intelligent Routing

When using an HttpClient in C#, you can use a custom DelegatingHandler or a service discovery mechanism to prioritize internal endpoints located in the same network topology:

// Simplistic representation of selecting an endpoint within the same zone
public async Task<string> GetDataFromLocalPeerAsync(string resourceId)
{
    // The discovery service should prioritize addresses in the current AZ
    var localEndpoint = await _serviceDiscovery.GetEndpointAsync(
        "OrderService", 
        preference: ServicePreference.SameAvailabilityZone);

    var response = await _httpClient.GetAsync($"{localEndpoint}/orders/{resourceId}");
    return await response.Content.ReadAsStringAsync();
}

By ensuring your service discovery logic is "topology-aware," you effectively pin traffic to the local zone, avoiding the inter-zone toll booth entirely. While multi-region deployments are essential for disaster recovery, they should be treated as the exception, not the rule, for daily operational traffic.

Key Takeaway: Minimize egress costs by architecting for "Zone Affinity"—keeping highly interactive services and their dependencies within the same physical datacenter zone whenever high availability requirements allow.