• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
About memcached
 

About memcached

on

  • 1,194 views

介绍Memcached的基础知识,使用范例,在实际使用过程中需要注意的问题等等...

介绍Memcached的基础知识,使用范例,在实际使用过程中需要注意的问题等等...

Statistics

Views

Total Views
1,194
Views on SlideShare
1,192
Embed Views
2

Actions

Likes
2
Downloads
46
Comments
0

1 Embed 2

http://loveky.info 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 1. NullObject\n2. Expire时的Cache击穿问题\n
  • 应用服务器侧:\n1. 取不到数据?\n2. 超时怎么控制?\n\n引入了prefetch worker:\n1. 多了一个run component\n2. 增加了开发、测试、维护的复杂性\n3. 好的系统:自成系统\n
  • \n
  • \n
  • \n
  • 在value内部设置1个超时值(timeout1), timeout1比实际的memcache timeout(timeout2)小。当从cache读取到timeout1发现它已经过期时候,马上延长timeout1并重新设置\n
  • 在value内部设置1个超时值(timeout1), timeout1比实际的memcache timeout(timeout2)小。当从cache读取到timeout1发现它已经过期时候,马上延长timeout1并重新设置\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 常用:\n1.指定失效时间 + 程序触发\n2.指定失效时间也适用于防止系统错误导致脏数据\n
  • 单纯把MC当MEM用时需要做Failover\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

About memcached About memcached Presentation Transcript

  • About Memcached E-mail lcycenter@gmail.com
  • Memcached?Memcached LiveJournal , , , WebLiveJournal Memcached MogileFS Perlbal : http://www.danga.com/memcached
  • ?
  • ?
  • CacheDo one thing well
  • Mapkey1: value1key2:value2key3:value3 Cache Do one thing well
  • Mapkey1: value1key2:value2 /key3:value3 Cache Do one thing well
  • Mapkey1: value1key2:value2 / Expiredkey3:value3 Cache Do one thing well
  • Mapkey1: value1key2:value2 / Expiredkey3:value3 Cache Do one thing well I am not... persistent failover / redundant authentication RDBMS...
  • Simple• C/S• support telnet interface• ✴set / get / stats
  • Simple• C/S• support telnet interface• ✴set / get / stats
  • Simple• C/S• support telnet interface• ✴set / get / stats
  • Simple• C/S• support telnet interface• ✴set / get / stats
  • Object getFoo(String fooId) { Object foo = memcached.get("foo" + fooId); if (foo != null) return foo; foo = database.fetchFoo(fooId); memcached.set("foo"+fooId, foo); return foo;}
  • Base Patten
  • any problem?Base Patten
  • Prefetch Patten
  • any problem?Prefetch Patten
  • if (memcached.get(key) == null) { if (memcached.add(key_mutex, 3*60*1000) == true) { foo = database.fetchFoo(key); memcached.set(key, foo); memcached.delete(key_mutex); } else { sleep(50); retry(); }} Mutex Patten
  • Mutex Patten
  • any problem?Mutex Patten
  • foo = memcached.get(key);if (foo == null) { if (memcached.add(key_mutex, 3 * 60 * 1000) == true) { foo = database.get(key); memcached.set(key, foo); memcached.delete(key_mutex); } else { sleep(50); retry(); }} else { ...} Mutex Patten II
  • foo = memcached.get(key);if (foo == null) { if (memcached.add(key_mutex, 3 * 60 * 1000) == true) { foo = database.get(key); memcached.set(key, foo); memcached.delete(key_mutex); } else { sleep(50); retry(); }} else { ... here} Mutex Patten II
  • if (foo.timeout >= now()) { if (memcached.add(key_mutex, 3 * 60 * 1000) == true) { foo.timeout += 3 * 60 * 1000; memcached.set(key, foo, KEY_TIMEOUT * 2); foo = database.get(key); foo.timeout = KEY_TIMEOUT; memcached.set(key, foo, KEY_TIMEOUT * 2); memcached.delete(key_mutex); } else { sleep(50); retry(); }} else { return foo;} Mutex Patten II
  • if (foo.timeout >= now()) { if (memcached.add(key_mutex, 3 * 60 * 1000) == true) { foo.timeout += 3 * 60 * 1000; memcached.set(key, foo, KEY_TIMEOUT * 2); foo = database.get(key); foo.timeout = KEY_TIMEOUT; memcached.set(key, foo, KEY_TIMEOUT * 2); memcached.delete(key_mutex); } else { sleep(50); retry(); }} else { return foo;} Mutex Patten II
  • Mutex Patten II
  • any problem?Mutex Patten II
  • ? WEB EJB RDBMSHTML Marshall SQLJSON Java Results XML Objects
  • ? WEB EJB RDBMSHTML Marshall SQLJSON Java Results XML Objects
  • ? WEB EJB RDBMSHTML Marshall SQLJSON Java Results XML Objects
  • • (memcached exptimes)• - SET or DELETE -• version - version key: user_v2_100 - + LRU
  • Distribute Memcached• MC HASH• MC Fail-over • MC-MC • DB-MC fail • Key-Value DB
  • MC Hash• - MC• Hash - - -•
  • idx = Zlib.crc32(key) % servers.size Consistent Hashing
  • Consistent Hashing
  • Consistent Hashing
  • Others•• hot keys • create local cache • alias key•
  • Memcached Client• Marshall•• Multiget support• Compressed support• Async/Sync Modes• Replicated• ...
  • Stats• Memcached: • telnet• : stats
  • Stats
  • Limits• Key Size (250 bytes)• Data Size (under 1 megabyte)• 32bit/64bit (maximum size of the process)
  • Protocol Commands• set / get / gets / replace / add / delete• append / prepend• increment / decrement• cas (compare and swap atomic!)• stats(detail)
  • Numbers Everyone Should KnowL1 cache reference 0.5 nsBranch mispredict 5 nsL2 cache reference 7 nsMutex lock/unlock 100 nsMain memory reference * 100 nsCompress 1K bytes with Zippy 10,000 nsSend 2K bytes over 1 Gbps network * 20,000 nsRead 1 MB sequentially from memory * 250,000 nsRound trip within same datacenter 500,000 nsDisk seek * 10,000,000 nsRead 1 MB sequentially from network * 10,000,000 nsRead 1 MB sequentially from disk * 30,000,000 nsSend packet CA->Netherlands->CA 150,000,000 ns