Azure Cosmos DB has been a go-to solution for multi-tenant applications due to its global distribution, automatic scaling, and flexible data models. Its partition-based architecture naturally aligns with tenant isolation requirements, making it attractive for SaaS platforms, IoT applications, and content management systems.
However, even with these capabilities, the fundamental multi-tenant partitioning challenges persist. This blog post explores an approach to solving multi-tenant scaling challenges in Go applications using Azure Cosmos DB. You'll learn how to implement this using the Go SDK for Azure Cosmos DB, focusing on how to achieve efficient data distribution and query performance using hierarchical partition keys in Azure Cosmos DB.
Run the loader to populate the database with sample data that uses hierarchical partition keys. Its a CLI application that generates user session data for users in different tenant types (Enterprise, Mid-market, Small business) and inserts it into the Cosmos DB container.
Clone the repository and change into the load directory:
git clone https://github.com/abhirockzz/cosmosdb-go-hierarchical-partition-keys cd cosmosdb-go-hierarchical-partition-keys/loadBuild the data loader application and run it. The database and container will be created automatically if they do not exist.
go build -o data-loader main.go ./data-loader -rows 100 -database <insert database name> -container <insert container name>To examine how different query patterns perform you can comment out the relevant sections in the main function of the query/main.go file, set the required environment variables, and run the application.
export COSMOS_DB_ENDPOINT=https://your-account.documents.azure.com:443/ export COSMOS_DB_DATABASE_NAME=<insert database name> export COSMOS_DB_CONTAINER_NAME=<insert container name> cd cosmosdb-go-hierarchical-partition-keys/query go run main.goRead this blog post for detailed examples.