3 Ways to Optimize Back End

by heru on December 6, 2010

This is the third part of the Web Optimization series.

    In the previous part, we have discussed that there are 3 different areas where you can optimize in a web design – backend area, frontend area, and in-between area.

    In this article, we will go deeper into backend area optimization. When we say “backend”, we are referring to optimization related to what you can do within your server.

    While there are many different ways to optimize your server or backend coding, we will cover 3 most common backend optimizations:

    1. Optimize Your Database Queries and Server

    For a dynamic database driven website, optimizing your database queries and server will greatly enhance your web design performance.

    There are so many ways to optimize your SQL. Here are a few things:

    • Optimize your query performance. Are you indexing the correct keys? Use EXPLAIN to understand on how your queries are executed.
    • Setup slow-query log. Setup your database server to record any slow query so that you can optimize them later on.
    • Tune your SQL server. The number of threads allowed, timeouts, memory size, etc. Fine tune your SQL server to fit your query executions behavior.

    If you are using MySQL, learn more about MySQL optimizations.

    2. Use memcached

    Memcached is a general-purpose distributed memory caching system used to speed up dynamic database-driven websites by caching data and objects in RAM (Wikipedia). This reduces the number of times to read data from external sources such as a database and API. Reading from RAM / memory is much faster compared to accessing the data from other external sources.

    If you are using the LAMP technology, you can easily follow the PHP memcache installation instruction.

    A simple example usage of memcache with PHP and MySQL is as follow:

    // connect to memcache
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    // create a query
    $sql = "SELECT * FROM schools WHERE state = 'WA'";
    // check if result in memcache exists
    $key = md5('mysql_query'.$sql);
    $result = $memcache->get($key);
    if($result == null) {
    // if not, then run the query and store it
    $qry = mysql_query($sql) or die(mysql_error()." : $sql");
    if(mysql_num_rows($qry)> 0) {
    $result = mysql_fetch_object($qry);
    // this will store the data for 1 hour
    $memcache->set($key,$result,0,3600);
    }
    }

    From the above code, we are caching queries using memcache. Therefore, when the same query is called subsequently, the system will pull the data from the RAM / memory instead of running the query again. This reduces the processing time by retrieving the cached result.

    3. ZIPPED IT!

    Finally, before sending your output, compress them so that the data travels is smaller and faster. This reduces the bandwidth for everybody!

    If you are running IIS, you can enable compression.

    If you are on Apache, you can enable output compression with either mod_deflate or mod_gzip. You can do this by adding the following into your .htaccess file:

    # compress text, html, javascript, css, xml:
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript

    # Or, compress certain file types by extension:
    <Files *.html>
    SetOutputFilter DEFLATE
    </Files>

    Or, if you don’t have access to your .htaccess, you can use the following:

    <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>

    In this third part of Web Optimization series on web design, we learn how to optimize by optimizing our SQL queries and server, caching results and objects, and compressing the output. These actions will help to reduce the processing time as well as the size of the output. Hence, it will allow the web to load faster.

    Related Posts

    Previous post:

    Next post: