building a horizontally scalable api in php

13
yada...yada...title was really too long wasn’t it?

Upload: wade-womersley

Post on 08-Jul-2015

3.697 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Building a horizontally scalable API in php

yada...yada...title was really too long wasn’t it?

Page 2: Building a horizontally scalable API in php

Presumptions

You know some MySQL (and InnoDB)

You’ve heard of NoSQL such as CouchDB

You’ve heard of Zend Framework

You know PHP

Page 3: Building a horizontally scalable API in php

Working with MySQL5.1 + InnoDB Plugin or 5.5

Binary hashes

Learn the configuration options

Separation of data logically (e.g. user data vs. analytical data)

Page 4: Building a horizontally scalable API in php

CouchDBCouchDB, Couchbase, BigCouch...

Stale views

Natural keys

Document update handlers

Design doc modification = rebuild

Page 5: Building a horizontally scalable API in php

Example update functionfunction (doc, req) {

try {var obj = 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'];

}

Page 6: Building a horizontally scalable API in php

CachingVarnish

Membase (distributed Memcache)

Op code caching (disable file check)

Page 7: Building a horizontally scalable API in php

Zend Framework 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

Extend the controller as your base

GET/PUT/POST/DELETE/HEAD/OPTIONS on the extension

Reflection allows automatic use of Allow header

OPTIONS can parse doc block for automation (@property)

A global error controller is great for centralised management of errors.

Re-use modules – you’ve got an API you can use.

Page 8: Building a horizontally scalable API in php

Example of implemented Methodspublic function implementedMethods()

{if(!isset($this->_actionController))

return array();

$class = get_class($this->_actionController);$oReflector = new ReflectionClass($class);$methods = $oReflector->getMethods(ReflectionMethod::IS_PUBLIC);$implementedMethods = array();

foreach($methods as $i => $method) /* @var $method ReflectionMethod */{

if($method->getDeclaringClass()->getName() == $class)$implementedMethods[] = str_replace('Action', '', $method->getName());

}

return array_intersect($implementedMethods, array('get', 'put', 'post', 'delete', 'head', 'options', 'trace'));

}

Page 9: Building a horizontally scalable API in php

PHPRequests using date ranges – break

down using DateInterval then cache

fopen(‘php://input’, ‘r’) for PUT data.

Exception($message, $code, $previous)

text/uri-list

Page 10: Building a horizontally scalable API in php

BuildingSSH keys

CI tool (Hudson/Jenkins) for testing and uploading

Lots of boxes = distribution server

Page 11: Building a horizontally scalable API in php

Testing (Stress)Apache JMeter is a great java GUI/CLI

tool for both testing requests and stress testing

API’s should inherently allow independent testing of minimal components at the user request level

Page 12: Building a horizontally scalable API in php

Links http://jakarta.apache.org/jmeter/

http://www.xcitestudios.com/

https://cloudant.com/

http://couchdb.apache.org/

http://www.membase.org/

Twitter@wadewomersley

Page 13: Building a horizontally scalable API in php

Questions?