Wednesday, July 16, 2014

Part 14 : Windows Azure Web Sites - Scaling

In this tutorial we will see how you scale your website up or down depending on the needs.

Scalability is the ability of a system to handle a growing amount of work in a capable manner or its ability to be enlarged to accommodate that growth

When you start developing a new web application, or when you want to test your web application, or try out the Azure WAWS, you might want to start free and small. All web sites can deploy in a free hosting mode. Then as the traffic to your website increases and as the expectations for performance and availability increases, you can scale out to meet the needs of your business.

Also if your website is already in production and you have some period of time where you expect the hits go high, for example gift shopping website during the Christmas season, you can scale up for that period of time and then scale down again later.  Consider the situation where you have your own servers in datacenter, you have to provisions the servers for the peak load and most of the times the servers will be lying ideally but incurs cost to you. With cloud, this is great feature to scale up and down depending on your needs and can save lots of money.

By adding and removing role instances to your Windows Azure application while it is running, you can balance the performance of the application against its running costs. You can add new instances when demand is high, and remove instances when you no longer need them in order to reduce running costs.

For increased performance and throughput for your websites on Microsoft Azure, you can use the Azure Management Portal to scale your Web Hosting Plan mode from Free to Shared, Basic, or Standard.

Scaling up on Azure Websites involves two related actions: changing your Web Hosting Plan mode to a higher level of service, and configuring certain settings after you have switched to the higher level of service.

Each scale operation happens quickly – typically within seconds – and does not require changes to your site’s code, nor a redeployment of your site. Scale operation have no impact on existing user sessions.
You can also scale up or down programmatically by using the Windows Azure Management API.

Before moving forward let's see what exactly is meaning of scale up and scale out.

Scale Up
Also know as scale "vertically". You can scale up by increasing the processing power by adding more processors and RAM or buying a bigger, multi-core robust server.
Pros : Less licensing cost, less power consumption than running multiple servers, less challenging to implement.
Cons : If single server fails, it brings down the whole application, price.

Scale up operations can be performed on any site without worrying about the implications of multi-instances data consistency.
Windows Azure offer servers for web sites in multiple instance sizes of small, medium and large. Scaling up in Windows azure for web sites can be achieved by changing the instance size of the server, e.g. from small to medium.








Scale Out
Also known as scale "horizontally". You can scale out by adding more servers and adding a load-balancer to distribute the demand between them.
Pros : cheaper than scaling vertically, easier to run fault tolerance
Cons : More licensing fees, higher utility cost, complex to implement.

To scale out your site in windows azure, you need to just increase the instance count for that web site. This is very easy and we see it in practical shortly.  The big advantage with Windows Azure is that when you scale out a web site in Windows Azure Web Sites there is no need to configure load balancing separately since this is already provided by the platform.

To benefit from scale OUT operations your site must be designed for concurrency. You should plan for the shared state so that request from particular user can be handled by any server under the load-balancer. Or use sticky session. Sticky session means that when a request comes into a site from a client all further requests go to the same server initial client request accessed. Load balancer will usually handle this.

One thing to understand regarding scale out is that even they can increase the total amount of traffic capacity that your site can handle, it does not always improve performance in the way that scaling up does.

Scale up and scale out operations can be combined for a website to provide hybrid scaling. The same contentions about multi-instance sites would apply to this scenario.

Let's start with a free website and we will scale it using Windows Azure Management Portal.

Scaling of Free website to Shared or Basic mode
1. Open Management portal and create a website in free mode (if not already created). Assume that your      website is currently in development mode and this is the reason we choose free mode.

 2. In the website listing page, click on the site and then go to "Scale" tab.












 3. Currently the website mode is free. Now our website goes into a test phase where business users and stakeholders are going to test the website. So we are going to scale it to Shared mode. The Web Hosting Plan Sites section shows a short list of sites in the current plan. All sites in the current plan will be changed to the Web Hosting Plan Mode that you select. Click on the Shared mode. Under capacity let the instance count be 1 instance and click on save. Note that in free mode you cannot change the instance count.
So now effectively we have scaled up the website to shared mode. 

4. Now testing is done and accepted by business users. We want to roll out the pilot site for several hundred users and we want  the website to available for most of the time. What we can do is to let the site remain in shared mode but increase the instance count. Again go to website and then scale tab and then in the instance count make it 3. You can scale out to maximum of 6 instances. Windows azure will automatically configure load balancer for you. Notice that in shared mode you can increase the instance count but not the instance size, because it is fixed in shared mode. So you can scale out but not scale up.  Also note that while changing the web hosting plan mode will change all the website under that plan , changing instance count will not affect the instance count of other sites in the same web hosting plan mode. 
Still confused :) try it out yourself and you will understand.


 5. Now our pilot phase is well received by audience and we want to go into production. But we don't want to go into production in shared mode. So we want to change it to Basic mode so that we will have dedicated servers serving only our website. Go to the scale tab again and this time choose the web hosting plan mode as Basic. You notice that there will be one more configuration item available "instance size". Now since we have our own reserved server, we can also choose the right fit size that we want. Since I just launched my website I want to start with medium size server and two of them to support availability. This is the example of hybrid scaling where we both scale up and scale out. 


Also notice that changing the instance count or size will change for all the sites in web hosting plan, which is little different that shared mode as described above.

6. Now suddenly the management decided that there are some serious business flaws in the current website and they want to fix it but at the same time they don't want to incur cost during that time. Easy, just go to scale tab again and choose the web hosting plan mode as "free" and start your development.

So we have seen how easy it is to scale up or down as required in Windows Azure and these changes take only seconds to apply.
Scaling Website to Standard Mode
Before going into this topic lets discuss Auto-scaling briefly. One of the key benefits that the Windows Azure technology platform delivers is the ability to rapidly scale your application in the cloud in response to changes in demand. If you rely on manual interventions to scale your application, you may not always achieve the optimal balance between costs and performance; an operator may respond late, or underestimate the number of role instances that you need to maintain throughput. An autoscaling solution reduces the amount of manual work involved in dynamically scaling an application. It can do this in two different ways: either preemptively by setting constraints on the number of role instances based on a timetable, or reactively by adjusting the number of role instances in response to some counter(s) or measurement(s) that you can collect from your application or from the Windows Azure environment. We will see the autoscaling feature in this topic.

NOTE: Before switching a Web Hosting Plan to Standard mode, you should remove spending caps in place for your Microsoft Azure Websites subscription. Otherwise, you risk your site becoming unavailable if you reach your caps before the billing period ends. 

1. To scale to Standard, follow the same initial steps as when scaling to Shared or Basic, and then choose Standard for Web Hosting Plan Mode. As before, the Web Hosting Plan Sites section shows a short list of sites in the current plan. In this case, all sites in the current plan will be changed to Standard mode.

2. Selecting Standard expands the Capacity section to reveal the Instance Size and Instance Count options, which are also available in Basic mode. The Edit Scale Settings for Schedule and Scale by Metric options are available only in Standard mode.

3.  Configure the Instance Size and instance count as we have seen before.

4.  If you want to automatically scale (autoscale) resources based on daytime versus nighttime, weekday versus weekend, and/or specific dates and times, choose Set up schedule times in the Edit Scale Settings for Schedule option.


5. A new dialog box called "Set up schedule times" will open.  The Set up schedule times dialog provides a number of useful configuration choices. These options let you define different scale settings during weekdays or weekends.

First checkbox is "Different scale settings for day and night". This creates two schedules. The first schedule runs from the start of the day to the end of the day. The second schedule runs from the end of one day to the start of the next day, and uses the options below to define times and the time zone.


Second checkbox is "Different scale settings for weekdays and weekends". This creates a separate profile that starts on Friday evening and ends on Monday morning. Use the options below to define start times, end times, days, and time zone.

Under Recurring Schedules, select Differing scale between Day and Night and/or Differing Scale between Weekday and Weekend to scale resources based on separate daytime and nighttime schedules and/or separate weekday and weekend schedules. For the purposes of this feature, the weekend starts at the end of day Friday (8:00 PM by default), and ends at the beginning of the day on Monday (8:00 AM by default). The weekend profile uses the same day start and end that you will define in the Time setting. 


Under Time, choose a start and end time for the day in half-hour increments, and a time zone. By default, the day starts at 8:00 AM and ends at 8:00 PM. Daylight Savings Time is respected for the time zone that you select.

Under Specific Dates, you can create one or more named time frames for which you want to scale resources. For example, you may want to provide additional resources for a sales or launch event during which you might have large peaks in web traffic. Please note that this will override your other scale settings.

After you have made your choices, click OK to close the Schedule Times dialog box.

6. The Edit Scale Settings for Schedule box now displays configurable schedules or events based on the changes you made. Select one of the recurring schedules or specific dates to configure it.


You can now use the Scale by Metric and the Instance Count features to fine tune the scaling of resources for each schedule that you choose.

7.  To dynamically adjust the number of instances that your website uses if its load changes, enable the Scale by Metric option by choosing CPU.


The graph shows the number of instances that have been used over the past week. You can use the graph to monitor scaling activity.

8. Scale by Metric modifies the Instance Count feature so that you can set the minimum and maximum number of virtual machines to be used for automatic scaling. Azure will never go above or below the limits that you set. This enable you to proactively set the number of role instances that your application can use; the minimum number of role instances helps you to meet your service level agreement (SLA) commitments, the maximum number of role instances helps you to control the running costs of your Windows Azure application.

9. Scale by Metric also enables the Target CPU option so that you can specify a target range for CPU usage. This range represents average CPU usage for your website. Windows Azure will add or remove Standard instances to keep your website in this range.

Note: When Scale by Metric is enabled, Microsoft Azure checks the CPU of your website once every five minutes and adds instances as needed at that point in time. If CPU usage is low, Microsoft Azure will remove instances once every two hours to ensure that your website remains performant.



No comments:

Post a Comment