Volume 8 · Chapter 5 — Azure Cloud Architecture & Enterprise DevOps for .NET Engineers
Azure Database Services
5.1 Introduction
Azure’s managed database services take backups, patching, replication, and failover off the team’s plate, letting engineers focus on data modeling and query design. The central architectural decision is relational versus NoSQL, and within those, which managed offering fits the workload’s consistency, scale, and compatibility needs. Choosing well means understanding not just features but the consistency and scaling model each service imposes.
5.2 Azure SQL Database
Azure SQL Database is a fully managed relational engine based on SQL Server, ideal for transactional (OLTP) workloads that need ACID guarantees, rich querying, and strong consistency. It offers automated backups with point-in-time restore, built- in high availability, active geo-replication, and intelligent performance tuning. The purchasing models (DTU or vCore) and service tiers let you match performance to cost, and serverless auto-pauses idle databases.
5.3 Azure SQL Managed Instance
Managed Instance provides near-full SQL Server compatibility - cross-database queries, SQL Agent, CLR, linked servers - for lift-and-shift migrations of legacy applications that depend on instance-level features Azure SQL Database does not expose. It trades some of the PaaS simplicity and cost efficiency for compatibility.
5.4 Azure Cosmos DB
Cosmos DB is a globally distributed, horizontally partitioned NoSQL database offering single-digit-millisecond latency at massive scale, turnkey multi-region replication, and multiple APIs (NoSQL, MongoDB, Cassandra). Its defining features are the partition key (which determines scalability) and five tunable consistency levels, letting you trade consistency for latency and availability per workload.
// Cosmos DB: the partition key choice determines scale and hot-partition risk.
var container = cosmos.GetContainer("bank", "transactions");
await container.CreateItemAsync(
new Transaction { Id = txId, AccountId = accountId, Amount = amount },
new PartitionKey(accountId)); // partition by account: even, query-friendly
// Point reads by id + partition key are the cheapest, fastest operation (1 RU-ish).
var read = await container.ReadItemAsync<Transaction>(txId, new PartitionKey(accountId));
5.5 Cosmos DB Consistency Levels
Cosmos DB exposes a spectrum from Strong (linearizable, highest latency) through Bounded Staleness, Session (the common default - consistent within a client session), and Consistent Prefix, to Eventual (lowest latency, weakest guarantee). This is a per-request, business-driven trade-off: a balance display might use Session, while a global leaderboard tolerates Eventual.
5.6 PostgreSQL and Open-Source Options
Azure Database for PostgreSQL (Flexible Server) provides a managed, open-source relational platform for teams standardized on PostgreSQL or needing its extensions, with similar managed benefits: HA, backups, read replicas, and scaling. MySQL is similarly available.
5.7 Scaling and High Availability
Relational databases scale primarily vertically, with read replicas offloading reads and zone-redundant HA plus geo-replication for availability and DR. Cosmos DB scales horizontally by partition and replicates across regions natively. The diagram shows a read-scaling pattern: writes to the primary, reads served from a replica.
sequenceDiagram autonumber participant App as Application participant P as Primary (writes) participant R as Read Replica App->>P: INSERT/UPDATE (transactional) P-->>App: committed P--)R: async replication App->>R: SELECT (reporting / read-heavy) R-->>App: rows (slightly stale) Note over P,R: Offloads read load; replica lag = eventual consistency for reads
5.8 Security and Cost
Secure databases with Entra ID authentication (no SQL logins where possible), private endpoints, Transparent Data Encryption at rest, TLS in transit, and auditing. For cost, right-size the tier, use serverless/auto-pause for intermittent workloads, provision Cosmos throughput carefully (autoscale RU/s or serverless), and design partition keys and indexes to minimize request units.
How do you choose between Azure SQL and Cosmos DB?
I choose by data model, consistency, and scale requirements. Azure SQL fits relational, transactional workloads needing ACID guarantees, complex joins and queries, and strong consistency - most line-of-business and financial systems. Cosmos DB fits workloads needing global distribution, elastic horizontal scale, predictable low latency at massive volume, and a flexible schema, where you can model access around a partition key and tolerate tunable (often non-strong) consistency. If the workload is a global, high-throughput, key-based access pattern (user profiles, IoT, catalogs), Cosmos shines; if it is relational with rich querying and strict consistency (a ledger, orders), Azure SQL is the better fit. Many systems use both - SQL for the transactional core, Cosmos for globally distributed read-heavy experiences.
Explain Cosmos DB consistency levels and how you pick one.
Cosmos DB offers five levels trading consistency against latency and availability. Strong gives linearizable reads (always the latest committed write) at the highest latency and cost and cannot span regions for writes usefully. Bounded Staleness guarantees reads lag by at most a defined number of versions or time. Session (the default) guarantees consistency within a single client session - you always read your own writes - which suits most user-facing apps. Consistent Prefix guarantees reads never see out-of-order writes. Eventual gives the lowest latency and highest availability with the weakest guarantee. I pick per workload: a financial balance a user just changed wants at least Session (read-your-writes); a globally replicated leaderboard or view counter is fine with Eventual. It is a business decision about how much staleness the feature can tolerate versus how much latency and cost you will pay.
Why is the partition key choice so critical in Cosmos DB?
The partition key determines how Cosmos distributes data and throughput across physical partitions, so it directly governs scalability and cost. A good key has high cardinality and even access distribution, so reads and writes spread across partitions and each stays within throughput limits. A poor key creates “hot partitions” - one key receiving a disproportionate share of traffic - which throttles (rate-limits) requests even though total provisioned throughput seems adequate, and you cannot change a container’s partition key without migrating data. The key should also align with your most common query so you can do efficient single-partition queries and cheap point reads rather than costly cross-partition fan-outs. Getting it right up front is one of the most consequential Cosmos design decisions.
How do you design the data architecture for a global banking platform with a transactional core and read-heavy customer experiences?
I’d use polyglot persistence, matching each store to its job. The transactional core - accounts, ledger, payments - goes on Azure SQL (or Managed Instance) for ACID guarantees, strong consistency, and rich relational integrity, deployed zone-redundant with geo-replication for DR and a strict RTO/RPO. The read-heavy, globally distributed customer experiences - transaction history views, profiles, dashboards - go on Cosmos DB, partitioned by account or customer for even distribution and replicated to the regions where users are, using Session consistency so customers always see their own writes. I connect them with an event-driven projection: the SQL core emits domain events (via an outbox) that populate Cosmos read models, accepting eventual consistency for the read side while the money-moving path stays strongly consistent. Everything is secured with managed identities, private endpoints, and encryption, and throughput/cost is controlled with autoscale and careful indexing. The principle is strong consistency where money moves, horizontal scale and low latency where users read.
A team defaults every new service to Cosmos DB "because it scales." How do you guide them?
I’d challenge the default, because Cosmos is not a free lunch and is the wrong tool for many workloads. Its scale comes from a partition-key data model and a request- unit cost model that punish relational access patterns - joins, ad-hoc queries, transactions spanning entities - which are natural in SQL and awkward or expensive in Cosmos. Teams that reach for it reflexively often end up modeling relational data into documents, fighting hot partitions, and paying high RU costs for cross- partition queries, all to solve a scale problem they do not actually have. I’d have them justify the choice against real requirements: do they need global distribution, horizontal scale beyond a large SQL instance, and single-digit-millisecond latency at high volume? If yes, Cosmos with a well-designed partition key is excellent. If the workload is relational and moderate-scale - which most line-of-business services are - Azure SQL is simpler, cheaper, and a better fit. The guidance is to choose the data store from the workload’s access pattern and scale, not from a blanket preference.
5.9 Banking Enterprise Scenario
A bank runs its ledger and payments core on Azure SQL Database, zone-redundant with active geo-replication to a paired region for a low RTO/RPO, using Entra ID auth and private endpoints. Customer-facing transaction history and profile views run on Cosmos DB, partitioned by account id and replicated to the regions where customers live, served at Session consistency so users always see their latest activity. The SQL core publishes domain events through a transactional outbox that project into the Cosmos read models, keeping money movement strongly consistent while the read experiences scale globally with low latency. A legacy reporting application that needs SQL Agent and cross-database queries runs on a Managed Instance during modernization.
Chapter Revision Notes
- Azure SQL for relational, ACID, strongly consistent transactional workloads.
- Managed Instance for lift-and-shift needing full SQL Server compatibility.
- Cosmos DB for global distribution, horizontal scale, and low latency.
- The Cosmos partition key drives scale - pick high-cardinality, even, query-aligned.
- Cosmos consistency is a per-workload trade-off from Strong to Eventual.
- Use polyglot persistence: strong consistency where money moves, scale where users read.