Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Pragmatic Microservices with C# and Azure

You're reading from   Pragmatic Microservices with C# and Azure Build, deploy, and scale microservices efficiently to meet modern software demands

Arrow left icon
Product type Paperback
Published in May 2024
Publisher Packt
ISBN-13 9781835088296
Length 508 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Christian Nagel Christian Nagel
Author Profile Icon Christian Nagel
Christian Nagel
Arrow right icon
View More author details
Toc

Table of Contents (23) Chapters Close

Preface 1. Part 1: Creating Microservices with .NET
2. Chapter 1: Introduction to .NET Aspire and Microservices FREE CHAPTER 3. Chapter 2: Minimal APIs – Creating REST Services 4. Chapter 3: Writing Data to Relational and NoSQL Databases 5. Chapter 4: Creating Libraries for Client Applications 6. Part 2: Hosting and Deploying
7. Chapter 5: Containerization of Microservices 8. Chapter 6: Microsoft Azure for Hosting Applications 9. Chapter 7: Flexible Configurations 10. Chapter 8: CI/CD – Publishing with GitHub Actions 11. Chapter 9: Authentication and Authorization with Services and Clients 12. Part 3: Troubleshooting and Scaling
13. Chapter 10: All About Testing the Solution 14. Chapter 11: Logging and Monitoring 15. Chapter 12: Scaling Services 16. Part 4: More communication options
17. Chapter 13: Real-Time Messaging with SignalR 18. Chapter 14: gRPC for Binary Communication 19. Chapter 15: Asynchronous Communication with Messages and Events 20. Chapter 16: Running Applications On-Premises and in the Cloud 21. Index 22. Other Books You May Enjoy

Creating an Azure Cosmos database

From the Azure portal, you can open the page for your Azure Cosmos DB account, open Data Explorer, and from there, click on New Database to create a new database, and New Container to create a container within the database. Here, we’ll use the Azure CLI instead:

az cosmosdb sql database create --account-name <your cosmos account name> -n codebreaker -g rg-codebreaker-test --throughput 400

This command creates a database named codebreaker in the existing account. Setting the throughput option with this command defines the scale of the database. Here, all containers within this database share the 400 RU/s throughput. 400 is the smallest value that can be set. Instead of supplying this value when creating the database, scaling can also be configured with every container. In case some containers should not take away scaling from other containers, configure the RU/s with every container – but here, the minimum value to be used with each container is 400 as well.

After creating the database, let’s create a container:

az cosmosdb sql container create -g rg-codebreaker-test -a <your cosmos account name> -d codebreaker -n GamesV3, --partition-key-path "/PartitionKey"

The implementation of the gamesAPI service uses a container named GamesV3. This container is created within the previously created database, using the /PartitionKey partition key, as was specified with the EF Core context in Chapter 3.

After this command is completed, check Data Explorer in the Azure portal, as shown in Figure 6.6:

Figure 6.6 – Data Explorer

Figure 6.6 – Data Explorer

You can see the database, the container, and, with the container, the configured partition key.

Configuring replication with Azure Cosmos DB

A great feature of Azure Cosmos DB is global data replication. Within the Azure portal, in the Settings category, click on Replicate data globally. Figure 6.7 shows the replication view:

Figure 6.7 – Replication with Azure Cosmos DB

Figure 6.7 – Replication with Azure Cosmos DB

You just need to click on the Azure regions that are available with your subscription to replicate data within the selected regions. You can also configure it to write to multiple regions.

With the codebreaker application where users around the world can play, for faster performance for users in the US, Europe, Asia, and Africa, writing to multiple regions can be configured. For this option to be available, automatic scaling cannot be configured. For the best scalability across the globe, we also need to think about the partition key. By using different partition key values for every game that’s stored, games can be stored within different partitions.

Configuring consistency

With the Settings category in the Azure portal of Azure Cosmos DB, we can configure the default consistency level. The outcomes are shown using music notes, reading, and writing from multiple regions, as shown in Figure 6.8:

Figure 6.8 – Outcome shown using music notes

Figure 6.8 – Outcome shown using music notes

The default setting is Session consistency – the data is consistent within the same session. With this setting, write latencies, availability, and read throughput are comparable to Eventual consistency. Using the Azure Cosmos DB API, a session can be created and distributed within the application.

The Strong consistency option is not available if multiple regions are configured. With multiple regions, Bounded staleness can be configured, which specifies a maximum lag time and a number of maximum lag operations before the data is consistently replicated.

The database is now ready to use, so let’s publish Docker images to the registry!

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image