资源描述:
《分布式存储及应用系统架构分析》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、分布式存储及应用系统架构分析目录1Memcached1.1Memcached架构memcached是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。龙兴平(MSN:lxp8@sina.com)第82页共82页2021-6-131.1Memcache实现分析理解1.1.1实现结构SpeedOfcourse,theprimarymotivationforcachingisspeed,soMemcachedisdesignedtobeasfastaspossible.Theinitialprototy
2、peofMemcachedwaswritteninPerl.AlthoughIlovePerl,theprototypewaslaughablyslowandbloated.Perltradesoffmemoryusageforeverything,soalotofpreciousmemorywaswasted,andPerlcan'thandletonsofnetworkconnectionsatonce.ThecurrentversioniswritteninCasasingle-process,single-threaded,asynchronousI/O,event-ba
3、sedd?mon.Forportabilityandspeed,weuselibevent(seetheon-lineResourcessection)foreventnotification.Theadvantageoflibeventisthatitpicksthebestavailablestrategyfordealingwithfiledescriptorsatruntime.Forexample,itchooseskqueueonBSDandepollonLinux2.6,whichareefficientwhendealingwiththousandsofconcu
4、rrentconnections.Onothersystems,libeventfallsbacktothetraditionalpollandselectmethods.InsideMemcached,allalgorithmsareO(1).Thatis,theruntimeofthealgorithmsandCPUusednevervarieswiththenumberofconcurrentclients,atleastwhenusingkqueueorepoll,orwiththesizeofthedataoranyotherfactor.Ofnote,Memcache
5、dusesaslaballocatorformemoryallocation.EarlyversionsofMemcachedusedthemallocfromglibcandendedupfallingontheirfacesafteraboutaweek,eatingupalotofCPUspaceduetoaddressspacefragmentation.Aslaballocatorallocatesonlylargechunksofmemory,slicingthemupintolittlechunksforparticularclassesofitems,thenma
6、intaining龙兴平(MSN:lxp8@sina.com)第82页共82页2021-6-13freelistsforeachclasswheneveranobjectisfreed.SeetheBonwickpaperinResourcesformoredetails.Memcachedcurrentlygeneratesslabclassesforallpower-of-twosizesfrom64bytesto1MB,anditallocatesanobjectofthesmallestsizethatcanholdasubmitteditem.Asaresultofus
7、ingaslaballocator,wecanguaranteeperformanceoveranylengthoftime.Indeed,we'vehadproductionMemcachedserversupfor4–5monthsatatime,averaging7,000queries/second,withoutproblemsandmaintainingconsistentlylowCPUusage.AnotherkeyrequirementforMemcachedw