Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Developers
What Programmers Need to Know
about Microsofts Cloud Platform
John Adams
Additional
Resources
4 Easy Ways to Learn More and Stay Current
Programming Newsletter
Get programming r elated news and content delivered weekly to your inbox.
oreilly.com/programming/newsletter
OReilly Radar
Read more insight and analysis about emerging technologies.
radar.oreilly.com
Conferences
Immerse yourself in learning at an upcoming OReilly conference.
conferences.oreilly.com
2015 OReilly Media, Inc. The OReilly logo is a registered trademark of OReilly Media, Inc. #15305
Azure for Developers
John Adams
Azure for Developers
by John Adams
Copyright 2015 OReilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA
95472.
OReilly books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (http://safaribooksonline.com). For
more information, contact our corporate/institutional sales department:
800-998-9938 or corporate@oreilly.com.
The OReilly logo is a registered trademark of OReilly Media, Inc. Azure for Devel
opers, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher and
the author disclaim all responsibility for errors or omissions, including without limi
tation responsibility for damages resulting from the use of or reliance on this work.
Use of the information and instructions contained in this work is at your own risk. If
any code samples or other technology this work contains or describes is subject to
open source licenses or the intellectual property rights of others, it is your responsi
bility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-92612-3
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
iii
Preface
v
does not require permission. Incorporating a significant amount of
example code from this book into your products documentation
does require permission.
We appreciate, but do not require, attribution. An attribution usu
ally includes the title, author, publisher, and ISBN. For example:
Azure for Developers by John Adams (OReilly). Copyright 2015
OReilly Media, 978-1-491-92612-3.
If you feel your use of code examples falls outside fair use or the per
mission given above, feel free to contact us at permis
sions@oreilly.com.
vi | Preface
How to Contact Us
Please address comments and questions concerning this book to the
publisher:
Preface | vii
Microsoft Azure for Developers
By now, you have certainly heard of the cloud. Many major compa
nies are using cloud computing today to power websites, mobile
apps, and business software on a massive scale and can normally
even save some money while doing it. Amazon, Google, and Micro
soft have all released cloud computing platforms over the last decade
that are designed to host software and services that were tradition
ally run out of a companys own private data centers. Amazon was
the first to get started and is still probably the biggest supplier of
cloud-hosted virtual machines in the world. Google and Microsoft
launched their own platforms a few years later and have developed
and grown them in unique directions. Googles cloud platform offers
several hosting options and some unique and powerful big data
NoSQL services. Microsoft Azure, interestingly, has adopted the
most pluralistic approach to the cloud and embraces development
using a wide range of programming frameworks, operating systems,
and third-party services, so that a company can move its entire tech
nology structure into the cloud regardless of the technologies it
depends on.
These cloud platforms have enabled businesses and developers to
evolve their applications in ways previously unavailable and unfeasi
ble. This report will explore Microsofts cloud platform, Microsoft
Azure, and take you through the basics of the platform and its com
ponents, on into some examples of how to properly use it for your
own software solutions. As you can imagine, Microsoft Azure has
dozens of features that run the full spectrum of IT services, but we
will focus here specifically on the parts of Azure you can use as a
developer to build your applications in the cloud.
1
Cloud Hosting Options
Before we get into the specifics of Microsoft Azure, let me introduce
the basics of what cloud hosting means. In order to use the cloud for
your own applications, you have to choose how you want your code
to be hosted by the cloud platform. There are three distinct hosting
options for your apps that can be arranged on a scale of how much
responsibility lies with you versus how much responsibility lies with
the cloud hosting provider:
Infrastructure as a Service
IaaS hosting is a way to lease your hardware from a data center in
the form of virtual machines and virtual networks. This might be
what you think of first when you hear discussions about the cloud.
In this model, it is your responsibility to maintain the operating sys
tems and software that run on top of this leased infrastructure. Vir
tual machines can often be selected from a gallery of preconfigured
images with different operating systems, or you can upload your
own images if you have them. As your application grows, you can
scale it by moving it onto more powerful hardware (known as scal
ing up or scaling vertically) or by allocating more instances of
your virtual machines onto more servers (known as scaling out or
scaling horizontally). Scaling either way will increase your costs, of
Platform as a Service
PaaS hosting moves you up one level higher in the stack so that your
lowest level of concern is software instead of an operating system.
This is my favorite cloud hosting model because it relieves develop
ers of DevOps responsibilities like managing operating system
patches and virtual machine images, while providing automatic
deployment of application code and allowing us to retain complete
control over our own software and its environment
In this model, you create your application code and then deploy
your app to managed virtual machines that run your code automati
cally. You can scale these virtual machines up or out, just as in IaaS,
in order to serve more concurrent users. You can still normally
access the operating system, but you are not responsible for instal
ling or maintaining it. Instead of leasing hardware that runs your
virtual machines, like with IaaS, you are leasing virtual machines
that run your code.
However, you normally do not have as many operating system
choices here as with IaaS. Since they are managing the service, the
cloud provider likely only supports a small set of operating systems
for use by your application code and a specific set of supported pro
gramming frameworks that your application can utilize. Youll want
to make sure the programming framework you want to use is sup
ported before you make an investment here.
Web Hosting
Web hosting is a model where you lease a spot on a web server that
hosts your application files. Instead of initially leasing full servers or
virtual machines, you lease a managed directory on a multitenant
web server and access your own files through FTP. By giving up
control over the operating system and web server software, you gain
a simpler interface to your application and pay less for what you are
using. You have a lot less responsibility for maintaining various lev
els of the programming stack, but you simultaneously give up some
control and flexibility.
Compute
As we just discussed, compute-based services include all forms of
cloud hosting and some other related features. Even though the dif
ferent hosting options are unique, you are completely free to mix
and match them to suit your application needs. A single solution
inside of Microsoft Azure could easily use all three hosting models.
Virtual machine disk drives are basically virtual hard drives stored
as page blobs (more on that later) within Azure storage. Azure also
supports network drives that can be mounted to your server instan
ces through a service called Azure Files. Azure Files drives can be
accessed by more than one machine at a time and are billed at a sep
arate rate. Either way, you should be able to configure your virtual
machines as you need them with some combination of these drive
types.
When you create an instance of a virtual machine, you have to
choose the tier of hardware on which to deploy it. The price breaks
down based on the specifications of each hardware configuration.
There are two general categories, Basic and Standard. The Basic vir
tual machines are designed to handle basic workloads. They do not
support load balancing, autoscaling, or any high memory configura
tions. These machines are available in the configurations listed in
Table 1-1.
G series virtual machines (Table 1-4) are the newest offering from
Microsoft and are built on the highest-performing hardware avail
able. These machines feature Intel Xeon E5 v3 processors at up to 32
cores, twice as much memory, and about four times as much local
SSD storage than the D series machines.
Azure App Service Web Apps. Web Apps is a straightforward web host
ing platform where you are given a slot on a multitenant web server
and you can access your own source-file directory through FTP.
Every Azure Web App gets a standard DNS entry that ends in azur
ewebsites.net and can be accessed with HTTP and HTTPS out of
the box. Your web app can be built using one of many different sup
ported platforms, including .NET, Java, Node.js, PHP, and Python
(more are likely to be added in the future).
Every web app can take advantage of great developer features
including remote debugging and continuous integration (CI). Web
apps can be remotely debugged directly from within Visual Studio,
so you can step through code as it runs in the cloud. Each web app
project can also tie into several common source code repositories,
such as GitHub, Bitbucket, and Visual Studio Online, for CI features
like automatic builds and deployments when changes are commit
ted. Of course, many different IDE tools are also supported, so you
can use the tools you like best to build your app in the Azure cloud.
The Azure App Service tier you select has specific implications for
your web apps. The five tiers differ in terms of network bandwidth,
number of concurrent connections, and support for features like
SSL and custom DNS names as outlined in Table 1-8. These specifi
The Standard and Premium tiers have exclusive options for dedica
ted staging environments, some included SSL configuration, and site
backup options. Each Standard tier website gets five dedicated stag
ing environments for application life cycle management and testing,
and each Premium tier app gets 20. For example, you could deploy
your code changes to a develop environment and then promote
that code to other environments when ready.
In addition to all of this, Azure App Service has a rich marketplace
full of existing website packages you can immediately deploy and
use. Here you will find dozens of well-known platforms covering
content management systems (CMS), ecommerce, wikis, collabora
tion, blogs, starter sites, and more. Most of these marketplace gallery
options are free and open source, so you can host your own system
and use it however you want. Any related resources, such as a data
base server, will be automatically listed and configured for you.
Azure App Service API Apps. API Apps is a new offering from Micro
soft as part of Azure App Service. Web Apps is to websites as API
Apps is to HTTP-based API projects. The only real difference
between these services is that API apps are designed to be consumed
in a different way than web apps. Just like web apps, your API apps
can be built using .NET, Java, Node.js, PHP, and Python.
The special features of API apps come in the form of metadata man
agement, gallery listings, and access control. For metadata manage
ment, each API app gets autogenerated documentation on API end
points and HTTP verbs (through Swagger) with a web-based editor
and URL-rewriting abilities. Visual Studio can use this metadata to
autogenerate REST client code, much like it does for Web Services
Definition Language (WSDL) services, for easy consumption. Once
you have deployed an API app to Azure, you can choose whether to
list that API publicly in the gallery or keep it private within your
own app service.
Azure App Service Mobile Apps. Mobile Apps is a special platform that
Microsoft created to power mobile applications on any device. From
the portal, you can download ready-made project templates to build
your client-side app so that it automatically connects to this back
end. Mobile Apps will host your data in an easy format that can be
managed from the portal, and will also host your backend server
logic. It supports push notifications, offline sync, enterprise single
sign-on, social integration, mobile analytics for your app, and
autoscaling. Mobile Apps is a straightforward way of adding a
mobile app for your site hosted in an Azure app service.
Azure App Service Logic Apps. Logic Apps is another new offering
from Microsoft as part of Azure App Service. These apps are very
interesting because nothing like this has been available on Microsoft
Azure before. A logic app is a graphically configured workflow pro
Storage
Applications of every type need to store and manage data. Microsoft
Azure offers a wide range of storage types, including files, queues,
service bus relays and subscriptions, NoSQL databases like key/
value stores and document stores, a managed SQL database,
File storage
Azure provides three different ways to store files: Blob Storage,
Azure Files, and a content delivery network (CDN). Blob Storage is
part of the standard Azure storage account service; Azure Files and
CDN are separate services.
Blob storage. Blob Storage is a simple service for storing data as files
(if you havent heard the term blob before, it stands for Binary
Large OBject). It can store a very large number of files per account,
and it can store individual files that are very large. Blob Storage
organizes files within containers, like folders, and can apply specific
permissions to the contents of these containers to limit access to
them. The Blob Storage APIs allow for parallel uploads of binary
data so that large files can be uploaded as quickly as possible across
the Internet. Files inside of Blob Storage can be created in one of
two ways: as block blobs or as page blobs. These two blob types have
unique features and purposes.
Block blobs are the most common type and are designed for all nor
mal file operations. Block blobs are designed to be accessed as a sin
gle unit, like in a file download. They are a good choice for files that
need to be accessed across the Internet, such as website images or
user-uploaded content; for storing a large number of potentially
large files with low cost, such as log files or videos; and also as a
location to persist data that a transient application, like a cloud ser
vice, might need between sessions.
The page blob is a special type of file that supports nonsequential
reads and writes, such as are necessary for virtual hard drives. This
blob type has its own limits and performance metrics. As an inter
Message-based storage
Message-based storage systems, like Azure Queue Storage and
Azure Service Bus, are designed to enable different tiers of an n-tier
application to communicate in an occasionally connected, or asyn
chronous, fashion. This architectural pattern brings big advantages
in terms of scalability: each tier of an n-tier application can operate
at its own maximum speed, since it is not waiting on any other tier
to synchronously respond to a request. Each tier communicates with
the message queue as a broker and either pushes or pulls messages
at whatever pace it can handle. Another advantage of this architec
ture is that not every tier has to be online at the same time for the
application to operate. If one or more tiers become unavailable for
some reason, either planned or not, the other tiers can still commu
Figure 1-3. Service Bus relay facilitating queue messages across a fire
wall
In the topics mode, you can use the Service Bus to create topics to
which consumer services can subscribe to receive messages. Each
time you push a message into a Service Bus topic, every subscriber
receives the message as a broadcast (see Figure 1-4). This makes the
communication one-to-many, as opposed to one-to-one.
SQL database
Even though NoSQL database systems are exciting and new, rela
tional database systems are still important and relevant for most
applications. Azure SQL Database is a managed database service
that supports the majority of SQL Server functionality, including
database programmability, SQL Server Management Studio, and the
familiar T-SQL operations you are used to. Unlike SQL Server on a
virtual machine, however, Azure SQL Database supports elastic
scale, geo-replication, and point-in-time restore; it is automatically
replicated, security patched, and has a guaranteed service-level
agreement (SLA). You can scale your throughput up and down as
needed to balance cost and performance.
Azure SQL Database was formerly known as SQL Azure and was
initially offered as a more basic SQL service with price based on
database size. The original implementation of this service lacked
some common features, did not perform as well as many expected,
and got a bad reputation in some circles. Things have changed,
however, and with the guaranteed throughput units and service tiers
now available this service is well worth your consideration and pro
duction use. In fact, having all of the database management tasks
taken care of as a service makes this an attractive option for nearly
any application.
Database throughput units (DTUs) are based on a blended measure
of CPU, memory, reads, and writes. These units are a bit hard to
describe, but they are accurately relative to one another: 10 units
will offer exactly twice the performance of 5 units. See the following
two tables for more details on real-world performance.
Redis cache
Azure Redis Cache is a service used to deliver high-throughput,
low-latency data your applications need to be able to read very
quickly without waiting for a potentially long-running operation or
request. Like Hadoop, Redis is not a Microsoft product, but they are
hosting and managing it so you can consume it as a scalable service.
Online Store
Imagine that you work for a retailer who generates a significant
amount of revenue through online sales. Imagine also that this
retailer has been around for long enough that it already has an
established web architecture that runs in a private data center. This
retailer has decided that it wants to move to a hosted platform so
that it no longer has any data center responsibilities and it can focus
on its core business. How do you replatform this web application
into Microsoft Azure? Lets first identify some requirements for this
system:
Backend tier. For the backend tier, I recommend that you use the
other type of cloud service template: worker roles. Worker roles are
managed by Azure the same as web roles, but worker roles have two
important differences. First, worker roles do not come configured
with IIS. You are allowed to do whatever you want with a worker
role, so you could configure your own web server and use it to host
web applications, but Azure would not manage this for you. Sec
ondly, since worker roles do not have an Azure-managed web
server, Azure can only tell if your application has crashed by watch
ing for events in the role entry point class (WorkerRole.cs for .NET
projects). For a worker role, you need to start your actual applica
tion inside of the role entry points Run() method and have it run in
Note how the application feeds all data writes into Queue Storage,
where the worker roles can handle keeping all of the data stores
synchronized. Data reads flow in the other direction, from the data
bases up to the web roles, and do not involve the backend worker
roles. If you prefer to wrap your data access in a service layer, I rec
ommend creating a different set of worker roles dedicated to that
function.
One thing I didnt mention before was how to store the shopping
cart data for each customer as they shop around the site. This is
something you may have stored in session state in the past, or
maybe in a relational database, but neither of those options fits the
Single-Page Application
In the last example we talked about an architecture that fits a large
ecommerce business with an established code base that needs some
custom services on its web servers. In that scenario, the best transi
tion to the cloud consisted of re-platforming from virtual machines
in private data centers to cloud services in Microsoft Azure. That
transition can still utilize a large portion of the companys existing
application code in those cloud services and only change what is
necessary to make the new data layer work. But what if you arent
trying to lift a large existing system into the cloud? What if you are
writing a new application and you want to write it as a single-page
application (SPA)? In this case, you have some different options and
I would recommend a different pattern.
Single-page applications are different from traditional web applica
tions in that they rely on client-side JavaScript for nearly all of the
application logic and behavior; the server-side component is nor
mally limited to the duties of authentication, authorization, and an
API for create/read/update/delete (CRUD) operations. Conse
quently, the server is responsible for returning only a single, basic
index HTML page that fires off all of the JavaScript. All of the navi
gation within the app is actually handled through JavaScript and
markup templates; the server does not serve the pages since they are
generated dynamically on the client (hence the term single page,
from the servers point of view). An application like this has at least
three important requirements for us to consider:
It needs a web API that can serve as the backend to the Java
Script frontend.
Hosting options
With this type of application, a cloud service is unnecessary (espe
cially at first) since you do not need any access to the operating sys
tem and you dont need to install any custom software on the under
lying virtual machine host. In this case, you can use Azure App Ser
vice with Web Apps. It is a friendlier, more feature-rich and lower-
cost hosting platform that is optimized for websites and HTTP APIs
when all you need is an FTP endpoint for management and deploy
ment and you want to deploy through source control. This hosting
option also makes it very easy to add new features to your site, such
as an API or mobile app, all under the same account and service tier.
Dont forget that one of the biggest benefits of the cloud is being able
to right-size your consumption. Only use what you need, and scale
up or down to match the cost to your usage requirements.
Table 1-15 will help you decide when to pick Cloud Services or App
Service Web Apps based on the features you need.
Table 1-15. When to choose Cloud Services or App Service with Web Apps
Cloud Services App Service Web Apps
Need unhindered access to OS Only need FTP
Need to scale indefinitely Maximum 50 dedicated VMs
Manual/scripted deployment OK Auto continuous deployment
Always dedicated VMs Free/multitenant tier options
Always custom code Prebuilt sites available
.NET/Java/Node.js/PHP/Python/Ruby .NET/Java/Node.js/PHP/Python
Hosting options
Since we have already covered the web hosting options of Cloud
Services and App Service Web Apps, we can just discuss which of
them is more appropriate here. Honestly, you could go either way,
but I would suggest starting with Azure App Service since it offers
more productivity features, has a built-in framework for adding
mobile apps, and can easily be migrated to Cloud Services if you
ever need to. The only reason I would choose Cloud Services ini
tially is if you need to install additional software on the servers, such
You will normally be querying for all of the posts for a single
user rather than joining data from multiple users at the same
time.
Each user will potentially generate hundreds or thousands of
posts and the list will continue to grow over time, so you need a
database that can easily retrieve all of these posts for a user very
efficiently.
It is all right if there is a short delay between the time when one
user posts a message and when other users see it.
Based on these criteria, I think the ideal database is a column-family
database that organizes users as rows and user posts as columns so
they can be retrieved together at peak efficiency. This database type
supports horizontal scaling and eventual consistency, so it can sup
port a write-intensive workload while still serving fast reads.
To implement a column-family database in Microsoft Azure, your
best option is to create some Azure virtual machine instances and
install the database software yourself. A great choice here is HBase,
as discussed in Hadoop and column-family databases on page 25.
I should mention that using a column-family database requires a lot
of manual maintenance and customization. You may prefer to use a
database like DocumentDB to store your data instead. DocumentDB
is a very high-speed and scalable service that can work for this appli
cations type of data so long as you carefully design your data entities
and queries. Whether you implement a column-family database or a
document database, you will need to work out your data querying
strategy carefully to get the correct level of performance.
Hosting Options
Mobile apps are used in different ways from web applications. A
users mobile device is always on; it can send alerts and notifications
based on individual preferences and location; and it can drive reve
nue in unique ways. You could use Microsoft Azure to build your
own custom backend services, databases, messaging functions, and
analytics, but Azure provides services to cover all of these needs in a
complete package, and I recommend taking advantage of this if for
Mobile Engagement
Microsoft Azure also has a new service called Mobile Engagement
that provides you with an API you can integrate into your mobile
apps to capture some specific big data analytics and to provide some
targeted push notification and communication options. This set of
tools is designed to increase the return on your investment by help
ing you get to know your users and their habits better so you can
better market services to them, engage with them, and retain them
over time, which all lead to generating more revenue.