Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

Parallel.ForEach vs Task.Factory.StartNew

5/11/2017

Comments

 
Recently in my project we had a performance issue, so our teams were on our toe to decrease application start-up. While analysing the code I came across a “for loop” which was creating the problem. Collection was very big and secondly it was performing some long running task. So I had two option two increase the performance:
  • Using Task and
  • Using Parallel.ForEach
Developer might be thinking both might be performing exactly the same and both might be using the ThreadPool. But in reality it works very differently though results will be exactly the same.
Using Task.Factory.StartNew

    
So if you see the above code, I am looping the collection and scheduling a single Task per item in the collection. This particular code will introduce far more overhead especially if the collection is very large and in that case overall runtimes is very slower. Also above code will run in asynchronous manner
Using Parallel.ForEach

    
Now if you see second block of code, it’s a simple Parallel.ForEach call. Internally it uses a Partitioner<T> to distribute your collection into work items. But it won’t create one task per item rather it will create a batch which will lower the overhead. As whole block Parallel.ForEach runs in synchronous manner i.e. block of code after Parallel.ForEach will run only after it's completed.

And in case if you want to run the Parallel.ForEach in asynchronous manner then we can modify the code as shown below. By implementing as shown below you are collaborating the power of Async plus the power of Parallel.ForEach.

Including both Parallel.ForEach and Task.Factory.StartNew

    
Comments

Exploring Azure Functions

5/10/2017

Comments

 
Picture
Azure functions is the idea of events and code, developers will provide the Azure with a function or code and you till what event should trigger that function.
  • Scheduler: You are actually telling Azure function to run the particular code at a particular time or maybe. So in this case scheduler is the event which triggers the Azure function. This can used at many places, suppose at the end of the trading day you need clear all the database or want to run the batch.
  • Process when a new data comes in the storage. This might be particularly helpful when a particular message or data received then we need to send the email.
  • Lastly, its web hook, whenever someone calls a specific URL, then you want your function to run and example might be whenever user comments on the issue created by GitHub then I need to run the azure function, or need to handle the webhook callback from the payment provider whenever anything is sold online from the e-commerce website.
All the above discussed scenario can be easily achieved using IaaS i.e. Infrastructure as a service i.e Create my own virtual machine and deploy everything from windows service to web server as per my requirement. But in that case all the responsibility is with the developer to handle patching and maintaining, scaling etc. So it will be really great if all that work is actually done by Microsoft himself and developer concentrates on just coding!!!
Azure Cloud Services: Developer can also use web roles or Worker roles. Web roles are used for hosting websites and web APIs, and Worker roles are designed for your background tasks, like processing queues and other batch work. So in cloud services all the environment related work is taken care by Microsoft and this model looks perfect for developers
​Why do we need Azure Functions?
  • Azure functions provides us with the simplified programming model. All developer needs to do is write the code that responds to the event, whether it’s an HTTP request or Queue message
  • No boilerplate of how the event is connected or raised, all that is handled by the framework, so it’s very light weight.
  • Focus on business requirements
  • New pricing model: With cloud services and IaaS user need to have at least one server and user need to pay for that server even though if use is minimal. So now with Azure Functions, developer pays only when its code is running. So suppose you are listening to the queue and no message arrives than you pay nothing.
  • Azure function will automatically scale the servers running your functions to meet the demand, so if there are no demands there might be no servers actively running the code but when needed framework will quickly spin the when required.
Benefits:
  • Rapid and simple development
    • Code within the portal
    • Eliminate the boilerplate
  • Developer will get all the power of Azure Web Apps since Azure Function is developed on top of this framework: CI, Kudu, Easy Auth, Certs etc
  • Highly cost effective pricing and it makes overall very cheap.
  • And the best is we don’t have to maintain any servers. So effectively in big organisation we don’t need to have any Environment team for setting up the server and maintaining it.
What is Serverless?
Server less doesn’t means there are no server involved, of course servers are involved but it’s abstract from the users/developers. But we delegate all the management of the server to Azure instead of us doing it. So we can purely focus on business requirement.
All the big enterprise application relies on the third party platform or backend. For example, for database we might use Azure DocumentDb and for authentication we might use some other third party platform. And with growing number of third party platform service, we might need to write our own custom backend code as per our requirement and that’s were Azure Functions comes to help us. So you can just tell the Azure function which events you need to respond to and you can write your own custom code. So with this comes a new term “Function as a Service (FaaS)”.

Comments

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot