Introducing Microsoft Azure Cloud and Cosmos DB Microsoft Azure Cloud features Microsoft Azure as an SPI Azure as PaaS Azure as IaaS Azure components Getting started with Azure Compute m
Trang 2Learning Azure Cosmos DB
A beginner's guide to creating scalable, globally distributed, and highly responsive applications usingCosmos DB
Shahid Shaikh
Trang 3BIRMINGHAM - MUMBAI
Trang 5Learning Azure Cosmos DB
Copyright © 2018 Packt Publishing
All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Sunith Shetty
Acquisition Editor: Chandan Kumar
Content Development Editor: Amrita Noronha
Technical Editor: Nilesh Sawakhande
Copy Editor: Safis Editing
Project Coordinator: Shweta H Birwatkar
Proofreader: Safis Editing
Indexer: Aishwarya Gangawane
Graphics: Tania Dutta
Production Coordinator: Aparna Bhagat
Trang 6Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well asindustry leading tools to help you plan your personal development and advance your career For moreinformation, please visit our website
Trang 7Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Trang 8Did you know that Packt offers eBook versions of every book published, with PDF and ePub filesavailable? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, youare entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more
details
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of freenewsletters, and receive exclusive discounts and offers on Packt books and eBooks
Trang 9Contributors
Trang 10About the author
Shahid Shaikh is an engineer, published author, and a blogger He has written tons of tutorials on
Node.js and various databases on his blog He is a believer in open source and almost all his sourcecode is on GitHub He has also developed and launched a SaaS product called Spark to handle
content sharing automation Currently, he is building a next-generation email marketing tool calledHashmailer
I would like to acknowledge my parents for supporting me throughout the book-writing phase I had to work on weekends and cancel lots of plans to deliver the chapters on time, and my family and friends canceled various things because of me and supported me while I was writing the book, which I am very thankful for.
Trang 11About the reviewer
Kasam Ahmed Shaikh is a seasoned professional with a 'could be' attitude with 10 years of
determined industry experience with one of the leading IT companies in Mumbai, India He is a
certified Azure architect, recognized as C# Corner MVP He is also a speaker, and has written a book
on Azure cognitive services He is also the founder of DearAzure.net and Shaikh Academy
Very first, I would like to thank Almighty Allah, my family, and in particular, my better half for motivating me throughout the process I am very thankful to Packt Publishing for believing and considering me for this opportunity.
Trang 12Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today
We have worked with thousands of developers and tech professionals, just like you, to help themshare their insight with the global tech community You can make a general application, apply for aspecific hot topic that we are recruiting an author for, or submit your own idea
Trang 13Table of Contents
Title Page
Copyright and Credits
Learning Azure Cosmos DB
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files Download the color images Conventions used
Get in touch
Reviews
1 Introducing Microsoft Azure Cloud and Cosmos DB
Microsoft Azure Cloud features
Microsoft Azure as an SPI
Azure as PaaS Azure as IaaS Azure components
Getting started with Azure
Compute modules Data management modules Networking module Big data modules Caching module Messaging module Identity module Mobile service module IoT modules
Azure Cosmos DB
Turnkey global distribution Multilevel data models Scale on demand
High response with less latency Consistency models
Money-back guarantee and SLA Cosmos DB use cases
IoT Retail and marketing
Trang 14Gaming Web and mobile applications Summary
2 Cosmos DB Global Turnkey Distribution
Setting up an Azure subscription
Creating an Azure Cosmos DB database
Adding data in MongoDB
Distributing data using global turnkey distribution
Failover handling
Automatic failover Manual failover Summary
3 Cosmos DB Multi-model Databases
Cosmos DB data models
ARS DocumentDB API The MongoDB data model The tables data model The graph (Gremlin) data model Cassandra API
The Cosmos DB consistency levels
Summary
4 Querying Cosmos DB
Performing CRUD operations in DocumentDB
DocumentDB query basics Creating data in Cosmos DB Reading data from DocumentDB Updating and deleting documents Performing CRUD operations in MongoDB
Performing CRUD operations in Graph DB
Performing CRUD operations in tables (key value store)
Performing CRUD operations in Cassandra.
Summary
5 Integrating Cosmos DB with Node.js
Node.js setup
Cosmos DB MongoDB setup
Connecting Cosmos DB with the Node.js app
App data in Cosmos DB
Integrating the DocumentDB API with Node.js
Integrating the Table API with Node.js
Integrating the Graph API with Node.js
Summary
6 Building a High-Performance Link Shortening and Tracking System Using Cosmos DB
Why do we need URL shortening?
Requirements of the system
Capacity estimation
Trang 15Database design
Encoding URL algorithms
Node app development
Security and permission
Summary
7 Building a Highly Available, Real-Time Email Tracking System using Cosmos
Is email tracking legal?
System design and assumptions
Choosing the database API in Cosmos DB
Writing our email sender
Tracking emails and storing tracking data
Backup and restore
Deploying a custom app over the Azure network
Monitoring traffic and usage
Database security
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Trang 16Azure Cosmos DB is Microsoft's managed database solution for individuals and enterprise with theindustry standard service-level agreement Azure Cosmos DB provides multi-model database supportthat can scale across more than 30 regions across the world Azure Cosmos DB provides a turn keydistribution that enables the database admins to scale and replicate their database with a few clicks
This book covers the major concepts and features of Azure Cosmos DB This book also covers thedevelopment of web applications using Cosmos DB and their integration using the software
development kit provided by Azure Cosmos DB
Trang 17Who this book is for
This book is intended for anyone who wants to get well versed with Microsoft's new NoSQLdatabase called Azure Cosmos DB You should have prior experience of dealing with any SQL orNoSQL databases
Trang 18What this book covers
Chapter 1, Introducing Microsoft Azure Cloud and Cosmos DB, aims to provide an introduction to the
Microsoft Azure platform The reader will get to know the various interfaces and modules provided
by the Microsoft Azure cloud Another part of this chapter provides an introduction to Cosmos DB,which is the main topic of the book
Chapter 2, Cosmos DB Global Turnkey Distribution, covers one of the key features of Cosmos DB,
which is its global turn key distribution system You can replicate your database more than 30 regionsscattered across the globe using a very simple interface
Chapter 3, Cosmos DB Multi-model Databases, provides the four (five after publishing state of this
book) database models that you can pick from This chapter covers these database models in detail
Chapter 4, Querying Cosmos DB, covers the different types of queries required for multi-model
databases This chapter segregates these queries in the CRUD (Create, Read, Update, and Delete)way to explain it better
Chapter 5, Integrating Cosmos DB with Node.js, covers all integration with one of the popular
backend runtime engines, Node.js We have covered how to connect to the database models of
Cosmos DB with Node.js
Chapter 6, Building High-Performance Link Shortening and Tracking System Using Cosmos
DB, covers the development of a simple and high-performance link shortening and tracking system
that's very similar to bit.ly using Cosmos DB and Node.js The reader will learn and relate how theycan build a complex system like this using a Cosmos DB-managed database solution
Chapter 7, Building Highly Available and Real-Time Email Tracking System Using Cosmos
DB, covers the development of a system that handles the email tracking that is open rates of the email
by millions of users and stores them in the database for further analytics
Chapter 8, Cosmos DB Deployment and Management, covers all of the deployment of your custom
application and Cosmos DB database to the production environment
Trang 19To get the most out of this book
Get the latest version of Node.js installed and learn MongoDB query basics to get the most out of thisbook
Trang 20Download the example code files
You can download the example code files for this book from your account at www.packtpub.com If youpurchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files
emailed directly to you
You can download the code files by following these steps:
1 Log in or register at www.packtpub.com
2 Select the SUPPORT tab
3 Click on Code Downloads & Errata
4 Enter the name of the book in the Search box and follow the onscreen instructions
Once the file is downloaded, please make sure that you unzip or extract the folder using the latestversion of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Learning-Azure -Cosmos-DB We also have other code bundles from our rich catalog of books and videos available at htt ps://github.com/PacktPublishing/ Check them out!
Trang 21Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book Youcan download it here: http://www.packtpub.com/sites/default/files/downloads/LearningAzureCosmosDB_ColorImages.p
df
Trang 22Conventions used
There are a number of text conventions used throughout this book
CodeInText: Indicates code words in text, database table names, folder names, filenames, file
extensions, pathnames, dummy URLs, user input, and Twitter handles Here is an example: "Let'sperform an UPDATE and DELETE operation."
A block of code is set as follows:
Bold: Indicates a new term, an important word, or words that you see onscreen For example, words
in menus or dialog boxes appear in the text like this Here is an example: "Click on New Databaseand choose the Gremlin as API."
Warnings or important notes appear like this.
Tips and tricks appear like this.
Trang 23Get in touch
Feedback from our readers is always welcome
General feedback: Email feedback@packtpub.com and mention the book title in the subject of your
message If you have questions about any aspect of this book, please email us at questions@packtpub.com
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do
happen If you have found a mistake in this book, we would be grateful if you would report this to us.Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Formlink, and entering the details
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be
grateful if you would provide us with the location address or website name Please contact us atcopyright@packtpub.com with a link to the material
If you are interested in becoming an author: If there is a topic that you have expertise in and you
are interested in either writing or contributing to a book, please visit authors.packtpub.com
Trang 24Please leave a review Once you have read and used this book, why not leave a review on the sitethat you purchased it from? Potential readers can then see and use your unbiased opinion to makepurchase decisions, we at Packt can understand what you think about our products, and our authorscan see your feedback on their book Thank you!
For more information about Packt, please visit packtpub.com
Trang 25Introducing Microsoft Azure Cloud and
Cosmos DB
The public cloud provides services such as virtual private servers and databases hosted as a service
to the general public on a subscription basis Some platforms, such as Amazon, charge an hourlysubscription or offer pay-as-you use (Lambda) services, while others charge a fixed amount for amonth, such as DigitalOcean, and so on The Windows Azure public cloud platform is one of the
important pillars of Microsoft's Cloud OS vision that is transforming the traditional data center
environment, helping businesses unlock insights in data stored anywhere, enabling the development of
a wide range of modern business applications, and empowering IT to support users who work
anywhere on any device while being able to manage these devices in a secure and consistent way
In this chapter, we are going to cover the following points related to Microsoft Azure Cloud andCosmos DB:
Azure Cosmos DB features
Azure Cosmos DB use cases
Let's get started by explaining the topics one by one
Trang 26Microsoft Azure Cloud features
One of the famous and simplest quotes about Azure goes as follows:
Windows Azure can be anything you want it to be
Microsoft Azure Cloud provides a wide range of different services Windows Azure lets you build,deploy, and manage solutions for almost any purpose you can imagine
Windows Azure is a world of unlimited possibilities Irrespective of whether you're a large
enterprise spanning several continents that has to run server workloads or a small business that needs
a website with a global presence, Windows Azure provides a platform for building applications thatcan leverage the cloud to meet the needs of your business
Microsoft Azure provides the following key features:
Open: Azure provides vast collections of software development kits (SDKs) and tools for
various languages in order to build scalable cloud-based applications
Flexible: Whether you are running a small business or a huge, high-end processing system,
Windows Azure provides various services and scalability to help you with all the stages ofyour business
Microsoft-managed: Microsoft has data centers across three continents—the United States,
Europe, and Asia Microsoft itself manages these data centers instead of hiring third-party
people to run these, which most other providers do
Compatible: You can integrate your existing cloud premise with Azure easily using either
support teams or tools
CDN: Azure provides a content delivery network (CDN) that consists of a geographically
distributed system of servers that enables Windows Azure to deliver high-bandwidth content forperformance-sensitive customers by caching data, images, pictures, and video from WindowsAzure compute instances
Distributed database: Azure cloud provides easy-to-manage distributed databases such as SQL
Server and Cosmos DB to help developers scale and build global range applications
You can refer to this diagram for all of the components Azure Cloud offers:
Trang 27Let's dive more into what Azure can do as a serial peripheral interface (SPI).
Trang 28Microsoft Azure as an SPI
There are many cloud computing platforms offered by different organizations such as Google Cloud,Amazon Web Services, and so on Windows Azure is one of them, and is provided by MicrosoftAzure
Azure is described as the managed data centers that are used to build, deploy, and manage the
applications and provide services through a global network Platform as a service (PaaS) and
infrastructure as a service (IaaS) are the services provided by Microsoft Azure It
supports majority of the programming languages
Trang 29Azure as PaaS
Azure provides platforms to clients to develop and deploy software without worrying about the
server infrastructure and configuration You can focus on the development of the application and
leave the infrastructure worries to Azure
One of the key perks of using Azure is the overall cost, which is low as the resources are allocated ondemand and servers are automatically updated Azure provides automatic updates to avoid securityissues, and it does all that in the background without affecting the application
Trang 30Azure as IaaS
Azure provides a managed compute service that gives complete control of the operating systems andthe application platform stack to the application developers It provides developers with control overthe data center and allows them to manage it
This is the ideal case for projects where complete control is required The virtual machine can becompletely altered to the requirements of the project or business However, if not managed carefully,
it could become troublesome; all the security updates then become the job of the developer who
manages the infrastructure
The maintenance of the legacy application also sometimes becomes difficult due to operating systemand application stack updates
Trang 31Azure components
Please refer to the following diagram, consisting of Azure components:
As you can see in the preceding diagram, there are different execution models, in particular the
distinctive advancements that can be utilized to execute applications running in the Windows Azurecloud The execution models mainly consist of the four Windows Azure compute services, websites,cloud services, and mobile services
At the base are the SDKs for different languages which you can use for building, sending, and
overseeing applications that can be kept running on Windows Azure
The SDKs that are presently upheld incorporate NET, Java, PHP, Node.js, Ruby, Swift, and Python.There is additionally a general Windows Azure SDK that gives essential help to any programmingdialect, for instance C++
Enough of the theories; let's get started with Azure
Trang 32Getting started with Azure
To start with Azure, first you need to visit Microsoft Azure and create a new free account or use yourexisting Hotmail account The free account comes with a credit and you can use that credit for 12months You need to add your credit card information and verify it Microsoft Azure won't charge youwithout the required action from the user's side:
When your account is created, log in into the Azure portal Once your login is successful, you shouldsee a screen similar to the following:
Trang 33Let's understand the components and functionalities of each module in brief.Azure components are divided into the following major components:
Compute modules
Data management modules
Networking module
Big data module
Caching and messaging module
Trang 34Compute modules
As the name suggests, the job of this module is to provide a set of tools and services to do computing.Here is the list of services which are provided under compute modules:
As seen in the preceding screenshot, these components mainly consist of virtual machines and
containers, but as per the requirements you can join them together as well
For example, a virtual machine with more disks and snapshots along with containers is used for
continuous delivery
Let's look over the next component, the data management modules
Trang 35Data management modules
Data management modules consist of SQL Server, Azure Cosmos DB, Redis, PostgreSQL, and datawarehouses You can deploy these data management modules and use them in your app
Here is the complete list of data management modules:
We are going to cover Azure Cosmos DB in an upcoming section Microsoft Azure started to supportMySQL and PostgreSQL in the current version (only at the time of writing this book)
Let's look over the networking module
Trang 36Networking module
The Azure traffic manager is in charge of directing the request of a user cleverly to an accessible datacenter The procedure includes finding the closest data center to the user who makes the demand forthe web application, and if the closest data center is not accessible because of different reasons, thetraffic manager deviates the demand to another data center
Be that as it may, rules are set by the administrator of the application regarding how the traffic
manager should act in such cases The virtual network permits a system between nearby machines atyour commencing place and a virtual machine in Azure's data center IPs to virtual machines can beappointed for identity and accessibility purposes Virtual network setup requires a virtual privatenetwork device
Azure also provides an application gateway which is very useful in micro services application
development
Following is the list of networking modules Azure supports at the time of writing this book:
Trang 37Let's look over the big data and business intelligence modules.
Trang 38Big data modules
The extensive measure of data can be stored and retrieved using Windows Azure Azure offers
HDInsight, which is a Hadoop-based service to determine and sense out enormous information stored
as big data Companies regularly need to oversee extensive measures of information which are notfundamentally relational in nature Hadoop is used by many organizations nowadays, and it is used inAzure too
In this manner, Azure offers Hadoop benefits on their platform for their customers The term big
compute alludes to high-performing calculations This is accomplished by executing code in parallel
on many machines in the meantime
The components offered by big data modules are shown in following screenshot:
Trang 39As you can see, Azure offers big data services ranging from storage using Hadoop clusters to
performing analytics over big data, performing log analytics to determine errors, and generating
reports based on your data
Azure also provides machine learning web services to be used as smart algorithms to train and derivesense out of data
Let's look over the caching and messaging modules
Trang 40Caching module
The Windows Azure caching service helps you to build a high-end, scalable, and dedicated cache foryour website
Azure cache is offered in three different types:
1 Basic: A cache size less than 1 GB comes with this type.
2 Standard: A cache size ranging from 1 GB to 10 GB comes with this type.
3 Premium: A cache size more than 5 GB and less than 150 GB comes with this type.
Let's look over the messaging module