{"id":6507,"date":"2017-02-27T09:11:42","date_gmt":"2017-02-27T01:11:42","guid":{"rendered":"http:\/\/rmohan.com\/?p=6507"},"modified":"2017-02-27T09:11:42","modified_gmt":"2017-02-27T01:11:42","slug":"installing-memcached-to-improve-database-performance","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6507","title":{"rendered":"Installing Memcached to Improve Database Performance"},"content":{"rendered":"<p>SQL databases are very good at storing and retrieving data, and they can do so quickly. However, no matter how well you tune your database servers there will come a time during periods of high traffic that your database server becomes a large bottleneck. By utilizing technologies like Memcache, we can keep results of frequently used database queries in a cache stored in RAM. Using the cached results significantly decreases that amount of time and effort to retrieve data and present in our application.<\/p>\n<p>Memcache is what\u2019s known as an in-memory key-value store. The key is a unique identifier that is used to quickly search for cached strings or objects. The value is the data that has been cached.\u00a0For the purpose of storing database query results, the key will typically be the query used on your database.<\/p>\n<p>&nbsp;<\/p>\n<h3><\/h3>\n<h3>Key-value Pairs<\/h3>\n<p>A key-value pair is essential an array of data. If you have any experience in programming, you will have a good understanding of how data is stored in Memcache. If were to present a key-value for a database query in an easily read form, it would look similar to the example below.<\/p>\n<p>Searching for long text strings isn\u2019t very efficient, so storing your keys as such is a bad idea. The example above is used just to illustrate how data is stored in cache. In a typical environment you would convert your key (the SQL query) into a MD5 hashed value, for example, before storing or retrieving data from Memcache.<\/p>\n<h3>Typical Infrastructure<\/h3>\n<p>The diagram below illustrates how your infrastructure will typically look when you deploy Memcache in your environment. You will notice that the Memcache server doesn\u2019t communicate directly with your database servers. Instead, they sit in their own pool and your application does all of the work.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Your application will first query the Memcached server(s) for cached database results. If nothing is found, the application will then query your database server(s). Any cached results from the database server will then be written to the Memcache server(s) by your application.<\/p>\n<p>Of course,\u00a0you can\u2019t just simply drop a Memcached server in and expect your application to be able to use. Your application will have to be modified to utilize the Memcache server. This is outside of the scope of this tutorial, but it is important that you know.<\/p>\n<p>&nbsp;<\/p>\n<h3><\/h3>\n<h3>Hardware Requirements<\/h3>\n<p>The hardware requirements for Memcache servers is low. There is very little CPU processing involved and virtually no disk storage needed in addition to the operating system. The only resource you need is RAM. How much will depend on what is being cached and the duration of the caches.<\/p>\n<h2>Installing Memcached<\/h2>\n<p>Memcached can be install anywhere in your infrastructure. For small environments, you may install it on the web application server itself. However, it\u2019s recommended that you create a separate server instance for Memcached. This allows your web application server to focus on just being a application server.<\/p>\n<h3>Ubuntu<\/h3>\n<p>Memcached is available in the default repositories. To install it, you can run the following command.<\/p>\n<pre class=\"bash\">sudo apt-get install memcached<\/pre>\n<h3>CentOS<\/h3>\n<p>Memcached is available in the default repositories. To install it, you can run the following command.<\/p>\n<pre class=\"bash\">yum install memcached<\/pre>\n<p>&nbsp;<\/p>\n<h2>Configuring Memcached<\/h2>\n<p>The default configuration should work fine for testing. However, you may want to fine tune it to better fit your server\u2019s hardware in production.<\/p>\n<h3>Ubuntu<\/h3>\n<ol>\n<li>Open the configuration file into a text editor.\n<pre class=\"bash\">sudo nano \/etc\/memcached.conf<\/pre>\n<\/li>\n<li>To increase the memory cap, look for the following line. The default value is 64MB.\n<pre class=\"output\">-m 64<\/pre>\n<\/li>\n<li>Change the IP address Memcached will listen on. The address should be accessible to your application server.\n<pre class=\"output\">-l 192.168.1.40<\/pre>\n<\/li>\n<li>Limit how many concurrent connections the server will accept. The default is 1024. Limiting connections is important to ensure the server isn\u2019t overwhelmed with requests.\n<pre class=\"output\">-c 1024<\/pre>\n<\/li>\n<li>Save your changes and exit the text editor.<\/li>\n<li>Reload the configuration into Memcached to apply your changes.\n<pre class=\"output\">sudo service memcached reload<\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3>CentOS<\/h3>\n<ol>\n<li>Open the configuration file into a text editor.\n<pre class=\"bash\">vi \/etc\/memcached.conf<\/pre>\n<\/li>\n<li>Modify the MAXCONN value to increase or decrease the maximum amount of connections the server can handle. This will be based on your hardware. To determine the appropriate value, you will need to stress test the server.\n<pre class=\"output\">MAXCONN=\"1024\"<\/pre>\n<\/li>\n<li>Modify the CACHESIZE value to increase or decrease the memory cap. This value will depend on how much RAM is available in your server.\n<pre class=\"output\">CACHESIZE=\"64\"<\/pre>\n<\/li>\n<li>Exit the text editor.<\/li>\n<li>Reload the configuration to apply your changes.\n<pre class=\"output\">service memcached reload<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>SQL databases are very good at storing and retrieving data, and they can do so quickly. However, no matter how well you tune your database servers there will come a time during periods of high traffic that your database server becomes a large bottleneck. By utilizing technologies like Memcache, we can keep results of frequently [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72,16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6507"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6507"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6507\/revisions"}],"predecessor-version":[{"id":6508,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6507\/revisions\/6508"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}