Access the global distribution database with Spring-mongodb Cosmos DB (3)

Source: Internet
Author: User
Tags install mongodb mongodb client azure cosmos db

One of the most important features of Azure Cosmos DB is global distribution, imagine you have multiple applications distributed globally, users are distributed globally, for example you have a new product listing, you have updated your catalog, SKU, price in the United States headquarters, you want users around the world can quickly local access, Guaranteed latency and performance, guaranteed user experience, how do you do it? If done in a traditional way, under the CAP law, this is a very complex system, and the cost is very high, and in Azure Cosmos DB, you only need a few mouse clicks, of course, the global replication of the regional configuration can also be through the command line, programming interface and other dynamic adjustment.

Azure Cosmos DB Global distribution configuration and management
    1. Open the management interface of Azure and go to the Cosmos DB main interface we created before, select the "Global Copy Data" menu and we can see the current. The MongoDB database we created is read and write only in northern China, no other replication or read area is configured, but we can see that there is another alternative region that can be configured:


Geographical Isolation Restrictions: due to the laws and regulations of different countries, such as China and Germany, the data replication region is confined to the physical region, for example, China can only choose Beijing, Shanghai is replicated to each other region, and can not copy the data to the cosmosdb of the overseas azure, So in the century connected azure you will see that the optional region is only China and China East, and if it is global azure, you can choose up to 30 + zones.

read-Write optimization: When you visit the COSMOSDB database, Cosmos DB provides services to ensure read latency and performance wherever you are in the region

    1. So how do we confirm that the host is currently connected, master or replica? We can use the native tool of MongoDB to view, need to install MongoDB client, in this test I use Ubuntu, the installation command is as follows:

sudo apt install mongodb-clients

The commands connected to MongoDB are as follows, username and password are consistent with the previous use of program connections

Mongo-u Mycosmos



--port 10255


    1. After connecting to COSMOSDB, you can use show DBS to view the current database and then use Db.ismaster () to check the status of the current database, from the results we can see very clearly that the currently connected server is master, The host name is, the port is 10255, and the region is China North:

    1. Next we configure global replication for Azure, go to the main Azure interface, select the "Global Replication" menu, tick "China East" area, select Save, configuration is very simple, of course, you can use the command line and SDK to dynamically configure, if necessary, you can consult the relevant documents After the configuration is complete, you can see that a new China east read-only zone is added:

    1. How do we check and access another replication site? How do I confirm that another site has started working? First we use the command Db.ismaster (), check the current status, we can card computer, global replication configuration is completed, in addition to the host in Beijing, a host of Shanghai, that is, we configure the replication site, you can also see the current connection is the master host, the Beijing database:



    1. We use the BACKUP database address to connect, after successful connection, the prompt will display "secondary", meaning the current connection is a replica replication server, the current parameters can also be seen:

      Mongo-u Mycosmos



      --port 10255


      "IsMaster": false, which represents the current connection is a replica

      The master server and the current server display:

      "PRIMARY": "",

      "Me": ""

    1. Querying the data for the current replica service has been synchronized with the primary server:

      Need to note:

  • Cosmos DB's global distribution database makes it possible for Cosmos db to automatically route you to the nearest region to provide services when connecting to the host access the database. From this point you can think of the function of the front entrance as a function similar to traffic manager, back-end automatic load and routing
  • When you connect to a write, the write request is routed from your current zone to the Master Master Library, then synchronized based on the consistency level you choose, with a correlation between consistency level and latency, in general, the higher the consistency level (such as strong consistency), the The higher the later, the later articles will be discussed in detail
  • Each newly added copy area is billed based on the same RU size and storage size as the main library
  • If you have more than one collection in your database, you can set his throughput for each collection, but can only be set using the command line at this time, for example, Azure CLI 2.0 is set up as follows:

    # Scale Throughput

    AZ cosmosdb collection update \

    --collection-name $collectionName \

    --name $name \

    --db-name $databaseName \

    --resource-group $resourceGroupName \

    --throughput $newThroughput

8. Simply test the geo-replication feature, we use SPRING-MONGODB in the primary database protocol two data, and then copy the database query:

Bike bike1=NewBike ("Bj008","Gen2","Beijing","China");
Bike1.setlatitude (23.34);
Bike1.setlongitude (24.76);
Bike1.setupdatedate (NewDate ());

Bike bike2=NewBike ("sh008", "Gen3", "Shanghai", "China");
Bike2.setlatitude (26.34);
Bike2.setlongitude (27.76);
bike2.setupdatedate (new Date ());

Then we copy the database in Shanghai to query, almost the millisecond level is copied, in the Shanghai data Center can see the replicated data:

    1. Finally, a brief introduction to Cosmos DB Billing, the basic costs include storage costs and the cost of preserving RU, if global replication is enabled, in addition to the storage and RU costs for each region, there will be network transfer charges, based on Azure standard network rates:

Storage cost is relatively simple, according to their own data volume to estimate, then the RU cost how to calculate?AzureA single request unit is defined inRUused to represent reads (by self-linking orID) A containingTenunique attribute value (except for System properties).1 KBthe processing capacity required for the item. and1Kthe data is written to5aRU, a calculationRUThe examples are as follows:

And for users to more easily calculate the ru you need, Microsoft has launched a calculator for the calculation of RU, you can upload your sample JSON file, request the demand, the calculator will help you calculate the required Ru, very convenient:


As you can see, a distributed database that supports multiple models, Azure Cosmos DB, is very convenient to use and configure, and can meet the needs of distributed data access under different business scenarios, and is now widely used in applications such as telematics, e-commerce, games, etc. that require large-scale distributed access, compared to other similar products, Its comprehensive SLA guarantees, low cost of ownership, multiple consistency level definitions, performance and latency assurance, and multiple model support are all very attractive to new applications, so you can try

Access the global distribution database with Spring-mongodb Cosmos DB (3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.