Wednesday, 19 September 2012

Using Windows Server AppFabric Caching Service

AppFabric Caching Service is a distributed cache platform for in-memory caches spread across multiple systems, developed by Microsoft. It is one of Microsoft's AppFabric's Services. In its early stages of development, it was referred by a code name Velocity.

Some useful PowerShell commands:
Get-Cache [-HostName] [-PortNumber] Without any parameters, this will list information about all caches and regions on a cluster. Otherwise on a specified cache host.
Get-CacheHost [-HostName] [-PortNumber] Sees all of the cache services status in a cluster. If the parameters are used then of the host.
New-Cache
Creates a new named cache. It has a few other parameters that are optional.
Get-CacheAllowedClientAccounts Lists all accounts that have permission.
Grant-CacheAllowedClientAccount -Account "DOMAINNAME\username" Grants permission for an account.
Stop-CacheHost
Stops the specified cache service.
The exception message that you will see when it is down: ErrorCode:SubStatus:There is a temporary failure. Please retry later.
Start-CacheCluster Starts all cache services in the cluster.
Start-CacheHost
Starts the specified cache service.
Restart-CacheCluster Restarts all cache services in the cluster.


GUI admin tool
There is also a GUI admin tool for managing the cache service: http://mdcadmintool.codeplex.com


Coding guide
To use the cache service in the codes, we need to include Microsoft.ApplicationServer.Caching.Client and Microsoft.ApplicationServer.Caching.Core to the project's References.

Examples of how to add an object to cache using Add method, Put method or Item property:
http://msdn.microsoft.com/en-us/library/ee790846%28v=azure.10%29.aspx

A simple codes example of how to use the cache service:
DataCacheFactory factory = new DataCacheFactory();
DataCache cache = factory.GetCache("test");

string key = "key1";
string value = "value1";
cache.Put(key, value);

var value = cache.Get(key);

For a thorough example that uses various features of AppFabric caching service, look at the CacheAPISample project of this example projects from Microsoft. Alternatively download the codes from here and run it as a console application.


References:
http://en.wikipedia.org/wiki/AppFabric_Caching_Service

Cache Administration with Windows PowerShell (Windows Server AppFabric Caching)
http://msdn.microsoft.com/en-us/library/ff718177%28v=azure.10%29.aspx

Managing Security (Windows Server AppFabric Caching)
http://msdn.microsoft.com/en-us/library/ff921012%28v=azure.10%29.aspx

Friday, 7 September 2012

Some Notes about Parallel Programming in .NET 4 (and 4.5)

- Try to avoid writing to shared memory object such as static variables or class properties. Using locks in parallelization will suffer the performance.

- Only use thread-safe methods. Calling to non thread-safe methods in parallel programming can cause exceptions and undetected data loss.

- Most of the time, any regular loop that fulfills both of the above requirements can be converted into a parallel loop.

- Keep it simple and avoid over-parallelization (i.e.; unnecessary nested parallelization). When using parallelization there is an overhead cost to partition the work and merge the result back.

- If the parallel work is used to populate data into a collection, use one of the thread-safe collection types.
http://msdn.microsoft.com/en-us/library/dd997305.aspx

- Avoid any ordering operation if possible. By default PLINQ does not preserve the ordering sequence in the source.
http://msdn.microsoft.com/en-us/library/dd460677.aspx

- When needed to do further operation of a PLINQ result, prefer to use ForAll() method instead of Parallel.ForEach().
For example; use this
source.AsParallel()
      .Where( i => i.SomePredicate() )
      .ForAll( i => i.DoSomething() );
instead of
var filteredItems = source.AsParallel().Where( i => i.SomePredicate() );

Parallel.ForEach(filteredItems, item =>
{
    item.DoSomething();
});

References:
Potential Pitfalls with PLINQ
Parallelism in .NET – Part 2, Simple Imperative Data Parallelism
Parallelism in .NET – Part 6, Declarative Data Parallelism
Parallelism in .NET – Part 8, PLINQ’s ForAll Method