martes, 27 de enero de 2015

Move twilio recordings to azure and save on your twilio monthly bill

Twilio is an amazing cloud communication service which excels in a lot of things, however their recording storage is quite expensive. They charge you by minute stored per month, which means that if you have a recording of 10 mins, you will be charged for those 10 mins each month as long as you keep the recording on twilio's servers. Each account gets 10,000 free minutes per month and after that they charge you $0.0005 per minute per month (details can be seen in Although this seems pretty low, the minutes start accumulating on your account, because after 3 months, you will be paying, for all the minutes recorded during the first month, plus the ones from the second and the third months too. If you have a high traffic callcenter, you can easily reach millions on minutes in a few months.

I checked multiple recordings, and in average, 1 minute equals 1MB in the recording file. And the minute is their unit, so no matter if your recording is 90 seconds long they charge you 2 minutes for that recording, although they only have to save 1.5MB in their datacenters.

Azure pricing is per GB, and at the time of this writing, its $0.048 per GB using geographically redundancy (6 copies of each file are stored).

Lets compare the costs of storing recordings in twilio versus storing them in azure in a real life scenario which is actually the reason why I did this in the first place. Currently, I'm the lead developer on a call center system that increased the calls that handles 3500 calls per day, with an average duration of 10 mins per call. We noticed that we were paying a lot of money for recording storage and this is the analysis that we did:

Twilio: (3500 [calls per day] * 10 [avg mins per call] * 30 [days in a month] - 10000 [free storage mins]) * $0.0005 [storage cost per min] = $520 per month

Azure: 3500 [calls per day] * 10 [avg mins per call] * 30 [days in a month] / 1024 [MB per GB] * $0.048 [storage cost per GB] = $49 per month

Note: The azure pricing calculation does not include the bandwidth cost of uploading the recordings, but for this same scenario its around $10 extra per month only one time.

So, just by moving the calls to azure we were able to save $471 per each month's recordings storage. And this is accumulable, because if you have been running the call center for 3 months, at the end of the 3rd month, you needed to store the recordings of the first month for the 3 months, plus the recordings of the second month for 2 months, plus the recording of the third month for 1 month. So after 3 months we were able to save $471 * (3 + 2 + 1) = $2826 saved in 3 months
Conclusion, lets the communications provider (twilio) for the communications parts of our solution, and lets use a cloud storage provider for storing the recordings (azure).

I have a github repository with the code needed to move your recordings to azure and start saving on your twilio's monthly bill right away:

This repository contains 2 projects:

A) MoveRecordingsToAzure: This is a console program that moves recordings from your twilio account to your azure account, including metadata of the recording like the duration and when it was created. This program has a configuration parameter that lets you tell the program to keep the recordings of the last XX days in twilio. If you want to move every recording to azure as soon as its generated, you can set it to 0. In order to move the recordings automatically, you would need to set it up as a windows scheduled task.

B) TwilioRecordings: Is an MVC website with just 1 available url (/recordings/{sid}.wav). This url allows you to stream files from twilio and azure transparently. If you have the recording's SID in your db, you can use this url and you dont have to worry if your recording was already moved to azure or if its still in twilio. This website checks if the audio is still in twilio and lets you stream from there (delegating the streaming details to twilio), and if it is not it grabs it from the corresponding azure blob and handles the streaming details by its own (allowing file seeking).

viernes, 9 de enero de 2015

Alias for SQL Server named instance

In this entry I'll describe a simple solution for a common problem that I saw while working on development teams with multiple coders.

Every time you install SQL Server, you might give the instance that you are installing a new name, or not and SQL Server will use ".". The problem is that when you start working with other coders on the same project, their instances names probably wont match yours and this generate problems in the project's connection strings.  This happened to me multiple times and every time I did a pull from our git repository I had fix the connection string.

Named instances aliases are a great solutions for this problem! Basically the only thing that you need to do is to define an alias with your coworkers and set it up. If you work with different groups of people in different projects, you can adds new aliases pointing to your SQL Server instance, and you can forget about fixing connection strings.

Lets see how to do this:

1) Open "Sql Server Configuration  Manager"

2) Open the network configuration and enable the TCP/IP protocol if you have it disabled

Note: I have 2 sql server instances, Im doing this for my "SQLSERVER2008R2" instance

3) Open the TCP/IP protocol properties and enable the "Listen All" properties so accept connetion on any ip that your computer has.

4) Grab the port where your instance will be accepting the TCP/IP connections. For this, right to the bottom you need to set "0" as the value for "TCP Dynamic Ports" and restart your sql instance. Once you do this, you will get a port number assigned and it will be reserved for this instance in the future. Copy the port number as you'll need it for the next step

Here I already have the port assigned
5) Now do a right click on the "Aliases" section and select "New Alias". You will need to specify, the name for the alias (this is what you will use in your connection strings, we used "SQLDev" with my team), the port number that you grabbed from step 4, the protocol (TCP/IP) and the server (which is the machine name, you can use ".", and the named instance if your instance is named if not it works with ".").

6) Use your Alias! 

This is how to use it with SQL Server Management Studio
You can use this alias in any connection string. Enjoy!