About Memcached          E-mail   lcycenter@gmail.com
Memcached?Memcached                     LiveJournal                                            ,              ,           ...
?
?
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                                                Cac...
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 = datab...
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);    ...
Mutex Patten
any problem?Mutex Patten
foo = memcached.get(key);if (foo == null) {    if (memcached.add(key_mutex, 3 * 60 * 1000) == true) {        foo = databas...
foo = memcached.get(key);if (foo == null) {    if (memcached.add(key_mutex, 3 * 60 * 1000) == true) {        foo = databas...
if (foo.timeout >= now()) {    if (memcached.add(key_mutex, 3 * 60 * 1000) == true) {        foo.timeout += 3 * 60 * 1000;...
if (foo.timeout >= now()) {    if (memcached.add(key_mutex, 3 * 60 * 1000) == true) {        foo.timeout += 3 * 60 * 1000;...
Mutex Patten II
any problem?Mutex Patten II
?       WEB          EJB       RDBMSHTML         Marshall                           SQLJSON          Java                 ...
?       WEB          EJB       RDBMSHTML         Marshall                           SQLJSON          Java                 ...
?       WEB          EJB       RDBMSHTML         Marshall                           SQLJSON          Java                 ...
•                   (memcached exptimes)•    -   SET or DELETE    -•        version    -    version key: user_v2_100    - ...
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 sw...
Numbers Everyone           Should KnowL1 cache reference                              0.5 nsBranch mispredict             ...
About memcached
About memcached
About memcached
About memcached
About memcached
About memcached
Upcoming SlideShare
Loading in …5
×

About memcached

1,402 views

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,402
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
59
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. About Memcached E-mail lcycenter@gmail.com
    2. 2. Memcached?Memcached LiveJournal , , , WebLiveJournal Memcached MogileFS Perlbal : http://www.danga.com/memcached
    3. 3. ?
    4. 4. ?
    5. 5. CacheDo one thing well
    6. 6. Mapkey1: value1key2:value2key3:value3 Cache Do one thing well
    7. 7. Mapkey1: value1key2:value2 /key3:value3 Cache Do one thing well
    8. 8. Mapkey1: value1key2:value2 / Expiredkey3:value3 Cache Do one thing well
    9. 9. Mapkey1: value1key2:value2 / Expiredkey3:value3 Cache Do one thing well I am not... persistent failover / redundant authentication RDBMS...
    10. 10. Simple• C/S• support telnet interface• ✴set / get / stats
    11. 11. Simple• C/S• support telnet interface• ✴set / get / stats
    12. 12. Simple• C/S• support telnet interface• ✴set / get / stats
    13. 13. Simple• C/S• support telnet interface• ✴set / get / stats
    14. 14. 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;}
    15. 15. Base Patten
    16. 16. any problem?Base Patten
    17. 17. Prefetch Patten
    18. 18. any problem?Prefetch Patten
    19. 19. 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
    20. 20. Mutex Patten
    21. 21. any problem?Mutex Patten
    22. 22. 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
    23. 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 { ... here} Mutex Patten II
    24. 24. 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
    25. 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. 26. Mutex Patten II
    27. 27. any problem?Mutex Patten II
    28. 28. ? WEB EJB RDBMSHTML Marshall SQLJSON Java Results XML Objects
    29. 29. ? WEB EJB RDBMSHTML Marshall SQLJSON Java Results XML Objects
    30. 30. ? WEB EJB RDBMSHTML Marshall SQLJSON Java Results XML Objects
    31. 31. • (memcached exptimes)• - SET or DELETE -• version - version key: user_v2_100 - + LRU
    32. 32. Distribute Memcached• MC HASH• MC Fail-over • MC-MC • DB-MC fail • Key-Value DB
    33. 33. MC Hash• - MC• Hash - - -•
    34. 34. idx = Zlib.crc32(key) % servers.size Consistent Hashing
    35. 35. Consistent Hashing
    36. 36. Consistent Hashing
    37. 37. Others•• hot keys • create local cache • alias key•
    38. 38. Memcached Client• Marshall•• Multiget support• Compressed support• Async/Sync Modes• Replicated• ...
    39. 39. Stats• Memcached: • telnet• : stats
    40. 40. Stats
    41. 41. Limits• Key Size (250 bytes)• Data Size (under 1 megabyte)• 32bit/64bit (maximum size of the process)
    42. 42. Protocol Commands• set / get / gets / replace / add / delete• append / prepend• increment / decrement• cas (compare and swap atomic!)• stats(detail)
    43. 43. 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

    ×