About Memcached
          E-mail   lcycenter@gmail.com
Memcached?
Memcached                     LiveJournal
                                            ,              ,
                        ,          Web


LiveJournal                 Memcached MogileFS   Perlbal



              : http://www.danga.com/memcached
?
?
Cache


Do one thing well
Map
key1: value1
key2:value2
key3:value3



                 Cache


 Do one thing well
Map
key1: value1
key2:value2    /
key3:value3



                   Cache


 Do one thing well
Map
key1: value1
key2:value2    /   Expired
key3:value3



                             Cache


 Do one thing well
Map
key1: value1
key2:value2              /            Expired
key3: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       RDBMS



HTML         Marshall
                           SQL
JSON          Java
                          Results
 XML         Objects
?

       WEB          EJB       RDBMS



HTML         Marshall
                           SQL
JSON          Java
                          Results
 XML         Objects
?

       WEB          EJB       RDBMS



HTML         Marshall
                           SQL
JSON          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 Know
L1 cache reference                              0.5 ns
Branch mispredict                                 5 ns
L2 cache reference                                7 ns
Mutex lock/unlock                               100 ns
Main memory reference *                         100 ns
Compress 1K bytes with Zippy                 10,000 ns
Send 2K bytes over 1 Gbps network *          20,000 ns
Read 1 MB sequentially from memory *        250,000 ns
Round trip within same datacenter           500,000 ns
Disk seek *                              10,000,000 ns
Read 1 MB sequentially from network *    10,000,000 ns
Read 1 MB sequentially from disk *       30,000,000 ns
Send packet CA->Netherlands->CA         150,000,000 ns

About memcached

  • 1.
    About Memcached E-mail lcycenter@gmail.com
  • 2.
    Memcached? Memcached LiveJournal , , , Web LiveJournal Memcached MogileFS Perlbal : http://www.danga.com/memcached
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
    Map key1: value1 key2:value2 / key3:value3 Cache Do one thing well
  • 8.
    Map key1: value1 key2:value2 / Expired key3:value3 Cache Do one thing well
  • 9.
    Map key1: value1 key2:value2 / Expired key3:value3 Cache Do one thing well I am not... persistent failover / redundant authentication RDBMS...
  • 10.
    Simple • C/S • support telnet interface • ✴set / get / stats
  • 11.
    Simple • C/S • support telnet interface • ✴set / get / stats
  • 12.
    Simple • C/S • support telnet interface • ✴set / get / stats
  • 13.
    Simple • C/S • support telnet interface • ✴set / get / stats
  • 15.
    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; }
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
    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
  • 21.
  • 22.
  • 23.
    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
  • 24.
    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
  • 25.
    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
  • 26.
    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
  • 27.
  • 28.
  • 31.
    ? WEB EJB RDBMS HTML Marshall SQL JSON Java Results XML Objects
  • 32.
    ? WEB EJB RDBMS HTML Marshall SQL JSON Java Results XML Objects
  • 33.
    ? WEB EJB RDBMS HTML Marshall SQL JSON Java Results XML Objects
  • 34.
    (memcached exptimes) • - SET or DELETE - • version - version key: user_v2_100 - + LRU
  • 35.
    Distribute Memcached • MCHASH • MC Fail-over • MC-MC • DB-MC fail • Key-Value DB
  • 36.
    MC Hash • - MC • Hash - - - •
  • 37.
    idx = Zlib.crc32(key)% servers.size Consistent Hashing
  • 38.
  • 39.
  • 40.
    Others • • hot keys • create local cache • alias key •
  • 44.
    Memcached Client • Marshall • • Multiget support • Compressed support • Async/Sync Modes • Replicated • ...
  • 45.
    Stats • Memcached: • telnet • : stats
  • 46.
  • 47.
    Limits • Key Size(250 bytes) • Data Size (under 1 megabyte) • 32bit/64bit (maximum size of the process)
  • 48.
    Protocol Commands • set/ get / gets / replace / add / delete • append / prepend • increment / decrement • cas (compare and swap atomic!) • stats(detail)
  • 49.
    Numbers Everyone Should Know L1 cache reference 0.5 ns Branch mispredict 5 ns L2 cache reference 7 ns Mutex lock/unlock 100 ns Main memory reference * 100 ns Compress 1K bytes with Zippy 10,000 ns Send 2K bytes over 1 Gbps network * 20,000 ns Read 1 MB sequentially from memory * 250,000 ns Round trip within same datacenter 500,000 ns Disk seek * 10,000,000 ns Read 1 MB sequentially from network * 10,000,000 ns Read 1 MB sequentially from disk * 30,000,000 ns Send packet CA->Netherlands->CA 150,000,000 ns

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 \n
  • #8 \n
  • #9 \n
  • #10 \n
  • #11 \n
  • #12 \n
  • #13 \n
  • #14 \n
  • #15 \n
  • #16 1. NullObject\n2. Expire时的Cache击穿问题\n
  • #17 应用服务器侧:\n1. 取不到数据?\n2. 超时怎么控制?\n\n引入了prefetch worker:\n1. 多了一个run component\n2. 增加了开发、测试、维护的复杂性\n3. 好的系统:自成系统\n
  • #18 \n
  • #19 \n
  • #20 \n
  • #21 在value内部设置1个超时值(timeout1), timeout1比实际的memcache timeout(timeout2)小。当从cache读取到timeout1发现它已经过期时候,马上延长timeout1并重新设置\n
  • #22 在value内部设置1个超时值(timeout1), timeout1比实际的memcache timeout(timeout2)小。当从cache读取到timeout1发现它已经过期时候,马上延长timeout1并重新设置\n
  • #23 \n
  • #24 \n
  • #25 \n
  • #26 \n
  • #27 \n
  • #28 \n
  • #29 \n
  • #30 \n
  • #31 常用:\n1.指定失效时间 + 程序触发\n2.指定失效时间也适用于防止系统错误导致脏数据\n
  • #32 单纯把MC当MEM用时需要做Failover\n
  • #33 \n
  • #34 \n
  • #35 \n
  • #36 \n
  • #37 \n
  • #38 \n
  • #39 \n
  • #40 \n
  • #41 \n
  • #42 \n
  • #43 \n
  • #44 \n
  • #45 \n
  • #46 \n