Monday, August 24, 2009

Load balancing issues for ASP.NET applications

Rencently, our company started to change our hosting to adapt cluster servers and Mirrored SQL Servers. We just used DFS replication, we did not use a load balancer server.

There are a couple of issues we need to address because of the change:

  • when put a object in session, make sure it is Serializable, so later in the clustered environment, if you manage session in sql server, it can be stored in sql server
  • We were using a third party control for uploading images called I-Load which created some issues with temp image folder, luckily we fixed it. So when buying third-party control like ILoad, make sure it supports web farm
  • We were using a third party control called DynamicControlsPlaceholder which does not work in cluster environment until we modified it a bit to make it work
  • view state needs to have shared machinekey set up in web.config for web farm environment (in iis7 use sharekey section to share key with other web servers in the farm, then use iis7 to generate unique machinekey for every application in the iis, so that applications do not interrupt with each other)
  • Cache & Application variable
    • The cached object is stored in process, which means it doesn’t persist if the application domain is restarted and it can’t be shared between computers in a web farm.
    • Once a worker process recycles, the entire Cache is lost because it was within this process.
    • Microsoft has tried to fix this a bit with SQL Server cache invalidation (ASP.NET Cache SqlCacheDependency), but it could have potential performance issue and according to Scott Watermasysk he has not seen many be successful with this approach.
    • There are a couple third party tools to fix this: Memcached ScaleOut NCache SharedCache
    • The good news is Microsoft is making some strides here. They are working on an out of processing caching component called Velocity.
    • We have to get rid of cache and application variables to address the problem

References:

ASP.Net Caching Is Too Easy

Friday, August 14, 2009

Delete files older than certain period

Public Shared Sub ClearTemporaryFiles(ByVal folderPath As String, ByVal maxLifeTime As Integer)

Dim timeLimit As DateTime = DateTime.Now.AddMinutes(0 - maxLifeTime)

Dim filePaths() As String = System.IO.Directory.GetFiles(folderPath, "*.jpg")

For i As Integer = 0 To filePaths.Length - 1

Dim filePath As String = filePaths(i)

If (System.IO.File.GetLastWriteTime(filePath) < timeLimit) Then

Try

System.IO.File.Delete(filePath)

Catch

End Try

End If

Next

End Sub