+ All Categories
Home > Documents > How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview •...

How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview •...

Date post: 06-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
39
How PostgreSQL 9 Makes Web Architecture Sweeter Jonathan S. Katz Vice President, Technology hDp:// www.paperlesspost.com
Transcript
Page 1: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

How  PostgreSQL  9  Makes  Web  Architecture  Sweeter  

Jonathan  S.  Katz  Vice  President,  Technology  

hDp://www.paperlesspost.com  

Page 2: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

IntroducJon  

•  No  secret:  PostgreSQL  9  has  some  very  powerful  new  features  

•  How  do  they  extend  to  web  applicaJons?  

2  

Page 3: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Overview  

•  Review  new  features  and  how  they  relate  to  web  apps  

•  Series  of  case  studies  of  PostgreSQL  9  opJmizaJons  

•  Overview  of  Sphinx  vs.  tsearch2  +  tying  into  PostgreSQL  9  +  web  

3  

Page 4: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

PostgreSQL  9:  The  List  

•  hDp://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.0  

4  

Page 5: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Highlights  

•  JOIN  removal  – Play  more  nicely  with  ORMs  •  (ORMs  +  PostgreSQL  –  separate  discussion)  

•  IS  NOT  NULL  +  indexes  •  DEFERRABLE  UNIQUE  CONSTRAINTS  •  Hstore  improvements:  “no  limits”  

•  LISTEN  /  NOTIFY  message  passing  

5  

Page 6: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Caveats  

•  Access  to  new  features  depends  on  PostgreSQL  adapter  – AcJveRecord  does  not  support  hstore  – Nonblocking  access  to  LISTEN  /  NOTIFY  

6  

Page 7: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

More  Highlights?  

•  NaJve  Streaming  ReplicaJon  /  Hot  Standby  

• Wow.  

7  

Page 8: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Real  World:  Paperless  Post  

•  Provides  staJonery  designed  via  web  interface,  delivered  via  email  

8  

Page 9: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Complex  Technology  Stack  

• Web  Servers  +  Load  Balancing  – nginx,  haproxy,  thin  (Ruby  app  server)  

•  Background  Workers  – Message  queues  – Scheduled  jobs  

•  Caching  (memcache,  redis)  

•     

9  

Page 10: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Major  ConsideraJons  

•  High  traffic  (especially  the  holidays)  – Response  Jme  

– High  availability  •  Developer  tools  +  PostgreSQL  playing  nicely  – “transparent  changes”  in  developer  environment  

•  How  does  streaming  replicaJon  /  hot  standby  help?  

10  

Page 11: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Backups  /  Failover  

•  RelaJvely  easy  to  setup  – OpJmal  to  have  some  DBA  knowledge  

•  Could  read  the  official  docs  or    hDp://wiki.postgresql.org/wiki/Streaming_ReplicaJon  

11  

Page 12: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

MulJple  Standbys  

•  Can  “horizontally  scale”  your  Postgres  instances  

•  Read-­‐Only  scale  out  – esp.  if  reads  account  for  a  lot  of  work  

12  

Page 13: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Case:  Business  Intelligence  

•  "Can  you  find  out  how  many  customers  are  using  blue  envelopes  over  the  past  week  and  cross  reference  it  against  our  sales  from  last  year  at  this  Jme?”  – “For  a  report  going  out  today"  

13  

Page 14: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

SoluJons  

•  8.4  – Make  a  SQL  dump  of  tables/database  and  run  query  locally  •  Jme  consuming  

– Run  the  query  on  the  producJon  server  •  Bad  user  experience,  i.e.  slow  site  

•  9.0  – run  the  query  on  a  hot  standby  instance!  – (Web)  applicaJon  for  standby  node  tailored  for  business  intelligence  

14  

Page 15: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Case:  Caching  

•  8.4  – Run  a  query,  cache  it’s  results  (memcache,  etc.)  

•  9.0  – Can  warm  up  a  cache  using  data  from  hot  standby  

15  

Page 16: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Example  

16  

Page 17: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Case:  Changing  Master  Servers  

•  (without  Slony  or  other  tools)  •  8.4  – Turn  site  off,  dump  data,  transfer  data,  load  data,  site  on  

•  9.0  – New  servers  acts  as  hot  standby  – Turn  site  off,  wait  for  standby  to  finish  catching  up,  switch,  turn  site  on  

17  

Page 18: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Case:  Redirect  Read-­‐Only  Queries  

•  Use  hot  standbys  for  read  only  queries  – Maintenance  situaJons  

– Offload  work  •  (Just  cache?)  

•  Caveat  emptor:  performance  may  vary  

18  

Page 19: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Case:  Full  Text  Search  

•  PostgreSQL  full  text  search:  tsearch2  •  Uses  GIN  or  GiST  indexes  •  GIN  – Faster  to  search  over,  slower  to  update  

•  GiST  – Slower  to  search  over,  faster  to  update  

•  (Can’t  have  our  strudel  and  eat  it)  

19  

Page 20: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Our  Path  Deviates  Slightly    

• Will  talk  about  Sphinx  search  engine  •  …and  we  will  get  back  to  PostgreSQL  9  •  …and  the  web  

20  

Page 21: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Sphinx:  High  Performance  Indexing  +  Search  

• WriDen  in  C  •  Supports  PostgreSQL  and  some  other  open  source  RDBMS  

• Makes  full  text  search…fast.    Really  fast.  

21  

Page 22: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Back  to  the  Strudel  Problem  

•  Sphinx  0.9  forces  you  to  do  a  complete  reindex  when  updaJng  search  set  – No  problem  if  data  is  small  or  not  updated  frequently  

– But…  • Highly  dynamic  data  set  

• Lots  of  write  once,  read-­‐only  data  

•  (Sphinx  1.10:  incremental  indexing!  Stay  tuned…)  

22  

Page 23: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

So:  Sphinx  or  tsearch2?  

•  Depends  on  the  use-­‐case  •  Tools  available  – Ruby  has  “ThinkingSphinx”  library  for  Ruby    Sphinx  access  

•  Sphinx  is  “yet  another  service”  • Write-­‐once,  read  many  Jmes  – tsearch2  +  GIN  and  Sphinx  both  do  this  well,  so…  

23  

Page 24: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Benchmark  BaDle!  

•  depesz  did  a  very  interesJng,  elaborate  benchmarking  

•  Source:  hDp://www.depesz.com/index.php/2010/10/17/why-­‐im-­‐not-­‐fan-­‐of-­‐tsearch-­‐2/  

•  Next  few  slides  use  some  content  from  above  source  

24  

Page 25: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

The  Machine  

•  CPU:  Dual  core,  2.93GHz  Intel  Core2Duo  E7500  

• Memory:  4GB  

•  Storage:  Seagate  Barracuda  LP  –  SATA  (3Gb/s)  –  1TB  

•  Ran  against  PostgreSQL  8.4.4  – (I  would  expect  similar  results  with  9)  

25  

Page 26: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

The  Setup  

•  Used  DB  of  ~19M  records  •  Broke  up  into  smaller  tables  for  comparison  

•  Broke  up  tests  by  word  saturaJon  in  text  (e.g.  30%,  20%,  5%)  

26  

Page 27: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Sexng  up  the  tsearch2  indexes  

27  

Page 28: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

tsearch2  and  Searching  

28  

Order  by  Jmestamp,  first  20  records;  Jme  in  milliseconds  

Page 29: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

My  InterpretaJon  

•  GiST  gets  pwned  •  GIN  works  well,  but…  – Explodes  on  large  table,  minus  searches  for  sparse  keywords  

29  

Page 30: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Sexng  up  the  Sphinx  Indexes  

30  

Page 31: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Sphinx  and  search  

31  

Order  by  Jmestamp,  first  20  records;  Jme  in  milliseconds  

Page 32: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

A  Table  Says  1,000  words  (30%  of  them)  

32  

Comparison  for  10  million  rows,  Jme  in  seconds  

• But  in  sphinx  0.9,  there  is  a  Jme  penalty  on  index  creaJon  

Page 33: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Which  Should  I  Use?  

•  Up  to  you  –  you  know  your  data  best  – Benchmark!  

– Infrastructure  setup  • Access  to  adding  new  services  

– What  tools  are  available  in  your  programming  language?  

33  

Page 34: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Why  are  we  discussing  this?  

•  (Other  than  to  fill  Jme)  •  I  actually  had  a  similar  problem:  – one  table  has  10  million  rows  

– a  related  table  has  about  7.5  million  – both  frequently  updated  – both  need  to  be  full  text  searchable  

34  

Page 35: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

With  PostgreSQL  8.4…  

•  Could  only  run  Sphinx  indexer  against  master  database  – Some  ideas  do  not  need  to  be  aDempted  

35  

Page 36: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

With  PostgreSQL  9.0…  

36  

•   would  keep  architecture  same  for  Sphinx  1.10  

Page 37: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Other  Notes  

•  Sphinx  libraries  – hDp://sphinxsearch.com/community/plugins/  

37  

Page 38: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

So…  

•  PostgreSQL  9  is  awesome  –  spread  the  word  – pg_upgrade  makes  upgrade  from  8.4  really  easy  

– only  issues  we’ve  had  have  been  self-­‐inflicted  •  Scaling  your  web  infrastructure  requires  you  to  understand  – your  data  – applicaJon  usage  – the  complexiJes  of  communicaJon  

38  

Page 39: How$PostgreSQL$9$Makes$Web$ Architecture$Sweeter$ · Overview • Review$new$features$and$how$they$relate$to$ web$apps$ • Series$of$case$studies$of$PostgreSQL9 opmizaons • Overview$of$Sphinx$vs.$tsearch2$+tying$into$

Thank  You  

39  


Recommended