π₯ Why This Blog Matters
β A company I worked with once spent $7,400 in a single monthβ¦
Just because an Azure Function was triggered around 8 million times β by stale messages in a forgotten queue.
The devs didnβt even know it was still active.
π‘ You donβt need cheaper pricing tiers.
You need smarter code.
This post is your developer-first playbook.
Iβll show you 10 real code patterns that:
Slash compute, storage, and telemetry waste
Are used inside Microsoft (Azure SDKs, Copilot infra, and durable workloads)
Can be implemented within a couple of weeks
Not a medium member? you can read this blog here.
π» Pattern 0: Cost Ghosts β Whatβs Billing You Silently
βYou canβt optimize what you donβt measure.β β Azure Advisor Team
π Common Mistake:
Ignoring silent costs from idle VMs, orphaned App Services, or chatty logs.
β
Fix:
Start with diagnostics:
Azure Cost Analysis β by tag or service
Application Insights β filter high-traffic routes
Azure Workbooks β cost-by-operation
Azure Advisor β underutilized resources
π‘ Quick Win: Tag every resource by team + feature. Unused ones will light up.
π§ Pattern 1: Queue-First, Not Compute-First
π Common Mistake:
Triggering Functions or APIs directly per event β even when it could wait.
β
Fix:
Use Storage Queues, Service Bus, or Event Hubs to buffer and batch.
API β Queue β Azure Function (batch trigger)
βQueues are cheaper than compute. Push work downstream.β β Azure Arch Center
π‘ Quick Win: Ingest telemetry β Event Hub β Storage Queue β Batch Function
π Potential Savings: 30β60% reduction in over-scaled compute
β³ Pattern 2: Time-to-Live Everything
π Common Mistake:
Cosmos DB, queues, and logs live forever. You keep paying for ghost data.
β
Fix:
Use TTL aggressively:
Cosmos DB: defaultTimeToLive
Service Bus: TimeToLive
Blobs: lifecycle policies
Durable Functions: auto-purge history
βUse TTL to enforce data lifecycle and reduce cost.β β Cosmos TTL Docs
π‘ Quick Win: Set Service Bus TTL = 48 hours. Cosmos = 7 days.
π Potential Savings: 10β25% on storage & telemetry
π Pattern 3: Cap Concurrency β Donβt Over-AutoScale
π Common Mistake:
Relying on autoscaling alone leads to bill spikes under load.
β
Fix:
Apply hard caps:
Azure Function β maxConcurrentRequests
Web APIs β SemaphoreSlim, ParallelOptions
Polly β BulkheadPolicy
βConcurrency limits prevent downstream exhaustion and surprise bills.β β Functions Scale Docs
π‘ Quick Win: Limit queue-triggered Functions to 5 concurrent instances.
π Potential Savings: 30β50% compute reduction in spikes
π§Ύ Pattern 4: Structured Logging + Sampling
π Common Mistake:
Logging every HTTP response at Information. Every. Single. Time.
β
Fix:
Use:
Structured logs:
_logger.LogInformation("Order {Id} processed at {Time}", id, DateTime.UtcNow);
Application Insights sampling
Log filters in host.json
βUse sampling to reduce ingestion, not insight.β β App Insights Docs
π‘ Quick Win: Use AdaptiveSamplingTelemetryProcessor. It auto-throttles verbosity.
π Potential Savings: 40β70% on App Insights ingestion cost
β±οΈ Pattern 5: Use Durable Timers Instead of Waiting
π Common Mistake:
Using Task.Delay or polling loops β which bills you per second.
β
Fix:
Use context.CreateTimer(...) in Durable Functions:
await context.CreateTimer(DateTime.UtcNow.AddMinutes(15), CancellationToken.None);
βDurable timers pause without active billing.β β Durable Functions Docs
π‘ Quick Win: Replace Thread.Sleep with CreateTimer in approval workflows.
π Potential Savings: 50β90% on wait-heavy Functions
π¦ Pattern 6: Blob Tiering Isnβt Optional
π Common Mistake:
Leaving all blobs in Hot Tier β even audit logs and backups.
β
Fix:
Use Lifecycle Management to move:
Hot β Cool β Archive β Delete
βArchive storage is up to 80% cheaper than Hot.β β Azure Storage Pricing
π‘ Quick Win: Set lifecycle policy:
30 days β Cool
90 days β Archive
180 days β Delete
π Potential Savings: 50β80% on storage cost
π§² Pattern 7: Trigger Filters Save You Real Money
π Common Mistake:
Function triggered by every Event Grid event β even irrelevant ones.
β
Fix:
Use:
Event Grid filters (eventType, subject)
Service Bus SQL filters
Cosmos DB Change Feed filters
βFilter events at the source to reduce compute.β β Event Grid Filters
π‘ Quick Win: Trigger only when eventType == "InvoiceCreated"
π Potential Savings: 20β40% reduction in Function invocations
π Pattern 8: Resilient Outbound Calls: Retry + Bulkhead
π Common Mistake:
Retrying failed APIs immediately β creates a retry storm.
Or worse β calling external APIs at 1000 RPS when they handle 50.
β
Fix:
Use:
new BlobClientOptions
{
Retry = {
MaxRetries = 5,
Mode = RetryMode.Exponential,
Delay = TimeSpan.FromSeconds(2)
}
}
And cap concurrency:
await semaphore.WaitAsync();
// API Call
semaphore.Release();
βSmart retries and bulkheads reduce downstream throttling and cost.β β Azure SDK + Cloud Patterns
π‘ Quick Win: Use Polly + SemaphoreSlim for any 3rd-party API call.
π Potential Savings: Up to 50% reduction in failed/duplicated requests
π Pattern 9: Track Cost per Route, Not Just Monthly Spend
π Common Mistake:
Reviewing cost by month or service β not by API route or feature.
β
Fix:
Use App Insights operation_Name
Correlate with Azure Cost Exports
Build Workbooks to analyze cost per endpoint
βTags + correlation = true FinOps insight.β β Azure FinOps Docs
π‘ Quick Win: Tag by project, owner, feature. Then sort cost by those.
π Potential Savings: Visibility = control = smarter decisions
π Bonus Pattern 10: Stop Paying for Premium SKUs You Donβt Need
π Common Mistake:
Using P1 Redis, Standard App Service, or AKS with 3-node pools for tiny workloads.
β
Fix:
Audit your SKU usage:
Redis: Use Basic unless youβre clustering
App Service: Move idle apps to Free or Consumption
AKS: Scale to 0 or move to Azure Container Apps
βPremium doesnβt mean better. Just pricier.β β Azure Advisor
π‘ Quick Win: Downgrade unused Redis cache from P1 to Basic.
π Potential Savings: $500β$1500/month depending on SKUs
β
Start Here: Apply These 3 Today
Add TTL to Cosmos + Service Bus
Cap Function concurrency to 5
Enable sampling in App Insights
π Watch your Azure bill dip within 48 hours.
π§ Final Thought
Smart code isnβt just faster β itβs cheaper. And it starts today.
π¬ Found a pattern youβre using already β or one that surprised you?
Drop it in the comments or share this with a teammate burning Azure cash.
Top comments (0)