<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="replication-solutions-scaleout">
      </a>
      19.4.5 Using Replication for Scale-Out
     </h3>
    </div>
   </div>
  </div>
  <p>
   You can use replication as a scale-out solution; that is, where
      you want to split up the load of database queries across multiple
      database servers, within some reasonable limitations.
  </p>
  <p>
   Because replication works from the distribution of one source to
      one or more replicas, using replication for scale-out works best
      in an environment where you have a high number of reads and low
      number of writes/updates. Most websites fit into this category,
      where users are browsing the website, reading articles, posts, or
      viewing products. Updates only occur during session management, or
      when making a purchase or adding a comment/message to a forum.
  </p>
  <p>
   Replication in this situation enables you to distribute the reads
      over the replicas, while still enabling your web servers to
      communicate with the source when a write is required. You can see
      a sample replication layout for this scenario in
   <a class="xref" href="replication-solutions-scaleout.html#figure_replication-scaleout" title="Figure 19.1 Using Replication to Improve Performance During Scale-Out">
    Figure 19.1, “Using Replication to Improve Performance During Scale-Out”
   </a>
   .
  </p>
  <div class="figure">
   <a name="figure_replication-scaleout">
   </a>
   <p class="title">
    <b>
     Figure 19.1 Using Replication to Improve Performance During Scale-Out
    </b>
   </p>
   <div class="figure-contents">
    <div class="mediaobject">
     <img alt="Incoming requests from clients are directed to a load balancer, which distributes client data among a number of web clients. Writes made by web clients are directed to a single MySQL source server, and reads made by web clients are directed to one of three MySQL replica servers. Replication takes place from the MySQL source server to the three MySQL replica servers." src="images/scaleout.png" style="width: 100%; max-width: 376px;"/>
    </div>
   </div>
  </div>
  <br class="figure-break"/>
  <p>
   If the part of your code that is responsible for database access
      has been properly abstracted/modularized, converting it to run
      with a replicated setup should be very smooth and easy. Change the
      implementation of your database access to send all writes to the
      source, and to send reads to either the source or a replica. If
      your code does not have this level of abstraction, setting up a
      replicated system gives you the opportunity and motivation to
      clean it up. Start by creating a wrapper library or module that
      implements the following functions:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <code class="literal">
       safe_writer_connect()
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       safe_reader_connect()
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       safe_reader_statement()
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       safe_writer_statement()
      </code>
     </p>
    </li>
   </ul>
  </div>
  <p>
   <code class="literal">
    safe_
   </code>
   in each function name means that the
      function takes care of handling all error conditions. You can use
      different names for the functions. The important thing is to have
      a unified interface for connecting for reads, connecting for
      writes, doing a read, and doing a write.
  </p>
  <p>
   Then convert your client code to use the wrapper library. This may
      be a painful and scary process at first, but it pays off in the
      long run. All applications that use the approach just described
      are able to take advantage of a source/replica configuration, even
      one involving multiple replicas. The code is much easier to
      maintain, and adding troubleshooting options is trivial. You need
      modify only one or two functions (for example, to log how long
      each statement took, or which statement among those issued gave
      you an error).
  </p>
  <p>
   If you have written a lot of code, you may want to automate the
      conversion task by writing a conversion script. Ideally, your code
      uses consistent programming style conventions. If not, then you
      are probably better off rewriting it anyway, or at least going
      through and manually regularizing it to use a consistent style.
  </p>
 </div>
 <br/>
</div>
