.Net下實現分佈式緩存同步的手段

來源: 沙加 - 博客園
  前不久,俺寫了篇文章談到了.Net下面的分佈式緩存的一些問題,並結合DNT裏面實現模式發表了一些自己的看法,近來通過學習相關的東西又有了一些新的體會, 寫在這裏作爲分佈式緩存列系文章的第二部分.

  其實對於性的擴展無非是Scale Up(向上擴展)或者是Scale Out(向外擴展), 微軟對此的看法是一個App的緩存最好是以它自己爲物理邊界進行讀寫,而不要放到別處去,這樣帶的問題可能有對象的序列化傳送,反序列化,網絡連接開銷,跨進程的開銷,對於高性能的站點來說都是不能忽視的問題.出於對這些因素的考慮微推薦的作法不是把多個應用放在一起在多臺Server佈署,而是將一個App劃分成若干個小的應用佈署到不同的服務器,下面的關係圖展示了這種關係, 這樣每種應用就可以獨立管理自己的緩存數據,但是對於象用戶數據這樣的通用數據仍然會存在多處.

  兩種解決方案爲了解決緩存同步的問題,有人想出瞭解決辦法, 可以參考這兩個地方,這個是MS的,這裏還有一個,先來看看Peter的這個吧, 主要思想就是把要放到緩存裏面的東西加上一層包裝CacheControlItem, 實現代碼請去原文出處下載.

  每臺機器都維護一個WebFarm裏面的成員服務器的列表,如果有新的服務器進來發現自己不在這個列表中則會通知其它的服務器把它加到這個名單裏面。添加緩存的這程是這樣, A服務器需要插入一個新的緩存值,它把這個項目插入到自己的緩存中,然後用它初始化一個CacheControlItem並指定它的緩存策略(優先級,緩存生存時間),設定它的動作,即添加,然後把這個結象序列化通過Web傳送到每一個成員服務器,接受到這些數據的服務器跟據這個對象的Action指令,把反序列化後的對象加入到自己的緩存裏面,這樣一個同步的過程就完成了,移除緩存對象的過程與之類似,只不過不需要傳送對象,只在包裝類裏面設定它的Action爲刪除就可以了. 當然,爲了提高性能,可以實現一個異步的偵聽線程專門用來響應緩存通知的請求. 總體上講這處辦法的效率比較低,在數據量較大的情況下可能會造成大量緩存數據的同步數據流。

  我們再來看看MS是怎麼做的,它的想法類似,只不過它不在WebFarm的成員服務器之間同步緩存,而只是保證每臺機器不要讀到已經失效的緩存數據,當緩存數據失效時(相關依賴數據被更新), 通過維護一個需要通知的服務器列表依次調用每臺服務器上的WebService,如果當前服務器上存在這鍵值的緩存則使它失效.

  這兩個老外寫的東西似乎都比較囉索,不過對初學者來說比較友好,可以一步步地知道這件事情的來龍去脈,理解會清楚更刻一些。

  Memcached到底有多快?看了這些如果還不滿意,那麼您可以試試Memcached它可以運行在Win32平臺下,在上篇文章中我們已經提到了這個東西,但是它在.Net的平臺下面究竟表現如何?是否能象在PHP平臺下面一樣地優秀,我們現在來做一個簡單的測試, 對比使用.Net自帶的Cache和Memcached兩種實現方式,看看差距究竟有多大,過程是這樣,分別生成10000個字符串對象並分別設定鍵值插入到緩存中,然後再取出來,看看花費的總時間. 服務器端:memcached-1.2.1-win32, 客戶端: memcacheddotnet_clientlib-1.1.5, 服務器端的使用也比較簡單,解壓文件之後在命令行下面輸入: c:/memcached -d install 先安裝服務, 接着 c:/memcached -d start就可以了,詳細使用方法見說明文件 -h 是查看幫助, 測試環境如下:

Memcached服務器 : Win2003 sp1, Framework 2.0,P4 D 3.4G, 768MB 內存, 千兆網卡.
Memcached客戶機 : Win2003 sp1, Framework 2.0,T2060, 1G內存( 沙加的神舟筆記本; ) ), 千兆網卡.
兩臺機器通過直連線相連.

.Net Cache單機測試 : P4 D 3.4G, 768MB 內存.

測試結果, 存取10000個條目的時間:

Memcached
Set(秒) 1.48 1.37 1.48 1.37 1.46
Get(秒) 2.42 2.42 2.42 2.43 2.42

HttpRuntime.Cache
Set(秒) 0.015 0.015 0.015 0.015 0.015
Get(秒)
0.015 0.015 0.015 0.015 0.015

  .Net內建緩存測試代碼HttpRuntime.Cache
 protected void Page_Load(object sender, EventArgs e)
 {
 int start = 200;
 int runs = 10000;

 string keyBase = "testKey";
 string obj = "This is a test of an object blah blah es, serialization does not seem to slow things down so much. The gzip compression is horrible horrible performance, so we only use it for very large objects. I have not done any heavy benchmarking recently";

 long begin = DateTime.Now.Ticks;
 for(int i = start; i < start+runs; i++)
 {
 HttpRuntime.Cache.Add(keyBase + i, obj,null,System.Web.Caching.Cache.NoAbsoluteExpiration,
 TimeSpan.FromMinutes(1),System.Web.Caching.CacheItemPriority.Normal,null);
}
 long end = DateTime.Now.Ticks;
 long time = end - begin;

 Response.Write(runs + " sets: " + new TimeSpan(time).ToString() + "ms<br />");
 begin = DateTime.Now.Ticks;
 int hits = 0;
 int misses = 0;
  for(int i = start; i < start+runs; i++)
 {
 string str = (string) HttpRuntime.Cache.Get(keyBase + i);
 if(str != null)
 ++hits;
 else
 ++misses;
 }
  end = DateTime.Now.Ticks;
 time = end - begin;

 Response.Write(runs + " gets: " + new TimeSpan(time).ToString() + "ms");
}
Memcached測試代碼Memcached
namespace Memcached.MemcachedBench
{
using System;
using System.Collections;
using Memcached.ClientLibrary;
  public class MemcachedBench
 {
  [STAThread]
 public static void Main(String[] args)
 {
 int runs = 100;
  int start = 200;
 if(args.Length > 1)
  {
 runs = int.Parse(args[0]);
 start = int.Parse(args[1]);
 }

 string[] serverlist = { "140.192.34.72:11211", "140.192.34.73:11211" };

 // initialize the pool for memcache servers
 SockIOPool pool = SockIOPool.GetInstance();
 pool.SetServers(serverlist);

  pool.InitConnections = 3;
 pool.MinConnections = 3;
 pool.MaxConnections = 5;
 pool.SocketConnectTimeout = 1000;
 pool.SocketTimeout = 3000;

 pool.MaintenanceSleep = 30;
 pool.Failover = true;

 pool.Nagle = false;
 pool.Initialize();

 MemcachedClient mc = new MemcachedClient();
 mc.EnableCompression = false;

 string keyBase = "testKey";
  string obj = "This is a test of an object blah blah es, serialization does not seem to slow things down so much. The gzip compression is horrible horrible performance, so we only use it for very large objects. I have not done any heavy benchmarking recently";

  long begin = DateTime.Now.Ticks;
  for(int i = start; i <start+runs; i++)
 {
  mc.Set(keyBase + i, obj);
 }
 long end = DateTime.Now.Ticks;
 long time = end - begin;

  Console.WriteLine(runs + " sets: " + new TimeSpan(time).ToString() + "ms");

  begin = DateTime.Now.Ticks;
 int hits = 0;
 int misses = 0;
 for(int i = start; i <start+runs; i++)
 {
 string str = (string) mc.Get(keyBase + i);
 if(str != null)
  ++hits;
  else
  ++misses;
 }
 end = DateTime.Now.Ticks;
 time = end - begin;
 Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms");
 Console.WriteLine("Cache hits: " + hits.ToString());
  Console.WriteLine("Cache misses: " + misses.ToString());

 IDictionary stats = mc.Stats();
  foreach(string key1 in stats.Keys)
 {
 Console.WriteLine(key1);
 Hashtable values = (Hashtable)stats[key1];
 foreach(string key2 in values.Keys)
 {
 Console.WriteLine(key2 + ":" + values[key2]);
 }
 Console.WriteLine();
 }
 SockIOPool.GetInstance().Shutdown();
 }
 }
}

  結論通過這個對比測試我們可以看出內建的Cache比使用Memcached要快出約130倍,但是從總體速度上來看並不慢,這兩種方式可以在項目中有選擇性地結合使用可以產生很棒的效果.並且Memcached可使用的內存數量要多得多,同時也可以做集羣避免單點問題. 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章