Wednesday, May 22, 2013

Some tips on how to Scale a Website


1) front-end level

  - Try best to cache
    Varnish is a good platform for web application cache. It sits in front of the web app.It speaks HTTP and configure it to cache the contents.
    https://www.varnish-cache.org/
    Behind Varnish we can put other memory based cache like memcached. It is more back end and it is in memory, which is important.          http://memcached.org/

  - performance wise, cookie defeats cache.

 2) server script level

  - avoid unnecessary ORM operations but use template. ORM is basically heavy database operation, which is obvious. Most script language or framework has templates, which allow basic programming ability to display, like accessing, if/for.. etc, but was forbidden from more advanced programming logic, which should be inside of Controller not in View.

  - try to deter everything if possible. Do not run asynch tasks in view function, but queue them for later processing. Also queue sync tasks if it is long-running.

  - pay attention to thundering herd
   http://en.wikipedia.org/wiki/Thundering_herd_problem

 3) Model and DB design level

  - find fast vs slow data
   fast data change a lot, like last access time
   slow data stay stable mostly, like username
   separate these two kinds of data into different tables

  - Make query as small as possible, so if you want to scale website, not afraid of writing custom SQL query.

  - If query result is huge (especially when query for first time), prerendering can be used. Use possible memory cache for query result if needed.

  - Do joins in SQL not in script

  - Do not let users assemble arbitrary queries

  - Never issue an explicit db lock

  - Do not store session, tasks queue in DB

 4) Server horizontal scaling, Sharding

  - Means using distributed server
  Divide db layer over multiple servers, like by custom, by geography ...

  - make sure common data distributed

  - understand growth model of each part of the data and separate them
     (linear, exponential with user base,  linear over time, constant .. )

 By a programmer at Olivet University

1 comment:

  1. http://www.stanford.edu/~ouster/cgi-bin/cs142-fall10/lecture.php?topic=scale

    ReplyDelete