ASP.Net Core saves me $4 a month!

Background

It has been more than 2 years since my website www.codingsoldier.com was conceived. It was created in ASP.Net MVC. Since then the site has evolved a lot technology wise. The website is used as a repository of my studies, as a means to publish my technical thoughts and also a place where I can try out the technologies/areas that I don’t usually get to do as part of my regular office work like full time DevOps or migrating sites to newer technologies without having to convince the management.

Technology Migration History

  • Initially created in ASP.Net MVC with SQL server as the database. Site was hosted in IIS in a windows server.
  • It was migrated to Node.Js + Express and later to Node.js + React. Database was MongoDb and site hosted in Node in a windows server.
  • Of late, the site has been migrated to ASP.Net core with SQLite as the Database. Site runs on a Linux instance, with Kestrel as the web server and Nginx as the Reverse Proxy.

Migrating to ASP.Net Core

ASP.Net core has been around for quite some time now and it too has evolved a lot. Migrating from ASP.Net MVC to Core was easy in some aspects, but I did face difficulties in some other areas. Entity Framework was replaced with Entity Framework core and the database provider was changed from SQL server to SQLite. 

ASP.Net core and EF core are very lightweight compared to their predecessors. Only thing is that we need to learn new commands, since the interaction is mostly command line based. But that is not a downside at all. Once we get used to it, it is easy and straightforward and importantly it is fast. 

Major problem I faced during migration was than one of the Nuget packages(Pagination package) working in ASP.Net MVC was not available with ASP.Net core. Many of the nuget packages are still evolving to target the Dot Net Standard for Core 2.0. So, I had to create the functionality for paging in the Views, on my own. Thankfully, this paved way for me to make use of TagHelpers, which is also new in ASP.Net core. 

EF core supports many database providers. I chose SQLite since it is so light weight and I can run the SQL commands against it, even though in command line. Integrating this part was not much difficult. We don’t have long EDMX files as we had with Entity Framework. We create the models first in our codebase, create the migrations and then update the database with the migrations we created.

Kestrel is the web server that ASP.Net core gives out of the box. Kestrel enables ASP.Net core to be run in multi environments. Microsoft recommends, Kestrel to be run behind a reverse proxy -  Nginx in case of Linux or IIS in case of a windows server.

Choice of WebServer

Below are the reasons I went with Linux as my choice of after migrating to ASP.Net core:
  • I have always worked with IIS in Windows server. Now that, with ASP.Net Core, Microsoft supports Cross Platforms it was time to try out Linux server and have Linux under my skillset belt.
  • I had hosted the site in the cheapest instance in Amazon EC2: t2.nano Windows instance. But t2.nano Linux instance is even cheaper. More on the price details to follow.
  • I have heard that Linux servers are fast compared to Windows server. Time to try that out as well.
  • I have been hearing about Nginx as a Reverse Proxy for some time. How do I configure that? Wouldn’t it be cool to configure a Reverse Proxy on my own ? Yes, of course.

Deploying to Linux Instance

After verifying that the website was launching fine in my local, it was time to publish the changes to EC2. I spun up a new Linux instance in EC2. 
There are 2 ways to publish in Core:
  1. Self Contained Deployment(SCD) - website files and framework files, both will be part of the published package.
  2. Framework Dependent Deployment(FDD) - only the website files will be part of the package. Other files needed for the website to run are expected to be in the server where the site is deployed to.

Since I wanted to learn more stuff on the Linux side, I thought I will install the runtime myself on the Linux server and hence went ahead with FDD. 

I logged into the shell using Putty. Installed the ASP.Net core runtime. Copied the files from my Windows machine to the terminal. And finally started up the website in the Kestrel server. I was able to access the website from outside. Cool!

Time to level up. I installed Nginx, did more configurations and finally was able to route the traffic to the Kestrel server. 

For me, it was the first time with Linux server. I faced lot of hiccups and took lot of time to have the setup done fully. But the learning, pride and happiness, I had after the setup is beyond words. 
I also did some benchmarking(using WebPageTest) of the website performance. I also deployed the same website to my existing Windows instance. Compared the performance of Linux/Windows and Linux tipped Windows in the performance. After the performance comparison, I terminated my Windows instance.

Performance Comparison metrics:


Cost Saving

And finally to the post subject, on how the new setup saves me 4$ a month.To demonstrate the saving, I’m going to show some numbers for AWS instances.

On an average, on-demand hours for my EC2 instance is 730 hours a month.
Windows On Demand cost for t2.nano instance is: $0.008100 hourly.
So total cost for Windows EC2 is 730 * 0.008100 = $5.913.
Linux On Demand cost for t2.nano instance is: $0.005800 hourly.
So total cost for Linux EC2 is 730 * 0.005800 = $4.234

EBS(Elastic Block Storage) size for Windows Instance is 30GB and Linux instance is 8GB.
EBS costs $0.10 per GB per month.
So total cost for EBS for Windows is $3 and for Linux is $0.8.

So total savings for Linux instance is: (5.913+3) - (4.234+0.8) = $3.879


Conclusion

The migration to ASP.Net Core has been great. It is such a lightweight environment. It is cross platform. It saves you money and gives you higher performance. And had you not worked in Linux environment before, it is an excellent learning opportunity. So without a doubt, go for it.
Next will be packaging my website + Nginx into a Docker image.






Comments

Post a Comment

Popular posts from this blog

My First Full Marathon

My Second Full Marathon