Glinkot
Glinkot

Reputation: 2964

Simplest way to persist data in Azure - recommended options?

I'm giving Azure a go with MVC4, and have the simplest data storage requirement I can think of. Each time my controller is hit, I want to record the datetime and a couple of other details to some location. There will most likely be only a few thousand hits at most per month. Then I'd like to view a page telling me how many hits (rows appended) there are.

Writing to a text file in the folder with Server.MapPath... gives permission errors and seems not to be possible due to the distributed nature of it. Getting a whole SQL instance is $10 a month or so. Using the table or blob storage sounds hopeful, but setting up the service and learning to use those seems nowhere near as simple as just a basic file or DB.

Any thoughts would be appreciated.

Upvotes: 0

Views: 207

Answers (2)

David Makogon
David Makogon

Reputation: 71030

Augmenting @Eoin's answer a bit: When using table storage, tables are segmented into partitions, based on the partition key you specify. Within a partition, you can either search for a specific row (via row key) or you can scan the partition for a group of rows. Exact-match searching is very, very fast. Partition-scanning (or table-scanning) can take a while, especially with large quantities of data.

In your case, you want a count of rows (entities). Storing your rows seems pretty straightforward, but how will you tally up a count? By day? By month? By year? It may be worth aligning your partitions to a day or month to make counting quicker (there's no function that returns number of rows in a table or partition - you'd end up querying them).

One trick is to keep an accumulated value in another table, each time you write a specific entity. This would be very fast:

  • Write entity (similar to what Eoin illustratecd)
  • Read row from Counts table corresponding to the type of row you wrote
  • Increment and write value back

Now you have a very fast way to retrieve counts at any given time. You could have counts for individual days, specific months, whatever you choose. And for this, you could have the specific date as your partition key, giving you very fast access to the correct entity holding the accumulated count.

Upvotes: 2

Eoin Campbell
Eoin Campbell

Reputation: 44268

Use TableStorage. For all intents and purposes it's free (it'll be pennies per month for that amount of volume a fraction of your web roles anyway).

As for how complicated you think it is, it's really not. Have a look at this article to get going. http://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/#create-table

//Create a class to hold your data
public class MyLogEntity : TableEntity
{
    public CustomerEntity(int id, DateTime when)
    {
        this.PartitionKey = when;
        this.RowKey = id;
    }

    public MyLogEntity () { }

    public string OtherProperty { get; set; }
}


//Connect to TableStorage
var connstr = CloudConfigurationManager.GetSetting("StorageConnectionString") //Config File
var storageAccount = CloudStorageAccount.Parse(connstr);

// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Create the table if it doesn't exist.
var table = tableClient.GetTableReference("MyLog");
table.CreateIfNotExists();


var e= new MyLogEntity (%SOMEID%, %SOMEDATETIME%);
e.OtherValue = "Some Other Value";

// Create the TableOperation that inserts the customer entity.
var insertOperation = TableOperation.Insert(customer1);

// Execute the insert operation.
table.Execute(insertOperation);

Upvotes: 2

Related Questions