Slide 1

Slide 1 text

Developing  Applica.ons  with   Couchbase,  PHP  and  Symfony   Michael  Nitschinger  (@daschl)   Developer  Advocate  at  Couchbase,  Inc.    

Slide 2

Slide 2 text

WHAT  IS  COUCHBASE?  

Slide 3

Slide 3 text

    Couchbase  Server  2.0  is  a  high  performance,   easy  to  scale  and  flexible  Document  “NoSQL”   Database.  

Slide 4

Slide 4 text

Easy   Scalability   Consistent  High   Performance   Always  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

Slide 5

Slide 5 text

New  in  2.0   JSON  support   Indexing  and  Querying   Cross  data  center  replica.on   Incremental  Map  Reduce   JSON JSON JSON JSON JSON

Slide 6

Slide 6 text

2.0  Release  Party  

Slide 7

Slide 7 text

Couchbase  Handles  Real  World  Scale  

Slide 8

Slide 8 text

How  Fast?   hIp://www.slideshare.net/renatko/couchbase-­‐performance-­‐benchmarking   Open  Source  Benchmark:  YCSB   PERFORMANCE

Slide 9

Slide 9 text

RELATIONAL  VS     NoSQL  TECHNOLOGY  

Slide 10

Slide 10 text

Rela.onal  Technology  Scales  Up   Expensive  and  disrup.ve  sharding,  doesn’t  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  

Slide 11

Slide 11 text

NoSQL  Technology  Scales  Out   Scaling  out  flaaens  the  cost  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  

Slide 12

Slide 12 text

Rela.onal  vs  Document  Data  Model   Rela.onal  data  model   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   {         }  

Slide 13

Slide 13 text

RDBMS  Example:  User  Profile   Address  Info   1   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    

Slide 14

Slide 14 text

All  data  in  a  single  document   Document  Example:  User  Profile    {          “ID”:  1,          “FIRST”:  “Frank”,          “LAST”:  “Weigel”,          “ZIP”:  “94040”,          “CITY”:  “MV”,          “STATE”:  “CA”      }   JSON   =   +  

Slide 15

Slide 15 text

User  ID   First   Last   Zip   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   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  

Slide 16

Slide 16 text

Making  the  Same  Change     With  a  Document  Database        {          “ID”:  1,          “FIRST”:  “Frank”,          “LAST”:  “Weigel”,          “ZIP”:  “94040”,          “CITY”:  “MV”,          “STATE”:  “CA”,          “STATUS”:                {    “TEXT”:  “At  Conf”               }   }              “GEO_LOC”:  “134”  },   “COUNTRY”:  ”USA”   Just  add  informa.on  to  a  document   JSON   ,   }  

Slide 17

Slide 17 text

User   ID   First   Last   Zip   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  

Slide 18

Slide 18 text

HOW  COUCHBASE   WORKS  

Slide 19

Slide 19 text

Write  Opera.ons   3   3   2   Managed  Cache   Disk  Queue   Disk   Replica0on   Queue   App  Server   Couchbase  Server  Node   Doc  1   Doc  1   Doc  1   To  other  node  

Slide 20

Slide 20 text

Update  Opera.ons   3   3   2   Managed  Cache   Disk  Queue   Replica0on   Queue   App  Server   Couchbase  Server  Node   Doc  1’   Doc  1   Doc  1’   Doc  1   Doc  1’   Disk   To  other  node  

Slide 21

Slide 21 text

Read  Opera.ons   Couchbase  Server  Node   GET   Doc  1   3   3   2   Disk  Queue   Replica0on   Queue   App  Server   Couchbase  Server  Node   Doc  1   Doc  1   Doc  1   Managed  Cache   Disk   To  other  node  

Slide 22

Slide 22 text

Cache  Evic.ons   3   3   2   Disk  Queue   Replica0on   Queue   App  Server   Couchbase  Server  Node   Doc  1   Doc  6   Doc  5   Doc  4   Doc  3   Doc  2   Doc  1   Doc  6   Doc  5   Doc  4   Doc  3   Doc  2   Managed  Cache   Disk   To  other  node  

Slide 23

Slide 23 text

COUCHBASE  SERVER    CLUSTER   “Green”  Cluster  State   •  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  

Slide 24

Slide 24 text

Add  Nodes  to  Cluster   •  Two  servers  added   One-­‐click  opera.on   •  Docs  automa.cally   rebalanced  across   cluster   Even  distribu0on  of  docs   Minimum  doc  movement   •  Cluster  map  updated   •  App  database     calls  now  distributed     over  larger  number   of  servers         REPLICA   ACTIVE   Doc  5   Doc  2   Doc   Doc   Doc  4   Doc  1   Doc   Doc   SERVER  1       REPLICA   ACTIVE   Doc  4   Doc  7   Doc   Doc   Doc  6   Doc  3   Doc   Doc   SERVER  2       REPLICA   ACTIVE   Doc  1   Doc  2   Doc   Doc   Doc  7   Doc  9   Doc   Doc   SERVER  3       SERVER  4       SERVER  5   REPLICA   ACTIVE   REPLICA   ACTIVE   Doc   Doc  8   Doc   Doc  9   Doc   Doc  2   Doc   Doc  8   Doc   Doc  5   Doc   Doc  6   READ/WRITE/UPDATE   READ/WRITE/UPDATE   APP  SERVER  1   COUCHBASE  Client  Library       CLUSTER  MAP   COUCHBASE  Client  Library       CLUSTER  MAP   APP  SERVER  2   COUCHBASE  SERVER    CLUSTER   User  Configured  Replica  Count  =  1  

Slide 25

Slide 25 text

Fail  Over  Node       REPLICA   ACTIVE   Doc  5   Doc  2   Doc   Doc   Doc  4   Doc  1   Doc   Doc   SERVER  1       REPLICA   ACTIVE   Doc  4   Doc  7   Doc   Doc   Doc  6   Doc  3   Doc   Doc   SERVER  2       REPLICA   ACTIVE   Doc  1   Doc  2   Doc   Doc   Doc  7   Doc  9   Doc   Doc   SERVER  3       SERVER  4       SERVER  5   REPLICA   ACTIVE   REPLICA   ACTIVE   Doc  9   Doc  8   Doc   Doc  6   Doc   Doc   Doc  5   Doc   Doc  2   Doc  8   Doc   Doc   •  App  servers  accessing  docs   •  Requests  to  Server  3  fail   •  Cluster  detects  server  failed   Promotes  replicas  of  docs  to   ac0ve   Updates  cluster  map   •  Requests  for  docs  now  go  to   appropriate  server   •  Typically  rebalance     would  follow   Doc   Doc  1   Doc  3   APP  SERVER  1   COUCHBASE  Client  Library       CLUSTER  MAP   COUCHBASE  Client  Library       CLUSTER  MAP   APP  SERVER  2   User  Configured  Replica  Count  =  1   COUCHBASE  SERVER    CLUSTER  

Slide 26

Slide 26 text

VIEWS  

Slide 27

Slide 27 text

Views   • Allow  you  to  define  secondary  indexes  on  top  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); } }

Slide 28

Slide 28 text

View  Indexing   Couchbase  Server  Node   3   3   2   Managed  Cache   Disk  Queue   Disk   Replica0on   Queue   App  Server   Doc  1   Doc  1   Doc  1   To  other  node   View  engine     Doc  1  

Slide 29

Slide 29 text

Cluster  wide  –  Indexing  and  Querying   COUCHBASE  SERVER    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  

Slide 30

Slide 30 text

INSTALL  &  CONFIG  

Slide 31

Slide 31 text

Download  &  Install     Couchbase  Server  2.0   •  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  

Slide 32

Slide 32 text

Couchbase  SDKs   •  High  performance,  official  client  libraries  for  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  

Slide 33

Slide 33 text

Installing  the  PHP  SDK   • Download  .so/.dll  files  from   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

Slide 34

Slide 34 text

Installing  libcouchbase   • One  more  thing...   • The  PHP  Extension  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

Slide 35

Slide 35 text

THE  COUCHBASE  PHP   SDK  

Slide 36

Slide 36 text

Couchbase  PHP  API   • Access  through   –  Func0ons  (Compa0bility)   –  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      

Slide 37

Slide 37 text

PHP  API  -­‐  Connec.ng   • Use  the  OOP  constructor  to  establish  a  connec.on:   • $client = new Couchbase( $urls, $username, // Optional $password, // Optional $bucket, // Optional $persistent // Optional ); • No  disconnect  needed!  

Slide 38

Slide 38 text

PHP  API  –  Reading  Data   • Use  get()  and  its  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  

Slide 39

Slide 39 text

PHP  API  –  Wri.ng  Data   • Different  methods  with  similar  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  

Slide 40

Slide 40 text

PHP  API  –  „Mul.“  Opera.ons   • Bulk  opera.ons  to  increase  performance  further  (and  avoid   loops  in  your  code):   • setMul0($keys,  $docs,  ...)   • getMul0($keys,  ...),  getAndLockMul0($keys,  ...),   getAndTouchMul0($keys,  ...)   • More  *Mul0  methods  available  throughout  the  SDK  

Slide 41

Slide 41 text

PHP  API  –  View  Access   • Use  the  „view()“  method  to  query  a  view:   $result = $cb->view("design", "view"); foreach($result["rows"] as $row) { // Access the result through $row! }

Slide 42

Slide 42 text

FRAMEWORK   INTEGRATION  

Slide 43

Slide 43 text

Basic  Integra.on   • Simple,  because  no  ORM  is  needed  and  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  

Slide 44

Slide 44 text

Integra.on  Example:  Silex   $app->get('/show/{id}', function($id) use ($app, $cb) { $doc = $cb->get($id); if(!$doc) { return $app->abort(404, 'Document not found'); } return $app['twig']->render('show.twig.html', array ( 'doc' => json_decode($doc, true) )); }); • Loading  a  Document  and  passing  it  to  the  View:  

Slide 45

Slide 45 text

Integra.on  Example:  Silex   • Use  Views  to  read  secondary  indexes:   4 $app->get('/', function() use ($app, $cb) { $options = array('limit' => 15); $results = $cb->view("beer", "brewery_beers", $options); $docs = array(); foreach($results['rows'] as $row) { $id = $row['id']; $doc = json_decode($cb->get($row['id']), true); $docs[] = $doc + array('id' => $row['id']); } return $app['twig']->render('index.twig.html', array ( 'docs' => $docs )); });

Slide 46

Slide 46 text

Integra.on  Example:  Symfony   • Define  a  Service  (config.yml)     • Use  it  in  your  controller:   services: couchbase: class: Couchbase arguments: ["127.0.0.1", "", "", "beer-sample"] public function showAction($name) { $beer = $this->get('couchbase')->get($name); return $this->render('...twig', array( 'beer' => json_decode($beer, true) )); }

Slide 47

Slide 47 text

BASEMENT  

Slide 48

Slide 48 text

Basement  Overview   • 5.3+  ODM  for  the  Couchbase  PHP  SDK   • Targets   –  Namespaces   –  Simplify  the  day-­‐to-­‐day  usage   –  Easy  ODM  Mapping  (PHP  Objects  to  their  JSON  Docs)   –  Well  tested  and  documented   • haps://github.com/Basement/Basement   • Composer:   haps://packagist.org/packages/basement/basement  

Slide 49

Slide 49 text

Basement  Features   • Connec.on  Management   use Basement\Client; $default = new Client(); $backup = New Client(array('name' => 'backup')); .... $backup = Client::connections('backup'); 4

Slide 50

Slide 50 text

Basement  Documents   • Easier  Document  Management   5 use Basement\Client; 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);

Slide 51

Slide 51 text

Retreiving  Documents   • By  the  Unique  Key   • By  View   $documents = $client->findByKey($document->key()); foreach($documents as $doc) { echo $doc->key(); // Prints "my_blogpost“ echo $doc->doc(); // Contains the stored array echo $doc->cas(); // Holds the associated CAS value } $viewResult = $client->findByView("design", "view"); foreach($viewResult->get() as $document) { echo $document->key(); }

Slide 52

Slide 52 text

More  Features   • Currently:   –  More  Document-­‐Related  Features   –  Custom  Transcoders   –  Environment  Management   • Planned   –  Classic  ODM  support  (Object  to  JSON)   –  Valida0ons   –  Design  Document  Management   –  Pagina0on   –  Support  for  higher-­‐level  abstrac0ons  (Framework  Integra0ons)   •  Filters,  Hooks  

Slide 53

Slide 53 text

SUMMARY  

Slide 54

Slide 54 text

Easy   Scalability   Consistent  High   Performance   Always  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

Slide 55

Slide 55 text

QUESTIONS?  

Slide 56

Slide 56 text

DEMO  TIME!  

Slide 57

Slide 57 text

No content