Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

All The Little Pieces

3,227 views

Published on

Quick, what do memcache, MogileFS, and Gearman have in common? They are scalable, distributed technologies, and they can also interface with PHP, your ubiquitous web development language. Digg uses all 3 (and a few more) in its quest for social news domination, and this presentation shares what we’ve learned about them and how they are best utilized with PHP.

Published in: Technology
  • Sex in your area is here: ❶❶❶ http://bit.ly/36cXjBY ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ❤❤❤ http://bit.ly/36cXjBY ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Jogando.net 13
    o MELHOR SERVIDOR DE MU ONLINE DO BRASIL! Season 6 Ep. 3
    em todos os Servers. Sendo 7 servers diferenciados proporcionando sua diversão.
    Conheça também o site de Animes Cloud: www.animescloud.com ,mais de 20.000 videos online. ENTRE JÁ NO SITE : www.jogando.net/mu/ >> CADASTRE-SE E GANHE 5 DIAS DE VIP 1ª Mega Maratona Jogando.net ~> MAIS DE 30 DIAS DE EVENTOS .
    Curta nossa página no Facebook : www.facebook.com/pages/jogandonet/3710275296185
    By: talula
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • really great slides
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

All The Little Pieces

  1. 1. all the little pieces distributed systems with PHP Andrei Zmievski @ Digg IPC Spring 2009 @ Berlin
  2. 2. Who is this guy? • Open Source Fellow @ Digg • PHP Core Developer since 1999 • Architect of the Unicode/i18n support • Release Manager for PHP 6 • Twitter: @a • Beer lover (and brewer)
  3. 3. Why distributed? • Because Moore’s Law will not save you • Despite what DHH says
  4. 4. Share nothing • Your Mom was wrong • No shared data on application servers • Distribute it to shared systems
  5. 5. distribute… • memory (memcached) • storage (mogilefs) • work (gearman)
  6. 6. Building blocks • GLAMMP - have you heard of it? • Gearman + LAMP + Memcached • Throw in Mogile too
  7. 7. memcached
  8. 8. background • created by Danga Interactive • high-performance, distributed memory object caching system • sustains Digg, Facebook, LiveJournal, Yahoo!, and many others • if you aren’t using it, you are crazy
  9. 9. background • Very fast over the network and very easy to set up • Designed to be transient • You still need a database
  10. 10. architecture client memcached client memcached client memcached
  11. 11. architecture client memcached client memcached client memcached
  12. 12. architecture client memcached client memcached client memcached
  13. 13. architecture client memcached client memcached client memcached
  14. 14. architecture memcached
  15. 15. slab #1
  16. 16. slab #2 slab #1
  17. 17. slab #3 slab #2 slab #1
  18. 18. slab #4 slab #3 slab #2 slab #1
  19. 19. slab #4 slab #3 slab #2 152 152 152 152 152 slab #1 bytes bytes bytes bytes bytes
  20. 20. slab #4 slab #3 slab #2 456 bytes 456 bytes 456 bytes 152 152 152 152 152 slab #1 bytes bytes bytes bytes bytes
  21. 21. slab #4 slab #3 1368 bytes 1368 bytes slab #2 456 bytes 456 bytes 456 bytes 152 152 152 152 152 slab #1 bytes bytes bytes bytes bytes
  22. 22. slab #4 4104 bytes slab #3 1368 bytes 1368 bytes slab #2 456 bytes 456 bytes 456 bytes 152 152 152 152 152 slab #1 bytes bytes bytes bytes bytes
  23. 23. memory architecture • memory allocated on startup, released on shutdown • variable sized slabs (30+ by default) • each object is stored in the slab most fitting its size • fragmentation can be problematic
  24. 24. memory architecture • items are deleted: • on set • on get, if it’s expired • if slab is full, then use LRU
  25. 25. applications • object cache • output cache • action flood control / rate limiting • simple queue • and much more
  26. 26. PHP clients
  27. 27. PHP clients • a few private ones (Facebook, Yahoo!, etc) • pecl/memcache • pecl/memcached
  28. 28. pecl/memcached • based on libmemcached • released in January 2009 • surface API similarity to pecl/ memcache • parity with other languages
  29. 29. API • get • cas • set • *_by_key • add • getMulti • replace • setMulti • delete • getDelayed / fetch* • append • callbacks • prepend
  30. 30. consistent hashing
  31. 31. consistent hashing A
  32. 32. consistent hashing A B
  33. 33. consistent hashing IP1 A B
  34. 34. consistent hashing IP1 A IP2-1 IP2-2 B
  35. 35. consistent hashing IP1 A IP2-1 IP2-2 B IP3
  36. 36. consistent hashing IP1 A IP2-1 IP2-2 B IP3
  37. 37. compare-and-swap (cas) • “check and set” • no update if object changed • relies on CAS token
  38. 38. compare-and-swap (cas) $m = new Memcached(); $m->addServer('localhost', 11211); do { $ips = $m->get('ip_block', null, $cas); if ($m->getResultCode() == Memcached::RES_NOTFOUND) { $ips = array($_SERVER['REMOTE_ADDR']); $m->add('ip_block', $ips); } else { $ips[] = $_SERVER['REMOTE_ADDR']; $m->cas($cas, 'ip_block', $ips); } } while ($m->getResultCode() != Memcached::RES_SUCCESS);
  39. 39. delayed “lazy” fetching • issue request with getDelayed() • do other work • fetch results with fetch() or fetchAll()
  40. 40. binary protocol • performance • every request is parsed • can happen thousands times a second • extensibility • support more data in the protocol
  41. 41. callbacks • read-through cache callback • if key is not found, invoke callback, save value to memcache and return it
  42. 42. callbacks • result callback • invoked by getDelayed() for every found item • should not call fetch() in this case
  43. 43. buffered writes • queue up write requests • send when a threshold is exceeded or a ‘get’ command is issued
  44. 44. key prefixing • optional prefix prepended to all the keys automatically • allows for namespacing, versioning, etc.
  45. 45. key locality • allows mapping a set of keys to a specific server
  46. 46. multiple serializers • PHP • igbinary • JSON (soon)
  47. 47. future • UDP support • replication • server management (ejection, status callback)
  48. 48. tips & tricks • 32-bit systems with > 4GB memory: memcached -m4096 - p11211 memcached -m4096 - p11212 memcached -m4096 - p11213
  49. 49. tips & tricks • write-through or write-back cache • Warm up the cache on code push • Version the keys (if necessary)
  50. 50. tips & tricks • Don’t think row-level DB-style caching; think complex objects • Don’t run memcached on your DB server — your DBAs might send you threatening notes • Use multi-get — run things in parallel
  51. 51. delete by namespace 1 $ns_key = $memcache->get(quot;foo_namespace_keyquot;); 2 // if not set, initialize it 3 if ($ns_key === false) 4 $memcache->set(quot;foo_namespace_keyquot;, 5 rand(1, 10000)); 6 // cleverly use the ns_key 7 $my_key = quot;foo_quot;.$ns_key.quot;_12345quot;; 8 $my_val = $memcache->get($my_key); 9 // to clear the namespace: 10 $memcache->increment(quot;foo_namespace_keyquot;);
  52. 52. storing lists of data • Store items under indexed keys: comment.12, comment.23, etc • Then store the list of item IDs in another key: comments • To retrieve, fetch comments and then multi-get the comment IDs
  53. 53. preventing stampeding • embedded probabilistic timeout • gearman unique task trick
  54. 54. optimization • watch stats (eviction rate, fill, etc) • getStats() • telnet + “stats” commands • peep (heap inspector)
  55. 55. slabs • Tune slab sizes to your needs: • -f chunk size growth factor (default 1.25) • -n minimum space allocated for key +value+flags (default 48)
  56. 56. slabs slab class 1: chunk size 104 perslab 10082 slab class 2: chunk size 136 perslab 7710 slab class 3: chunk size 176 perslab 5957 slab class 4: chunk size 224 perslab 4681 ... slab class 38: chunk size 394840 perslab 2 slab class 39: chunk size 493552 perslab 2 Default: 38 slabs
  57. 57. slabs Most objects: ∼1-2KB, some larger slab class 1: chunk size 1048 perslab 1000 slab class 2: chunk size 1064 perslab 985 slab class 3: chunk size 1080 perslab 970 slab class 4: chunk size 1096 perslab 956 ... slab class 198: chunk size 9224 perslab 113 slab class 199: chunk size 9320 perslab 112 memcached -n 1000 -f 1.01
  58. 58. memcached @ digg
  59. 59. ops • memcached on each app server (2GB) • the process is niced to a lower level • separate pool for sessions • 2 servers keep track of cluster health
  60. 60. key prefixes • global key prefix for apc, memcached, etc • each pool has additional, versioned prefix: .sess.2 • the key version is incremented on each release • global prefix can invalidate all caches
  61. 61. cache chain • multi-level caching: globals, APC, memcached, etc. • all cache access is through Cache_Chain class • various configurations: • APC ➡ memcached • $GLOBALS ➡ APC
  62. 62. other • large objects (> 1MB) • split on the client side • save the partial keys in a master one
  63. 63. stats
  64. 64. alternatives • in-memory: Tokyo Tyrant, Scalaris • persistent: Hypertable, Cassandra, MemcacheDB • document-oriented: CouchDB
  65. 65. mogile
  66. 66. background • created by Danga Interactive • application-level distributed filesystem • used at Digg, LiveJournal, etc • a form of “cloud caching” • scales very well
  67. 67. background • automatic file replication with custom policies • no single point of failure • flat namespace • local filesystem agnostic • not meant for speed
  68. 68. architecture node tracker node tracker app node DB node tracker node
  69. 69. architecture node tracker node tracker app node DB node tracker node
  70. 70. architecture node tracker node tracker app node DB node tracker node
  71. 71. architecture node tracker node tracker app node DB node tracker node
  72. 72. architecture node tracker node tracker app node DB node tracker node
  73. 73. architecture node tracker node tracker app node DB node tracker node
  74. 74. architecture node tracker node tracker app node DB node tracker node
  75. 75. architecture node tracker node tracker app node DB node tracker node
  76. 76. architecture node tracker node tracker app node DB node tracker node
  77. 77. architecture node tracker node tracker app node DB node tracker node
  78. 78. architecture node tracker node tracker app node DB node tracker node
  79. 79. applications • images • document storage • backing store for certain caches
  80. 80. PHP client • File_Mogile in PEAR • MediaWiki one (not maintained)
  81. 81. Example $hosts = array('172.10.1.1', '172.10.1.2'); $m = new File_Mogile($hosts, 'profiles'); $m->storeFile('user1234', 'image', '/tmp/image1234.jpg'); ... $paths = $m->getPaths('user1234');
  82. 82. mogile @ digg
  83. 83. mogile @ digg • Wrapper around File_Mogile to cache entries in memcache • fairly standard set-up • trackers run on storage nodes
  84. 84. mogile @ digg • not huge (about 3.5 TB of data) • files are replicated 3x • the user profile images are cached on Netscaler (1.5 GB cache) • mogile cluster load is light
  85. 85. gearman
  86. 86. background • created by Danga Interactive • anagram of “manager” • a system for distributing work • a form of RPC mechanism
  87. 87. background • parallel, asynchronous, scales well • fire and forget, decentralized • avoid tying up Apache processes
  88. 88. background • dispatch function calls to machines that are better suited to do work • do work in parallel • load balance lots of function calls • invoke functions in other languages
  89. 89. architecture client worker client gearmand worker client worker
  90. 90. architecture client worker client gearmand worker client worker
  91. 91. architecture client worker client gearmand worker client worker
  92. 92. architecture client worker client gearmand worker client worker
  93. 93. architecture client worker client gearmand worker client worker
  94. 94. applications • thumbnail generation • asynchronous logging • cache warm-up • DB jobs, data migration • sending email
  95. 95. servers • Gearman-Server (Perl) • gearmand (C)
  96. 96. clients • Net_Gearman • simplified, pretty stable • pecl/gearman • more powerful, complex, somewhat unstable (under development)
  97. 97. Concepts • Job • Worker • Task • Client
  98. 98. Net_Gearman • Net_Gearman_Job • Net_Gearman_Worker • Net_Gearman_Task • Net_Gearman_Set • Net_Gearman_Client
  99. 99. Echo Job class Net_Gearman_Job_Echo extends Net_Gearman_Job_Common { public function run($arg) { var_export($arg); echo quot;nquot;; } } Echo.php
  100. 100. Reverse Job class Net_Gearman_Job_Reverse extends Net_Gearman_Job_Common { public function run($arg) { $result = array(); $n = count($arg); $i = 0; while ($value = array_pop($arg)) { $result[] = $value; $i++; $this->status($i, $n); } return $result; } } Reverse.php
  101. 101. Worker define('NET_GEARMAN_JOB_PATH', './'); require 'Net/Gearman/Worker.php'; try { $worker = new Net_Gearman_Worker(array('localhost:4730')); $worker->addAbility('Reverse'); $worker->addAbility('Echo'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . quot;nquot;; exit; }
  102. 102. Client require_once 'Net/Gearman/Client.php'; function complete($job, $handle, $result) { echo quot;$job complete, result: quot;.var_export($result, true).quot;nquot;; } function status($job, $handle, $n, $d) { echo quot;$n/$dnquot;; } continued..
  103. 103. Client $client = new Net_Gearman_Client(array('lager:4730')); $task = new Net_Gearman_Task('Reverse', range(1,5)); $task->attachCallback(quot;completequot;,Net_Gearman_Task::TASK_COMPLETE); $task->attachCallback(quot;statusquot;,Net_Gearman_Task::TASK_STATUS); continued..
  104. 104. Client $set = new Net_Gearman_Set(); $set->addTask($task); $client->runSet($set); $client->Echo('Ich bin ein Berliner');
  105. 105. pecl/gearman • More complex API • Jobs aren’t separated into files
  106. 106. Worker $gmworker= new gearman_worker(); $gmworker->add_server(); $gmworker->add_function(quot;reversequot;, quot;reverse_fnquot;); while (1) { $ret= $gmworker->work(); if ($ret != GEARMAN_SUCCESS) break; } function reverse_fn($job) { $workload= $job->workload(); echo quot;Received job: quot; . $job->handle() . quot;nquot;; echo quot;Workload: $workloadnquot;; $result= strrev($workload); echo quot;Result: $resultnquot;; return $result; }
  107. 107. Client $gmclient= new gearman_client(); $gmclient->add_server('lager'); echo quot;Sending jobnquot;; list($ret, $result) = $gmclient->do(quot;reversequot;, quot;Hello!quot;); if ($ret == GEARMAN_SUCCESS) echo quot;Success: $resultnquot;;
  108. 108. gearman @ digg
  109. 109. gearman @ digg • 400,000 jobs a day • Jobs: crawling, DB job, FB sync, memcache manipulation, Twitter post, IDDB migration, etc. • Each application server has its own Gearman daemon + workers
  110. 110. tips and tricks • you can daemonize the workers easily with daemon or supervisord • run workers in different groups, don’t block on job A waiting on job B • Make workers exit after N jobs to free up memory (supervisord will restart them)
  111. 111. Thrift
  112. 112. background • NOT developed by Danga (Facebook) • cross-language services • RPC-based
  113. 113. background • interface description language • bindings: C++, C#, Cocoa, Erlang, Haskell, Java, OCaml, Perl, PHP, Python, Ruby, Smalltalk • data types: base, structs, constants, services, exceptions
  114. 114. IDL
  115. 115. Demo
  116. 116. Thank You http://gravitonic.com/talks

×