<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged careerbytecode</title>
<link>https://coderlegion.com/tag/careerbytecode</link>
<description>Powered by Question2Answer</description>
<item>
<title>Zero Downtime: Designing an Azure Multi-Region Architecture</title>
<link>https://coderlegion.com/8008/zero-downtime-designing-an-azure-multi-region-architecture</link>
<description>&lt;h2 id=&quot;zero-downtime-designing-an-azure-multi-region-architecture&quot;&gt;Zero Downtime: Designing an Azure Multi-Region Architecture&lt;/h2&gt;
&lt;hr&gt;
&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;High availability isn’t just a buzzword to throw into a system design interview; it’s the difference between a minor hiccup and a career-defining disaster. We often build for the &quot;happy path,&quot; assuming Azure regions are invincible fortresses. But cables get cut, weather happens, and yes, sometimes entire regions go dark.&lt;/p&gt;
&lt;p&gt;If your application lives in a single region, your uptime is at the mercy of that specific geography. Today, we’re going to walk through moving from a fragile, single-region setup to a robust Active-Active Multi-Region Architecture that can survive a total regional outage without your users ever noticing.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;the-story-the-3-am-wake-up-call&quot;&gt;The Story: The 3 AM Wake-Up Call&lt;/h3&gt;
&lt;p&gt;It was 3:14 AM on a Tuesday—it’s always a Tuesday. My phone buzzed off the nightstand, vibrating with the distinctive, heart-stopping pattern of PagerDuty.&lt;/p&gt;
&lt;p&gt;&quot;East US is down,&quot; the message read.&lt;/p&gt;
&lt;p&gt;I groggily opened my laptop, hoping it was just a blip. It wasn't. An underlying storage outage in the primary region had cascaded, taking our entire e-commerce platform offline. We had a &quot;Disaster Recovery&quot; plan, but it was a cold backup in West Europe. Restoring it meant updating DNS records manually, warming up cold caches, and praying the database backups were consistent.&lt;/p&gt;
&lt;p&gt;It took us four hours to get back online. In internet time, that’s an eternity. We lost revenue, but worse, we lost trust.&lt;/p&gt;
&lt;p&gt;That morning, fueled by too much coffee and regret, we made a pact: &lt;strong&gt;Never again.&lt;/strong&gt; We were going to build a system that could lose an entire region and keep humming along. Here is exactly how we did it.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;core-concepts-active-passive-vs-active-active&quot;&gt;Core Concepts: Active-Passive vs. Active-Active&lt;/h3&gt;
&lt;p&gt;Before we write a single line of code, we need to agree on the strategy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Active-Passive:&lt;/strong&gt; You have a primary region handling traffic and a secondary region sitting idle (or receiving replication data). Failover usually requires manual intervention or a delay.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Active-Active:&lt;/strong&gt; Both regions are live. They both handle traffic simultaneously. If one dies, the global load balancer simply stops sending users there. This is the holy grail for zero downtime.&lt;/p&gt;
&lt;p&gt;To achieve this, we need three layers of abstraction:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;Brain&lt;/strong&gt; (Traffic Routing): Something global to direct users.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Muscle&lt;/strong&gt; (Compute): Stateless application servers.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Heart&lt;/strong&gt; (Data): A database that accepts writes in multiple places.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;step-by-step-guide&quot;&gt;Step-by-Step Guide&lt;/h3&gt;
&lt;p&gt;We are going to build this using Azure Front Door, Azure App Service, and Cosmos DB.&lt;/p&gt;
&lt;h4 id=&quot;1-the-entry-point-azure-front-door&quot;&gt;1. The Entry Point: Azure Front Door&lt;/h4&gt;
&lt;p&gt;Old school setups used Azure Traffic Manager (DNS-based). Modern web apps should use Azure Front Door. It operates at Layer 7 (HTTP/S), uses the Microsoft global edge network to speed up content, and fails over almost instantly because it uses active health probes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why Front Door?&lt;/strong&gt; It terminates SSL at the edge and routes traffic to the closest available backend.&lt;br&gt;
&lt;strong&gt;The trick:&lt;/strong&gt; We set up an &quot;Origin Group&quot; containing our East US and West Europe endpoints.&lt;/p&gt;
&lt;h4 id=&quot;2-the-compute-layer-go-stateless&quot;&gt;2. The Compute Layer: Go Stateless&lt;/h4&gt;
&lt;p&gt;This is where most developers get stuck. You cannot store session state (like a shopping cart or user login) in the memory of your web server. If a user hits East US for Request A and West Europe for Request B, the session will be lost.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Externalize state. Use Azure Redis Cache (Enterprise tier allows active-geo replication) or just keep state in the database.&lt;br&gt;
&lt;strong&gt;The Code:&lt;/strong&gt; Ensure your application connects to the &lt;em&gt;local&lt;/em&gt; region's database endpoint to minimize latency.&lt;/p&gt;
&lt;h4 id=&quot;3-the-data-layer-multi-region-writes&quot;&gt;3. The Data Layer: Multi-Region Writes&lt;/h4&gt;
&lt;p&gt;This is the hardest part. In a traditional SQL setup, you usually have one Writer and multiple Readers. If the Writer region goes down, you have to promote a Reader, which takes time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Enter Azure Cosmos DB.&lt;/strong&gt; It supports Multi-Region Writes. You can write to East US and West Europe simultaneously, and Cosmos DB handles the replication magic in the background.&lt;br&gt;
&lt;strong&gt;Consistency:&lt;/strong&gt; For zero downtime, we usually accept &quot;Session&quot; consistency. It guarantees that a user sees their own writes immediately, which is enough for 99% of apps.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;infrastructure-as-code-terraform-&quot;&gt;Infrastructure as Code (Terraform)&lt;/h3&gt;
&lt;p&gt;Let's look at how to define the critical Front Door configuration in Terraform. This snippet creates a backend pool that checks both regions.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;```terraform
resource &quot;azurerm_cdn_frontdoor_profile&quot; &quot;main&quot; {
  name                = &quot;example-frontdoor&quot;
  resource_group_name = azurerm_resource_group.main.name
  sku_name            = &quot;Premium_AzureFrontDoor&quot; # Required for some advanced features
}

resource &quot;azurerm_cdn_frontdoor_origin_group&quot; &quot;main&quot; {
  name                       = &quot;example-origin-group&quot;
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.main.id

  load_balancing {
    sample_size                     = 4
    successful_samples_required     = 3
    additional_latency_in_milliseconds = 50
  }

  health_probe {
    path                = &quot;/health&quot;
    protocol            = &quot;Https&quot;
    interval_in_seconds = 100 # Aggressive probing for faster failover
    request_type        = &quot;HEAD&quot;
  }
}

# Add East US Origin
resource &quot;azurerm_cdn_frontdoor_origin&quot; &quot;east_us&quot; {
  name                          = &quot;app-eastus&quot;
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.main.id
  enabled                       = true
  host_name                     = azurerm_linux_web_app.east_app.default_hostname
  http_port                     = 80
  https_port                    = 443
  priority                      = 1
  weight                        = 1000
}

# Add West Europe Origin
resource &quot;azurerm_cdn_frontdoor_origin&quot; &quot;west_eu&quot; {
  name                          = &quot;app-westeu&quot;
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.main.id
  enabled                       = true
  host_name                     = azurerm_linux_web_app.west_app.default_hostname
  http_port                     = 80
  https_port                    = 443
  priority                      = 1
  weight                        = 1000
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;best-practices-amp-gotchas-&quot;&gt;Best Practices &amp;amp; &quot;Gotchas&quot;&lt;/h3&gt;
&lt;h4 id=&quot;1-the-cost-factor&quot;&gt;1. The Cost Factor&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; Multi-region is expensive. You are effectively doubling your compute and paying for cross-region data transfer.&lt;br&gt;
&lt;strong&gt;Tip:&lt;/strong&gt; Use auto-scaling rules. In &quot;peace time,&quot; run your secondary region with minimal instances (maybe 2). Configure scale-out rules to handle the load only if the primary region fails or traffic spikes.&lt;/p&gt;
&lt;h4 id=&quot;2-conflict-resolution&quot;&gt;2. Conflict Resolution&lt;/h4&gt;
&lt;p&gt;If User A updates a record in the US, and User B updates the same record in Europe at the exact same millisecond, who wins?&lt;br&gt;
&lt;strong&gt;Last Write Wins (LWW):&lt;/strong&gt; The default in Cosmos DB. It uses the system clock. For most apps, this is fine.&lt;br&gt;
&lt;strong&gt;Custom Merge:&lt;/strong&gt; If you need complex logic, you'll need to write a stored procedure to merge the data.&lt;/p&gt;
&lt;h4 id=&quot;3-chaos-engineering&quot;&gt;3. Chaos Engineering&lt;/h4&gt;
&lt;p&gt;You cannot trust this architecture until you break it.&lt;br&gt;
&lt;strong&gt;Action:&lt;/strong&gt; Schedule a &quot;Game Day.&quot; Manually turn off the Web App in the primary region during a low-traffic window. Watch your Front Door metrics. Does traffic shift automatically? Did anyone notice?&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;community-corner&quot;&gt;Community Corner&lt;/h3&gt;
&lt;p&gt;I’d love to hear how you handle data consistency in distributed systems!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Do you use SQL Failover Groups instead of Cosmos?&lt;/li&gt;
&lt;li&gt;Have you ever had a failover that didn't work? What happened?&lt;/li&gt;
&lt;li&gt;Drop a comment below. The best war story gets a shoutout in next week's newsletter!&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;faq&quot;&gt;FAQ&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Q: Can I use Azure SQL Database for active-active?&lt;/strong&gt;&lt;br&gt;
A: Generally, no. Azure SQL is typically Active-Passive (one writer). You can use &quot;Failover Groups&quot; to automate the failover, but there is usually a short period (seconds to minutes) of write unavailability.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Q: What is the difference between Availability Zones and Regions?&lt;/strong&gt;&lt;br&gt;
A: Zones are separate datacenters within the &lt;em&gt;same&lt;/em&gt; region (e.g., separate buildings in East US). They protect against fire/power failure in one building. Regions are hundreds of miles apart. They protect against natural disasters affecting a whole city.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Q: How do I handle scheduled deployments?&lt;/strong&gt;&lt;br&gt;
A: Deploy to one region at a time. Deploy to the &quot;passive&quot; or secondary region first, run smoke tests, and then deploy to the primary. This ensures you don't break the whole world simultaneously.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Q: Does Front Door add latency?&lt;/strong&gt;&lt;br&gt;
A: Actually, it often &lt;em&gt;reduces&lt;/em&gt; it. Front Door uses &quot;Anycast&quot; to onboard user traffic at the nearest Microsoft Edge node (PoP) and rides the dedicated Microsoft backbone network to your app, avoiding the slower public internet.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Q: What is RTO and RPO?&lt;/strong&gt;&lt;br&gt;
A: &lt;strong&gt;RTO (Recovery Time Objective):&lt;/strong&gt; How long you are down (Goal: Near zero). &lt;strong&gt;RPO (Recovery Point Objective):&lt;/strong&gt; How much data you lose (Goal: Zero).&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Building a Zero-Downtime Multi-Region architecture is an investment. It requires more complexity in your deployment pipelines and data handling. But the first time you sleep through a major regional outage because Front Door quietly shifted traffic to Europe, you’ll know it was worth every penny.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Next Step:&lt;/strong&gt; Go to your Azure Portal right now and check your current application. Is it in a single region? If yes, map out what components (SQL, Storage, Compute) would need to change to support a second region. Start small—maybe just replicate your storage first!&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/8008/zero-downtime-designing-an-azure-multi-region-architecture</guid>
<pubDate>Mon, 01 Dec 2025 05:00:00 +0000</pubDate>
</item>
</channel>
</rss>