Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CartoDB
: Scaling Maps with PostgreSQL (and Node)

rochoa
November 27, 2015

CartoDB
: Scaling Maps with PostgreSQL (and Node)

rochoa

November 27, 2015
Tweet

Other Decks in Programming

Transcript

  1. •  CartoDB   •  Maps  101  &  CartoDB  Maps  API

      •  Node.js:  Mapnik,  C++  modules,  and  metaAling   •  PosgreSQL:  Postgis  and  TWKB   •  QuesAons Agenda
  2. There  are  trillions  of  new  locaCon  data  points   that

     are  currently  not  generaCng  insights. PROBLEM
  3. Everybody  should  be  able  to  analyse,  visualise   and  extract

     insights  from  locaCon  data.  To  build   the  ecosystem  that  will  enable  individuals  and   businesses  to  map  their  world's  data  and   become  more  knowledgeable  and  successful MISSION
  4. Everybody  should   be  able  to  tell  the   stories

     that  maMer,   create  awesome   maps  &  extract  the   value  of  locaCon   on  their  data.
  5. Components CartoDB.js Odyssey.js VisualRaster Torque.js Vecnik.js Leaflet  /Gmaps/Olayers PostgreSQL PostGIS

    SQL  API Redis Maps  API Torque Mapnik Varnish  Caching Varnish  Caching CartoDB  on  Amazon   elasLc  Recipes CartoDB  on   Amazon  elasLc   Recipes PostgreSQL PostGIS CartoDB  Server  API Rest  API Imports  API CartoDB  Editor Browser
  6. OPEN  SOURCE  COMPANY Some  numbers 30k+  commits  in  CartoDB/cartodb  

    2k+  pull  requests   60+  contributors   ! Much  more  than  CartoDB/cartodb
  7. OPEN  SOURCE  COMPANY Some  numbers,  30  days 200  PR  in

     CartoDB/cartodb   1.3k+  commits   22k+  lines  added  /  46k+  lines  removed   279  closed  issues,  178  new  issues  
  8. •  At  zoom=z:  2^z  *  2^z  =  4^z   •

     At  zoom=16:  4^16=  4,294,967,296 Number  of  tiles
  9. •  An  open  source  toolkit  for  developing  mapping  applicaAons  

    •  At  the  core  is  a  C++  shared  library  providing  algorithms  and  pa]erns  for   spaAal  data  access  and  visualizaAon.   •  Combines  pixel-­‐perfect  image  output  with  lightning-­‐fast  cartographic   algorithms   •  And  exposes  interfaces  in  C++,  Python,  and  Node. mapnik
  10. Metatile  in  practice •  Route  request  to  same  metaAle  backend

     (nginx  +  lua)     •  Be  aggressive  with  UV_THREADPOOL_SIZE,  when  you  can
  11. •  PostGIS  is  a  spaAal  database  extender  for  PostgreSQL  object-­‐relaAonal

      database   •  It  adds  support  for  geographic  objects  allowing  locaAon  queries  to  be  run  in   SQL Postgis
  12. Reducing  vertex  count:  ST_Simplify() •  Since  the  map  output  will

     be  displayed  on  a  screen  with  some  kind  of   resoluAon,  any  vertex  density  that  exceeds  the  output  resoluAon  is  wasted   informaAon.
  13. Pre-­‐filtering:  ST_SnapToGrid() •  Snapping  coordinates  to  a  tolerance  and  removing

     duplicates  is  a  very  fast   operaAon   •  Snapping  at  high  tolerances  can  lead  to  an  output  that  doesn’t  follow  the   original  line  parAcularly  closely,  and  can  contain  some  odd  effects
  14. PostGIS  2.2’s  ST_RemoveRepeatedPoints() •  Includes  an  opAonal  parameter,  to  consider

     any  point  within  a  tolerance  to  be   “repeated”   •  The  cost  is  only  the  distance  calculaAon,  so  it’s  no  more  expensive  than   snapping,  and  the  results  are  much  closer  to  the  original  line.
  15. Representation  size •  The  number  “5”  can  be  stored  as

     a:   •  a  1-­‐byte  character   •  or  a  2-­‐byte  short  integer   •  or  a  4-­‐byte  integer   •  or  an  8-­‐byte  double   •  or  an  8-­‐byte  long  integer
  16. WKB •  Default  implementaAon  of  the  PostGIS  plug-­‐in  for  Mapnik

     uses  “well-­‐known   binary”  (WKB)  format  to  represent  geometries  when  requesAng  them  from   PostGIS.   •  The  WKB  format  in  turn  specifies  8-­‐byte  doubles  for  represenAng  verAces.   •  That  can  take  a  lot  of  space,  space  that  maybe  we  don’t  need  to  use.
  17. TWKB •  Storing  the  distance  between  one  vertex  and  the

     next  vertex  in  a  shape   usually  only  takes  1  or  2  bytes,  because  the  points  aren’t  very  far  apart   •  There  is  a  big  space  savings  available  in  using  “delta”  coordinates  rather  than   “absolute”  coordinates   •  Niklas  Aven  was  working  on  “Tiny  well-­‐known  binary”,  a  representaAon  that   retains  the  logical  structure  of  WKB  (simple  feature  geometry  types)  but  packs   them  into  the  minimum  possible  size  using  delta  encoding  and  variable  length   integers   •  All  this  calculaAng  of  deltas  and  minimum  sized  integers  takes  a  fair  amount   of  CPU  Ame,  but  in  our  applicaAon  the  trade-­‐off  is  worth  it
  18. •  h]p:/ /mapAmeboston.github.io/web-­‐maps-­‐101/     •  h]ps:/ /xkcd.com/977/   •

     h]p:/ /mapnik.org/   •  h]ps:/ /nodejs.org/api/addons.html   •  h]ps:/ /www.mapbox.com/Alemill/docs/guides/metaAles/   •  h]p:/ /blog.cartodb.com/smaller-­‐faster/     •  h]ps:/ /github.com/TWKB/SpecificaAon/blob/master/twkb.md References