Building a horizontally scalable API in php

4,131 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,131
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Building a horizontally scalable API in php

  1. 1. Building a horizontally scalable API in PHP<br />yada...yada...title was really too long wasn’t it?<br />
  2. 2. Presumptions<br />
  3. 3. Working with MySQL<br />5.1 + InnoDBPlugin or 5.5<br />Binary hashes<br />Learn the configuration options<br />Separation of data logically (e.g. user data vs. analytical data)<br />
  4. 4. CouchDB<br />CouchDB, Couchbase, BigCouch...<br />Stale views<br />Natural keys<br />Document update handlers<br />Design doc modification = rebuild<br />
  5. 5. Example update function<br />function (doc, req) {    try {        varobj = eval('(' + req.body + ')');    } catch (err) {        return [null, {            headers: {                'X-Status-Code': '400'            },            body: 'Invalid JSON'        }];    }    if (!doc) {        if (!obj.url) return [null, {            headers: {                'X-Status-Code': '422'            },            body: 'No URL'        }];         doc = {            _id: req.path[5],            url: obj.url,             statistics: []        };    }    if (obj.url) delete obj.url;    doc.statistics.push(obj);    return [doc, 'OK'];<br />} <br />
  6. 6. Caching<br />Varnish<br />Membase (distributed Memcache)<br />Op code caching (disable file check)<br />
  7. 7. Zend Framework<br />Does have a REST Controller (albeit pants, but it’s a starting point) – and we don’t call it Rest, we just call it an API <br />Extend the controller as your base<br />GET/PUT/POST/DELETE/HEAD/OPTIONS on the extension<br />Reflection allows automatic use of Allow header<br />OPTIONS can parse doc block for automation (@property)<br />A global error controller is great for centralised management of errors.<br />Re-use modules – you’ve got an API you can use.<br />
  8. 8. Example of implemented Methods<br /> public function implementedMethods()<br /> {<br /> if(!isset($this->_actionController))<br /> return array();<br /> $class = get_class($this->_actionController);<br /> $oReflector = new ReflectionClass($class);<br /> $methods = $oReflector->getMethods(ReflectionMethod::IS_PUBLIC);<br /> $implementedMethods = array();<br />foreach($methods as $i => $method) /* @var $method ReflectionMethod */<br /> {<br /> if($method->getDeclaringClass()->getName() == $class)<br /> $implementedMethods[] = str_replace('Action', '', $method->getName());<br /> }<br /> return array_intersect($implementedMethods, array('get', 'put', 'post', 'delete', 'head', 'options', 'trace'));<br /> }<br />
  9. 9. PHP<br />Requests using date ranges – break down using DateInterval then cache<br />fopen(‘php://input’, ‘r’) for PUT data.<br />Exception($message, $code, $previous)<br />text/uri-list<br />
  10. 10. Building<br />SSH keys<br />CI tool (Hudson/Jenkins) for testing and uploading<br />Lots of boxes = distribution server<br />
  11. 11. Testing (Stress)<br />Apache JMeter is a great java GUI/CLI tool for both testing requests and stress testing<br />API’s should inherently allow independent testing of minimal components at the user request level<br />
  12. 12. Links<br />http://jakarta.apache.org/jmeter/<br />http://www.xcitestudios.com/<br />https://cloudant.com/<br />http://couchdb.apache.org/<br />http://www.membase.org/<br />Twitter@wadewomersley<br />
  13. 13. Questions?<br />

×