Developing Applications with Couchbase, PHP and Symfony
This talk was given at the Symfony User Group Austria (http://symfony-austria.org/). It starts off with a basic introduction on NoSQL and Couchbase, and then moves on how to use the PHP SDK both standalone and integrated in Silex and Symfony.
On 24x365 Grow cluster without applica0on changes, without down0me with a single click Consistent sub-‐millisecond read and write response 0mes with consistent high throughput No down0me for so<ware upgrades, hardware maintenance, etc. Couchbase Server JSON JSON JSON JSON JSON PERFORMANCE Flexible Data Model JSON document model with no fixed schema. Couchbase Server
perform at web scale RDBMS Scales Up Get a bigger, more complex server Users Applica.on Scales Out Just add more commodity web servers Users System Cost Applica0on Performance Rela.onal Database Web/App Server Tier System Cost Applica0on Performance Won’t scale beyond this point
and performance curves NoSQL Database Scales Out Cost and performance mirrors app .er Users Couchbase Distributed Data Store Web/App Server Tier Applica.on Scales Out Just add more commodity web servers Users System Cost Applica0on Performance Applica0on Performance System Cost
Document data model Collec0on of complex documents with arbitrary, nested data formats and varying “record” format. Highly-‐structured table organiza0on with rigidly-‐defined data formats and record structure. JSON JSON C1 C2 C3 C4 JSON { }
DEN 30303 CO 2 MV 94040 CA 3 CHI 60609 IL User Info KEY First ZIP_id Last 4 NY 10010 NY 1 Frank 2 Weigel 2 Ali 2 Dodson 3 Mark 2 Azad 4 Steve 3 Yen ZIP_id CITY ZIP STATE 1 Frank 2 Weigel 2 MV 94040 CA To get info about specific user, you perform a join across 2 tables
Frank Wiegel 94040 2 Joe Smith 94040 3 Ali Dodson 94040 4 Sarah Gorin NW1 5 Bob Young 30303 6 Nancy Baker 10010 7 Ray Jones 31311 8 Lee Chen V5V3M • • • 50000 Doug Moore 04252 50001 Mary White SW195 50002 Lisa Clark 12425 Country ID TEL 3 001 Country ID Country name 001 USA 002 UK 003 Argen.na 004 Australia 005 Aruba 006 Austria 007 Brazil 008 Canada 009 Chile • • • 130 Portugal 131 Romania 132 Russia 133 Spain 134 Sweden User ID Photo ID Comment 2 d043 NYC 2 b054 Bday 5 c036 Miami 7 d072 Sunset 5002 e086 Spain Photo Table 001 007 001 133 133 User ID Status ID Text 1 a42 At conf 4 b26 excited 5 c32 hockey 12 d83 Go A’s 5000 e34 sailing Status Table 134 007 008 001 005 Country Table User ID Affl ID Affl Name 2 a42 Cal 4 b96 USC 7 c14 UW 8 e22 Oxford Affilia.ons Table Country ID 001 001 001 002 Country ID Country ID 001 001 002 001 001 001 008 001 002 001 User Table . . . Making a Change Using RDBMS
1 Frank Wiegel 94040 2 Joe Smith 94040 3 Ali Dodson 94040 4 Sarah Gorin NW1 5 Bob Young 30303 6 Nancy Baker 10010 7 Ray Jones 31311 8 Lee Chen V5V3 • • • 5000 Doug Moore 04252 5001 Mary White 41694 5002 Lisa Clark 12425 User ID Photo ID Comment 2 d043 NYC 2 b054 Bday 5 c036 Miami 7 d072 Sunset 5002 e086 Spain User Table Photo Table User ID Status ID Text 1 a42 At conf 4 b26 excited 5 c32 hockey 12 d83 Go A’s 5000 e34 sailing Status Table User ID Affilia.ons ID Affilia.ons Name 2 a42 Cal 4 b96 USC 7 c14 UW 8 e22 Oxford Affilia.ons Table Rela.onal vs Document Performance 1 Frank 94040 Weigel a42 1 At conf 5 Bob 30303 Young c036 5 Miami 4 Sarah NW1 Gorin b26 4 hockey JSON { } JSON { } JSON { } JSON { } JSON { } JSON { } JSON { } JSON { } JSON { } JSON { } 8 Lee V5V3 Chen e22 8 Oxford 5002 Lisa 12425 Clark e086 5002 Spain c032 5 excited Faster response .mes and higher throughput
Docs distributed evenly across servers • Each server stores both ac.ve and replica docs Only one server ac0ve at a 0me • Client library provides app with simple interface to database • Cluster map provides map to which server doc is on App never needs to know • App reads, writes, updates docs • Mul.ple app servers can access same document at same .me User Configured Replica Count = 1 READ/WRITE/UPDATE ACTIVE Doc 5 Doc 2 Doc Doc Doc SERVER 1 ACTIVE Doc 4 Doc 7 Doc Doc Doc SERVER 2 Doc 8 ACTIVE Doc 1 Doc 2 Doc Doc Doc REPLICA Doc 4 Doc 1 Doc 8 Doc Doc Doc REPLICA Doc 6 Doc 3 Doc 2 Doc Doc Doc REPLICA Doc 7 Doc 9 Doc 5 Doc Doc Doc SERVER 3 Doc 6 APP SERVER 1 COUCHBASE Client Library CLUSTER MAP COUCHBASE Client Library CLUSTER MAP APP SERVER 2 Doc 9
of your JSON documents. • Access your documents not only through the unique ID, but based on the map/reduce query you give it. • Wriaen in JavaScript through the Web UI or through the SDKs. function (doc, meta) { if(doc.type && doc.type == "beer") { emit(doc.name, null); } }
CLUSTER ACTIVE Doc 5 Doc 2 Doc Doc Doc SERVER 1 REPLICA Doc 4 Doc 1 Doc 8 Doc Doc Doc APP SERVER 1 COUCHBASE Client Library CLUSTER MAP COUCHBASE Client Library CLUSTER MAP APP SERVER 2 Doc 9 • Indexing work is distributed amongst nodes • Large data set possible • Parallelize the effort • Each node has index for data stored on it • Queries combine the results from required nodes ACTIVE Doc 5 Doc 2 Doc Doc Doc SERVER 2 REPLICA Doc 4 Doc 1 Doc 8 Doc Doc Doc Doc 9 ACTIVE Doc 5 Doc 2 Doc Doc Doc SERVER 3 REPLICA Doc 4 Doc 1 Doc 8 Doc Doc Doc Doc 9 Query
Provision via wizard in Web Console – Or provision via REST interface: opera0ons folks to automate provisioning (i.e Chef, puppet, Rightscale rightscript) • Linux: Ubuntu and Red Hat/CentOS – Packages for most common distribu0ons. • dpkg -‐i , rpm -‐i, etc. • Mac – Download a .zip, open, drag to Applica0ons • Windows – Download a setup.exe, double click
PHP, Java, .NET, Ruby, C, Python – Community supported for Clojure, JRuby, Perl, Go, Erlang, Node.js • Head to couchbase.com/develop: – Client libraries – Geeng Started guides – Tutorial with a sample applica0on – A complete API reference
hap://www.couchbase.com/develop/php/current • Copy it to the extension directory • Enable it through in your {php|couchbase}.ini through extension=couchbase.so • Start coding! • (and we have yet unofficial windows build as well!) michael@daschlbook ~ $ php -m | grep couchbase couchbase
depends on libcouchbase, a high-‐ performance, portable and flexible Couchbase SDK in C. • Packages available for Debian, RHEL, OS X,... 3 michael@daschlbook ~ $ brew install libcouchbase
– OOP-‐Interface (Recommended) • Two main type of Queries possible – By the unique Key – Through Views • Other helpful methods available for debugging/info • Full API/Docs: hap://www.couchbase.com/docs/couchbase-‐php-‐1.1/index.html
variants: • get($key, ...) -‐> Retrieve a document by its unique key • getAndLock($key, ...) -‐> Retrieve the document and lock it • getAndTouch($key, ...) -‐> Retrieve the document and touch it
meaning available: • set($key, $doc, ...) -‐> Stored and/or override („UPSERT“) • add($key, $doc, ...) -‐> Store and fail if document already exists • replace($key, $doc, ...) -‐> Store and fail if document does not exist • delete($key) -‐> Delete the document
performance further (and avoid loops in your code): • setMul0($keys, $docs, ...) • getMul0($keys, ...), getAndLockMul0($keys, ...), getAndTouchMul0($keys, ...) • More *Mul0 methods available throughout the SDK
the API is straigh~orward. • Allows easy integra.on in both the Caching and DB-‐Access infrastructure. • Higher-‐Level abstrac.ons are le• to ODMs (see Basement later). • In the works: Lithium li3_couchbase (community) • Planned: Doctrine ODM Integra.on
use Basement\model\Document; $client = new Client(); $key = 'sampledocument'; $doc = array('store' => 'me', 'please'); // Through an array $arrayDoc = array(compact('key', 'doc')); $client->save($arrayDoc); // Through the Document object $objectDoc = new Document(compact('key', 'doc')); $client->save($objectDoc);
On 24x365 Grow cluster without applica0on changes, without down0me with a single click Consistent sub-‐millisecond read and write response 0mes with consistent high throughput No down0me for so<ware upgrades, hardware maintenance, etc. Couchbase Server JSON JSON JSON JSON JSON PERFORMANCE Flexible Data Model JSON document model with no fixed schema. Couchbase Server