building a horizontally scalable api in php

Post on 08-Jul-2015

3.697 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Presumptions

You know some MySQL (and InnoDB)

You’ve heard of NoSQL such as CouchDB

You’ve heard of Zend Framework

You know 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)

CouchDBCouchDB, Couchbase, BigCouch...

Stale views

Natural keys

Document update handlers

Design doc modification = rebuild

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'];

}

CachingVarnish

Membase (distributed Memcache)

Op code caching (disable file check)

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.

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'));

}

PHPRequests using date ranges – break

down using DateInterval then cache

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

Exception($message, $code, $previous)

text/uri-list

BuildingSSH keys

CI tool (Hudson/Jenkins) for testing and uploading

Lots of boxes = distribution server

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

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

http://www.xcitestudios.com/

https://cloudant.com/

http://couchdb.apache.org/

http://www.membase.org/

Twitter@wadewomersley

Questions?

top related