Check if an Azure Synapse Cosmos DB linked service container is a Read-Only replica

Mariano Peinador Perez 20 Reputation points Microsoft Employee
2025-01-06T14:56:01.58+00:00

I'm accessing a Cosmos DB through a linked service in Synapse. And we need to be able to verify if the replica we are accessing is readonly of if it has write capabilities; can we do this? I have tried several approaches but cant seem to find a way to check this, I understand CosmosDB has some metadata that may indicate if the instance is read-only or write-enabled.
This doesn't seem to work because replicaType column is not a standard column in the documents, I understand I'm getting this wrong because I'm trying to access the column in the collection instead of the metadata:
/**

  • ReadReplicaValidationLayer is responsible for ensuring that
  • the Cosmos DB endpoint used by the DataFrame is a read replica.
  • Responsibilities:
    • Validates if the Cosmos DB endpoint is a read replica using a lightweight query.
    • Aborts execution and breaks the pipeline if a write replica is detected.

*/

object ReadReplicaValidationLayer {

/**

  • Validates whether the specified Cosmos DB endpoint is a read replica
  • by executing a lightweight query to fetch the replicaType.
  • @param reader A DataFrameReader configured for Cosmos DB.
  • @throws IllegalStateException if the endpoint is a write replica.

*/

def validateReadReplica(readerSettings: DataSourceBuilderConfig): Unit = {

println("Executing lightweight query for read replica validation.")



// Create our owne reader to avoid modifying the original configuration

val validationReader = spark.read

  .format(readerSettings.sourceType)

  .option("spark.synapse.linkedService", readerSettings.linkedService.get)

  .option("spark.cosmos.preferredRegionsList", readerSettings.preferredRegions.getOrElse(""))

  .option("spark.cosmos.container", readerSettings.container.get)

// Use key value settings specified

readerSettings.keyVaultCreds.foreach { kv =>

    validationReader.option("spark.cosmos.accountKey", kv.accountKey)

          .option("spark.cosmos.database", kv.dbName)

          .option("spark.cosmos.accountEndpoint", kv.dbURI)

}

if (readerSettings.systemProperties) validationReader.option("spark.cosmos.read.inferSchema.includeSystemProperties", "true")



// Perform a lightweight query to fetch the top 10 documents

val validationDF = validationReader.load()

validateReplicaType(validationDF)

}

/**

  • Validates the replicaType column in the DataFrame to ensure it is a read replica.
  • @param df The DataFrame containing the replicaType column.
  • @throws IllegalStateException if the endpoint is a write replica.

*/

private def validateReplicaType(df: DataFrame): Unit = {

val replicaTypeColumn = "replicaType" // Assuming this column indicates the replica type

if (!df.columns.contains(replicaTypeColumn)) {

  println(s"WARN: Validation Skipped: Column '$replicaTypeColumn' not found in the DataFrame.")

  throw new IllegalStateException("Validation failed: `replicaType` column is missing.")

}

val replicaTypes = df.select(replicaTypeColumn).distinct().collect().map(_.getString(0))

val isWriteReplica = replicaTypes.contains("Write")

val isReadOnlyReplica = df.select("replicaType").distinct().collect().contains("ReadOnly")

if (!isReadOnlyReplica) {

  println("Validation Failed: Write-enabled replica detected. Aborting pipeline execution.")

  throw new IllegalStateException("Write-enabled replica detected. Aborting pipeline execution.")

} else {

  println("Validation Passed: Read replica confirmed.")

}

}

}

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,149 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,738 questions
{count} votes

Accepted answer
  1. Ganesh Gurram 3,270 Reputation points Microsoft Vendor
    2025-01-07T06:47:00.87+00:00

    Hi @Mariano Peinador Perez

    Thanks for the question and using MS Q&A platform.
    To determine whether the Cosmos DB replica you're accessing through your Azure Synapse Analytics linked service is a read-only replica or a write-enabled replica, there are a few things to consider:

    Replica Type in Cosmos DB - Cosmos DB doesn't store the replica type (like ReadOnly or Write) as part of the document data. Instead, this information is part of the Cosmos DB account's region configuration. So, querying the documents directly won’t give you this information.

    Check Synapse Linked Service Configuration - When you configure the linked service in Synapse, you can specify preferred regions. These can either be write-enabled or read-only regions.

    If your linked service points to a read-only region, then you’re accessing a read-only replica.

    If it points to a write-enabled region, you’re working with a write-enabled replica.

    To check whether you're connected to a read-only or write-enabled replica, you can use the Azure SDK or the Cosmos DB REST API. These will let you query the Cosmos DB account's region configurations and check if the region you're accessing is set as ReadOnly or Write.

    Synapse Pipeline Validation - If you’re automating this process in a Synapse pipeline, you can validate the replica type by reviewing the region settings in the linked service configuration. Ensure you are connecting to the correct region type (read-only or write-enabled).

    You won’t find a replicaType field in your Cosmos DB documents. Instead, the best way to confirm the replica type is to look at the region configuration in your Cosmos DB account settings. If you're connecting to a read-only region, then you're using a read-only replica. If the region is write-enabled, you're working with a write-enabled replica.

    Hope this helps. Do let us know if you have any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.