search

Memcached调优

Jun 07, 2016 pm 04:31 PM
memcachedOriginalwholearticleTuning

文章系本人原创,转载请保持完整性并注明出自 《四火的唠叨》 项目中有一个对实时响应性比较高的服务,引入了Memcached以减少延迟和减少数据库压力。但是期间遇到了一些问题,这里记录一些调优细节。? 客户端选择 最开始我使用的是 Memcached Java Client,

文章系本人原创,转载请保持完整性并注明出自 《四火的唠叨》

Memcached调优项目中有一个对实时响应性比较高的服务,引入了Memcached以减少延迟和减少数据库压力。但是期间遇到了一些问题,这里记录一些调优细节。?

客户端选择

  • 最开始我使用的是 Memcached Java Client,但是最后放弃了,放弃原因包括:
    • 有时会出现的“No Thread For Socket”异常,我记录在 这里;
    • 它不支持NOREPLY模式(在这种模式下,更新缓存的set操作可以不需要Memcached服务端响应,这使得set操作非常非常快)。
  • 现在我使用的是 XMemcached。

统计信息

可以通过nc命令向Memcached服务端发送消息来获取统计信息,例如:

echo "stats settings" | nc localhost 20200 | sort

但是,我更需要客户端的统计信息,尤其是缓存命中率,set操作成功率等等。所以在客户端添加了一个简单的统计模块。每次处理用户请求的过程中,通常有两次向Cache服务端的提交get请求,很多情况下还有两次set请求,合计消耗17ms,在把set请求改成NOREPLY模式以后,这个数减少到10ms以内。因此,对于实时性要求比较高的情形,请打开这个模式,或者干脆使用异步的set。

服务端参数

  • 可以使用-U来使用UDP传输,但是收效不大。
  • -k参数可以阻止换页操作发生,在内存足够的情况下对提高性能有益。
  • -C参数可以禁用CAS。
  • -t指定使用的线程数,如果你是多CPU、多核CPU,可以把这个值配成和总CPU核数一致。
  • -f参数,增长因子,存储大对象把它配大一点可以提高效率,配小一点可以减少浪费。

客户端参数

  • 在使用Memcached Java Client的时候:
    • 由于它会使用direct memory,一定不能加上DisableExplicitGC这个参数,否则就等着OOM吧;
    • 配置大一些的heap size可以提高L1 cache的命中率;
    • 把alive check置为false。
  • 对于实时性和响应性要求比较高的项目,需要做GC调优,主要是GC时延,比如配置MaxGCPauseMillis参数到一个可以接受的值,但是不是越小越好,减低时延的同时会降低吞吐量。
  • 有同事提了个建议,在客户端存放一个cache key的集合,可以在去cache server查询之前,先在本地查看一下是否有缓存记录(比如用 Bloom filter来实现),如果有,再去cache server查询。这个集合可以和实际的cache key有出入,也许一个小时同步一次就可以。但是实际上实现起来比较困难,本身key set的总量非常大,而且Memcached最初提供获取key iterator的接口返回的是一个限定大小key set的iterator,缺乏实际意义(这个接口在后来Memcached的版本中已经被废弃)。至于stats方法,它会把所有cache对象dump出来,只能小规模调试的时候使用。
  • 关于Nagle算法:Nagle的好处是可以批量处理请求,提高TCP包有效部分的大小,从而提高网络利用率,但是如果对每个请求处理时延要求很高的话请关闭。
  • 一定要指定socket timeout或者get/set timeout。

最后,有人做了一个几个Memcached客户端的综合的性能试验: 链接。

文章系本人原创,转载请保持完整性并注明出自 《四火的唠叨》

分享到:

你可能也喜欢:

  • Memcached调优 Issue record: “No thread for socket” about Memcached
  • Memcached调优 设计缓存框架需要关注的要素
  • Memcached调优 Ehcache详细解读
  • Memcached调优 OSCache框架源码解析
  • Memcached调优 Javascript Memoizer

Memcached调优

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

MySQL String Data Types: A Comprehensive GuideMySQL String Data Types: A Comprehensive GuideMay 08, 2025 am 12:14 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,idealforconsistentlengthdatalikecountrycodes;2)VARCHARforvariable-lengthstrings,suitableforfieldslikenames;3)TEXTtypesforlargertext,goodforblogpostsbutcanimpactperformance;4)BINARYandVARB

Mastering MySQL BLOBs: A Step-by-Step TutorialMastering MySQL BLOBs: A Step-by-Step TutorialMay 08, 2025 am 12:01 AM

TomasterMySQLBLOBs,followthesesteps:1)ChoosetheappropriateBLOBtype(TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOB)basedondatasize.2)InsertdatausingLOAD_FILEforefficiency.3)Storefilereferencesinsteadoffilestoimproveperformance.4)UseDUMPFILEtoretrieveandsaveBLOBsco

BLOB Data Type in MySQL: A Detailed Overview for DevelopersBLOB Data Type in MySQL: A Detailed Overview for DevelopersMay 07, 2025 pm 05:41 PM

BlobdatatypesinmysqlareusedforvoringLargebinarydatalikeImagesoraudio.1) Useblobtypes (tinyblobtolongblob) Basedondatasizeneeds. 2) Storeblobsin Perplate Petooptimize Performance.3) ConsidersxterNal Storage Forel Blob Romana DatabasesizerIndimprovebackupupe

How to Add Users to MySQL from the Command LineHow to Add Users to MySQL from the Command LineMay 07, 2025 pm 05:01 PM

ToadduserstoMySQLfromthecommandline,loginasroot,thenuseCREATEUSER'username'@'host'IDENTIFIEDBY'password';tocreateanewuser.GrantpermissionswithGRANTALLPRIVILEGESONdatabase.*TO'username'@'host';anduseFLUSHPRIVILEGES;toapplychanges.Alwaysusestrongpasswo

What Are the Different String Data Types in MySQL? A Detailed OverviewWhat Are the Different String Data Types in MySQL? A Detailed OverviewMay 07, 2025 pm 03:33 PM

MySQLofferseightstringdatatypes:CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM,andSET.1)CHARisfixed-length,idealforconsistentdatalikecountrycodes.2)VARCHARisvariable-length,efficientforvaryingdatalikenames.3)BINARYandVARBINARYstorebinarydata,similartoC

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.