Learn from Memcached’s Success
Memcached becomes more and more popular nowadays. It is widely used by many heavy loaded sites. Why does it succeed?
Well, of course the first and the most important reason is that it meets the need for speed of the web 2.0 sites, by caching data and objects in memory. However, from the point of view of a server developer, what I want to emphasize is that it is the simplicity of memcached’s protocol design makes it more successful. Take a look at memcached’s protocol:
<command name> <key> <flags> <exptime> <bytes> [noreply]rn
cas <key> <flags> <exptime> <bytes> <cas unique> [noreply]rn
reply: ("ERRORrn", "CLIENT_ERROR <error>rn", "SERVER_ERROR <error>rn",
"STOREDrn", "NOT_FOUNDrn", "EXISTSrn", "NOT_FOUNDrn")
get <key>rn
gets <key>rn
reply: ("ENDrn",
"VALUE <key> <flags> <bytes> [<cas unique>]rn<data block>rn")
delete <key> [<time>] [noreply]rn
reply: ("DELETEDrn", "NOT_FOUNDrn")
incr <key> <value> [noreply]rn
decr <key> <value> [noreply]rn
reply: ("NOT_FOUNDrn",
"<value>rn")
statsrn
stats <args>rn
reply: ("STAT <name> <value>rn",
"STAT items:<slabclass>:<stat> <value>rn"
"ENDrn")
flush_all
reply: ("OKrn")
versionrn
reply: ("VERSION <version>rn")
verbosity
reply: ("OKrn")
quit
With the textual protocol as shown above, memcache can be easily supported and implemented in various programming languages. No wonder dozens of different memcache clients appear. And then it consequently boosts memcached’s use. Simple thing usually will withstand the test of time. The old simple textual protocols, e.g., HTTP, FTP, SMTP and POP3 are still in use on the modern Internet. Not only because textual protocols can be easily parsed and extended, but also they are convenient for human being to read and debug. This is where the UNIX philosophy shines.
In conclusion, always prefer textual protocol when designing your own application. It would turn out to be really a wise decision.
Tank said,
July 14, 2009 @ 10:17 pm
没用过Memcached,好像有点DSL的精神。与其说“prefer textual protocol ”,不如说将传统狭义的(programming) language的思想(token,syntax,bnf,ast…)发扬光大
Tank said,
July 14, 2009 @ 10:23 pm
“textual protocol”的认知是肤浅的。比如xml,灵魂是infoset,textual的xml markup只是infoset序列化的一种形式,皮囊而已。
Tank said,
July 14, 2009 @ 10:43 pm
第一个回复说得不是太顺口。老朱,照我的理解,本文说的是两个截然不同的事情:1)memcached的syntax(不是protocol)很舒爽;2)HTTP等protocol是基于文本的。
深入第一件事,就是DSL的天快亮了;对第二件事,我没啥说的,不过以此来粉UNIX好像有点牵强:)
Tank said,
July 15, 2009 @ 3:32 pm
从没接触过memcached,昨天仅从本文的只言片语了解,感觉它更像一门简单特定的语言的syntax,而不像传统的通讯协议,今天看了下http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt,官方确实叫protocol,不好意思,还有第二条回复有点故意扯淡,也不好意思:)
Tank said,
July 15, 2009 @ 3:49 pm
确实该叫protocol,因为它只由一些简单的command构成,而language,是由terminal及non-terminal构建起庞大的AST。
不能修改自己的发言,我成超级顶贴员了,看来简单的东西不一定就好啊!