Skip to content

Databases

First PublishedByAtif Alam

Azure provides fully managed databases — relational and NoSQL — that handle patching, backups, high availability, and scaling.

Azure SQL is the managed version of Microsoft SQL Server. It comes in three deployment options:

OptionWhat It IsBest For
Azure SQL DatabaseSingle database (serverless or provisioned)New cloud-native apps
Elastic PoolMultiple databases sharing resourcesSaaS apps (one DB per tenant)
SQL Managed InstanceNear-complete SQL Server compatibilityLift-and-shift of on-prem SQL Server
Terminal window
# Create a server (logical container for databases)
az sql server create \
--resource-group myapp-rg \
--name my-sql-server \
--admin-user sqladmin \
--admin-password 'S3cure!Pass123'
# Create a database
az sql db create \
--resource-group myapp-rg \
--server my-sql-server \
--name myappdb \
--edition GeneralPurpose \
--compute-model Serverless \
--auto-pause-delay 60 \
--min-capacity 0.5
# Allow Azure services to connect
az sql server firewall-rule create \
--resource-group myapp-rg \
--server my-sql-server \
--name AllowAzure \
--start-ip-address 0.0.0.0 \
--end-ip-address 0.0.0.0
TierComputeBest For
General PurposeBalanced (standard SSD)Most workloads
Business CriticalHigh IOPS (local SSD, built-in read replica)Low-latency, mission-critical
HyperscaleScale up to 100 TB, near-instant backups, up to 4 read replicasVery large databases

Azure SQL’s serverless tier auto-scales and can pause when idle:

No activity ──► Auto-pause (60 min default) ──► $0 compute cost
Traffic arrives ──► Auto-resume (~1 min) ──► Scale up

You pay for storage when paused, but not compute. Great for dev/test and variable workloads.

TierHA MethodFailover
General PurposeRemote storage replicasManaged failover (~30s)
Business CriticalAlways On (local replicas, built-in read)Automatic (~30s)
HyperscaleMultiple compute replicasAutomatic, read replicas always available
Terminal window
# Create a readable geo-replica in another region
az sql db replica create \
--resource-group myapp-rg \
--server my-sql-server \
--name myappdb \
--partner-server my-sql-server-eu \
--partner-resource-group myapp-eu-rg

Use auto-failover groups for automatic DNS-based failover between regions.

FeatureHow to Enable
Private endpointAccess via private IP in your VNet
Encryption at restTransparent Data Encryption (TDE) — on by default
Encryption in transitTLS — enforced by default
Azure AD authenticationLogin with Entra ID instead of SQL credentials
Dynamic data maskingMask sensitive columns from non-privileged users
AuditingLog all database operations to Storage or Log Analytics
Advanced Threat ProtectionDetect SQL injection, anomalous access

Azure Cosmos DB is a globally distributed, multi-model NoSQL database. It’s Azure’s answer to DynamoDB — but more flexible, supporting multiple APIs and data models.

APIData ModelWhen to Use
NoSQL (native)JSON documentsNew apps, most flexible
MongoDBMongoDB-compatible documentsMigrating from MongoDB
CassandraWide-columnMigrating from Cassandra
GremlinGraphSocial networks, knowledge graphs
TableKey-valueMigrating from Azure Table Storage
PostgreSQLRelational (distributed)Distributed PostgreSQL (Citus)
Cosmos DB Account
└── Database: myapp
└── Container: orders (like a table/collection)
├── Item: {id: "1", customer: "alice", amount: 50}
├── Item: {id: "2", customer: "bob", amount: 30}
└── Partition key: /customer
ConceptWhat It Is
AccountTop-level resource. Choose API, regions, and consistency.
DatabaseLogical grouping of containers.
ContainerWhere data lives (like a table). Has a partition key and throughput.
ItemA single document/row (up to 2 MB).
Partition keyDetermines data distribution. Choose a high-cardinality key.
Terminal window
# Create account
az cosmosdb create \
--name mycosmosacct \
--resource-group myapp-rg \
--locations regionName=eastus failoverPriority=0 \
--locations regionName=westeurope failoverPriority=1
# Create database
az cosmosdb sql database create \
--account-name mycosmosacct \
--resource-group myapp-rg \
--name myapp
# Create container
az cosmosdb sql container create \
--account-name mycosmosacct \
--resource-group myapp-rg \
--database-name myapp \
--name orders \
--partition-key-path /customer \
--throughput 400
ModelHow It WorksBest For
ProvisionedSet RU/s (Request Units per second)Predictable traffic
AutoscaleScales between min and max RU/sVariable traffic
ServerlessPay per request (no provisioned RU/s)Dev/test, low/intermittent traffic

Request Units (RU): A single point read (1 KB item by ID + partition key) costs 1 RU. Writes cost ~5-10 RU. Complex queries cost more.

Cosmos DB’s killer feature — multi-region, active-active replication with single-digit ms latency:

Write in East US ──► replicate ──► West Europe (read replica)
──► Southeast Asia (read replica)

Enable multi-region writes for active-active (writes in any region).

LevelGuaranteeLatencyUse Case
StrongLinearizable (latest write always)HighestFinancial transactions
Bounded stalenessReads lag by at most N versions or T timeHighLeaderboards, metrics
Session (default)Read-your-own-writes within a sessionModerateMost applications
Consistent prefixReads are in order but may lagLowChat, activity feeds
EventualNo ordering guaranteeLowestCounters, non-critical
from azure.cosmos import CosmosClient
client = CosmosClient("https://mycosmosacct.documents.azure.com", credential)
database = client.get_database_client("myapp")
container = database.get_container_client("orders")
# Create
container.upsert_item({
"id": "order-001",
"customer": "alice",
"amount": 49.99,
"status": "shipped"
})
# Read (point read — 1 RU)
item = container.read_item("order-001", partition_key="alice")
# Query
results = container.query_items(
"SELECT * FROM orders o WHERE o.customer = @customer AND o.amount > @min",
parameters=[
{"name": "@customer", "value": "alice"},
{"name": "@min", "value": 20}
],
partition_key="alice"
)

Managed PostgreSQL and MySQL — equivalent to AWS RDS.

OptionWhat It IsBest For
Flexible Server (recommended)Zone-redundant HA, burstable compute, stop/startMost workloads
Single Server (legacy)Basic managed PostgreSQL/MySQLExisting deployments
Terminal window
az postgres flexible-server create \
--resource-group myapp-rg \
--name my-postgres \
--admin-user pgadmin \
--admin-password 'S3cure!Pass123' \
--sku-name Standard_D2s_v3 \
--tier GeneralPurpose \
--storage-size 128 \
--version 16 \
--high-availability ZoneRedundant
FeatureDetail
HAZone-redundant (standby in another AZ, automatic failover)
Read replicasUp to 10 replicas (async, cross-region supported)
BackupsAutomatic daily, 7–35 day retention, point-in-time restore
Stop/StartPause dev servers to save costs
VNet integrationPrivate access via VNet (no public IP)
Entra ID authAuthenticate with managed identities
ScenarioChoose
SQL Server migration / .NET ecosystemAzure SQL
PostgreSQL or MySQL workloadAzure Database for PostgreSQL/MySQL
Global distribution, multi-model NoSQLCosmos DB
Key-value at massive scale, simple accessCosmos DB (NoSQL API)
Document database (MongoDB compatible)Cosmos DB (MongoDB API)
Graph queriesCosmos DB (Gremlin API)
In-memory cachingAzure Cache for Redis
Data warehouse / analyticsAzure Synapse Analytics
  • Azure SQL is managed SQL Server. Use the serverless tier for variable workloads (auto-pause saves costs). Business Critical tier for low-latency production.
  • Cosmos DB is globally distributed NoSQL with multiple APIs. Choose the right consistency level and partition key. Pay per RU.
  • Azure Database for PostgreSQL/MySQL (Flexible Server) is the equivalent of AWS RDS — zone-redundant HA, read replicas, point-in-time restore.
  • Use Private Endpoints to access databases over private IPs. Use Entra ID authentication where supported.
  • Always enable encryption at rest, automated backups, and restrict access via NSGs or Private Endpoints.