Websites and services on free and shared tiers of Microsoft Azure (and most other cloud providers) will idle your application after 20 minutes of inactivity to save resources. Unfortunately a “cold” application will take some time to spin up, resulting in a sub par experience for your visitors.
To keep a site “hot,” I’ll show you how to simulate requests to your application using Azure Mobile Services. The best part: this service will be free as long as you do not exceed the Azure pricing thresholds. Let’s get started.
MOBILE SERVICES
menu item.CREATE A NEW MOBILE SERVICE
on the screen that appears.URL
give your application a URL and make note of it. Select to create a free SQL database or, if you’ve already used your free database, create a new SQL instance. Under REGION
, select the option closest to the majority of your hosted services. Finally, under BACKEND
, choose JavaScript
and click the arrow button to move to the next screen.Ready
. Click on the name of your service and, on the next screen, select the DATA
tab. On the screen that appears, click ADD A TABLE
.URL
and, for the purpose of this tutorial, set the INSERT PERMISSION
field to Everyone
. You can change this later if your purposes require it. Click the check button to generate the table.COLUMNS
tab and then ADD COLUMN
.String
and, in the COLUMN NAME
field, enter url
.SET INDEX
menu item to optimize reads on our URL column.SCRIPT
tab, be sure the operation is set to INSERT
, and paste the code below. These functions do a bit of validation and ensure that you don’t insert duplicate URLs.function insert(item, user, request) {
var sitesTable = tables.getTable('url');
if (item.url.length > 100) {
request.respond(statusCodes.BAD_REQUEST, 'Url length must be under 100.');
} else {
sitesTable.where({
url: item.url
}).read({
success: function (results) {insertIfUnique(results, request); }
});
}
}
function insertIfUnique (existingItems, request) {
if (existingItems.length === 0) {
request.execute();
} else {
request.respond(statusCodes.BAD_REQUEST, 'Url already added.');
}
}
Now that the service is configured, we need to schedule a task that will request saved URLs in our table to keep websites and services in memory.
SCHEDULER
tab.CREATE
and provide a descriptive job name. You can leave the default schedule of 15 minutes unless your purposes require otherwise. Click the check button to generate the scheduler.ENABLE
to start the scheduler.SCRIPT
tab and replace the JavaScript with the code below. The function queries the table of URLs and uses Node to generate the HTTP request that will keep your website or service from going cold.function ping() {
var sitesTable = tables.getTable('url');
var req = require('request');
sitesTable.select('url')
.read({ success: function(results) {
results.forEach(function (siteObject) {
req.get({ url: siteObject.url});
});
}});
}
The only thing left to do is add the URL of any website or service you wish to keep in memory. Open your favorite web debugging proxy and create a POST request to the URL of the mobile service you created in the first section. Below is a sample request using the Fiddler proxy tool.
POST http://your-mobile-service.azure-mobile.net/tables/url HTTP/1.1
User-Agent Fiddler
Content-type: application/json
Host: your-mobile-service.azure-mobile.net
Content-Length: 35
{"url":"https://your-app-name.com"}