<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coffee on the Keyboard &#187; software</title>
	<atom:link href="http://coffeeonthekeyboard.com/tag/software/feed/" rel="self" type="application/rss+xml" />
	<link>http://coffeeonthekeyboard.com</link>
	<description>by James Socol</description>
	<lastBuildDate>Mon, 06 Feb 2012 23:33:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>		<item>
		<title>Better Living through Memcached</title>
		<link>http://coffeeonthekeyboard.com/better-living-through-memcached-85/</link>
		<comments>http://coffeeonthekeyboard.com/better-living-through-memcached-85/#comments</comments>
		<pubDate>Mon, 19 May 2008 15:00:54 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[instructions]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[useful]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/better-living-through-memcached-85/</guid>
		<description><![CDATA[I wanted to put something specific in the title, like &#8220;Speed up your service&#8221; or &#8220;Reduce server load&#8221; or &#8220;Limit database calls&#8221; or&#8230; You see why I chose &#8220;Better Living.&#8221; Memcached is a memory caching system with an obvious name. It allows you to store basically any data that can be serialized into a giant, [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to put something specific in the title, like &#8220;Speed up your service&#8221; or &#8220;Reduce server load&#8221; or &#8220;Limit database calls&#8221; or&#8230; You see why I chose &#8220;Better Living.&#8221;</p>
<p><a href="http://danga.com/memcached/">Memcached</a> is a memory caching system with an obvious name. It allows you to store basically any data that can be serialized into a giant, memory-resident hash, then retrieve it with its unique key.</p>
<p>Imagine not querying your database on every request, and you only begin to get a sense of how useful this is.</p>
<p>Let&#8217;s go through a simple, single-server setup.<span id="more-85"></span></p>
<h3>Installing Memcached</h3>
<p>First you&#8217;ll need to make sure <a href="http://www.monkey.org/~provos/libevent/">libevent</a> is installed. Easy enough through yum or apt-get.</p>
<p><a href="http://danga.com/memcached/download.bml">Download Memcached</a> and untar it. Run the <code>configure</code> script. You can probably do this with no arguments. I used <code>--prefix=/usr</code> to change from the default of<code>/usr/local</code>. On your system you may want to add <code>--enable-64bit</code> or <code>--enable-threads</code>, depending on your configuration and expected load.</p>
<p>Run a quick <code>make &amp;&amp; make install</code> and you&#8217;re done.</p>
<h3>Running Memcached</h3>
<p>You can run Memcached from the command line but sticking the <code>-d</code> parameter to run as a daemon. If you&#8217;re still root, you&#8217;ll need to add <code>-u<em>nobody</em></code>, where &#8220;nobody&#8221; is some unprivileged user. And that&#8217;s it. Memcached is up and running on your server and ready to connect.</p>
<p>If you&#8217;re on a system with chkconfig, I&#8217;ve put together a <a href="http://coffeeonthekeyboard.com/wp-content/uploads/2008/05/memcache-chkconfig.zip" title="Memcached Chkconfig Scripts">simple chkconfig script</a> for it. The zip file contains two files, <code>etc/rc.d/init.d/memcached</code> and <code>etc/sysconfig/memcached</code>. Copy them to those locations, <code>chmod +x /etc/rc.d/init.d/memcached</code> and then run <code>chkconfig --add memcached</code>, and <code>chkconfig memcached on</code> to run Memcached when the server starts. You can type <code>service memcached start</code> to get it up and running.</p>
<p>Typing <code>memcached -h</code> will show you all the options, including which ports and IP addresses to listen on, or which socket to use, how much memory to allocate, and others. You can add any of these command line options to the <code>$OPTIONS</code> variable in the <code>/etc/sysconfig/memcached</code> file to set the default options for the Memcached service.</p>
<h3>Using Memcached (Finally!)</h3>
<p>Memcached is installed and running. Now what? Well it sort of depends on your language—there are Memcached bindings for Perl, PHP, Ruby, Java, Python, C, C#, Lua, and even Postgres and MySQL—and your program. I&#8217;m not going to go through the specific implementation, but here&#8217;s the basic idea. (In PHP. Ok, so I lied about that specific-implementation thing.)</p>
<p>Let&#8217;s say our goal is to reduce database load. Then we want to try to preempt as many SELECT queries as we can.</p>
<p>If your original code was like this: (Please! Be safe about SQL-injection.)</p>
<blockquote>
<pre>// What post do we want?
$id = get_current_id();

// Query the database
$query = "SELECT * FROM posts WHERE id = '$id';"
$result = $db-&gt;query($query);

// Now we have the post data
$post = $result-&gt;fetch_assoc();</pre>
</blockquote>
<p>What we&#8217;re doing is getting an ID, possibly from a user, then getting the associated table row and returning it. Now to speed that up with Memcached:</p>
<blockquote>
<pre>// Create a new Memcache object
$m = new Memcache;
$m-&gt;<a href="http://s.hort.cc/D">connect</a>('localhost');

// Check Memcache for the data, first
if ( !$post = $m-&gt;get("post:$id") ) {
  // Not in the cache, get from the database
  $query = "SELECT * FROM posts WHERE id = '$id';";
  $result = $db-&gt;query($query);

  // Get the post data
  $post = $result-&gt;fetch_assoc();

  // If we expect this data to be used again soon,
  // we can store it in the cache
  $m-&gt;set("post:$id", $post);
}</pre>
</blockquote>
<p>There we have all the basics. The string <code>"post:$id"</code> is the Memcached key. The post data is serialized and stored (with <a href="http://s.hort.cc/E">Memcache::set</a>) as a key-value pair. If it&#8217;s in the cache, which we can check with <a href="http://s.hort.cc/F">Memcache::get</a> (unless the stored value was boolean <code>false</code>, then it gets confusing) then we don&#8217;t need to waste a database query.</p>
<p>If you expect data to be used as soon as it&#8217;s created, you can do your database <code>INSERT</code> statements and then store the same data in the cache immediately. (Or <a href="http://s.hort.cc/G">Memcache::replace</a>, whichever is necessary.)</p>
<h3>So, what?</h3>
<p>To be fair, my Memcached example made the code much longer, and required instantiating another object, another TCP connection&#8230; What&#8217;s the payoff?</p>
<p>Memcached was written to improve performance over at LiveJournal when they hit the 20+ million page view/day mark. Imagine that database call was in a loop, or if, instead of serializing one post, we built an array of all the posts from one user, and stored that. You can quickly see how many database queries we eliminate.</p>
<p>For fun, I ran a test that generated a random number, then tried to pull the associated row out of a database. (I only generated numbers for which I knew there was a row.) I iterated over this 3,000 times and did 10 trials each.</p>
<p>In PHP with a MySQL database, the Memcached version was around 60% faster, averaging 0.315 seconds vs. 0.820 seconds without Memcached.</p>
<h3>But Wait! There&#8217;s More</h3>
<p>Since Memcached was designed for a site that already existed on dozens of servers, it&#8217;s based on the idea that you&#8217;d run Memcached on all your web servers, and then treat them like a pool. You can connect to several servers simultaneously to spread the Memcaching around. Data gets hashed to a particular server and, if it&#8217;s available on any of them, gets pulled back.</p>
<p>Memcached can also compress data on the fly with zlib, so if you did plan on storing the last 20 posts of every user, you wouldn&#8217;t be out quite so much space.</p>
<p>Finally, Memcached will keep adding data until it hits its memory limit, and which point it drops the Least Recently Used stuff to make room. So even though you can set data not to expire, you can&#8217;t trust that it will be in the cache forever.</p>
<p>For sites that rely on database reads, Memcached can help a lot. If you&#8217;re site depends more on writes or updates, it will probably do very little, or even cause unnecessary overhead.</p>
<p>But since most data-driven sites are significantly bigger readers than writers, Memcached is a great way to improve response time and quality of service, and to reduce load on those databases for when you really do need to read or write to it.</p>
<h3>Do You Memcached?</h3>
<p>So, do you use Memcached? How? Does it speed up your service or have no noticeable effect?</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/better-living-through-memcached-85/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to: Upgrade or Recompile PHP on RHEL5 (Outdated)</title>
		<link>http://coffeeonthekeyboard.com/how-to-upgrade-or-recompile-php-on-rhel5-59/</link>
		<comments>http://coffeeonthekeyboard.com/how-to-upgrade-or-recompile-php-on-rhel5-59/#comments</comments>
		<pubDate>Sat, 03 Nov 2007 17:00:00 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[recompile]]></category>
		<category><![CDATA[red hat]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/how-to-upgrade-or-recompile-php-on-rhel5-59/</guid>
		<description><![CDATA[Update: This post is nearly two years old, and this is not how I would recommend upgrading PHP on RHEL, yet it continues to get traffic. If I can get my hands on a copy of RHEL, I&#8217;ll update this (or I might try using Fedora just to compare). Upgrading PHP on RHEL 5 is [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: </strong><em>This post is nearly two years old, and this is <strong>not</strong> how I would recommend upgrading PHP on RHEL, yet it continues to get traffic. If I can get my hands on a copy of RHEL, I&#8217;ll update this (or I might try using Fedora just to compare).</em></p>
<p>Upgrading PHP on RHEL 5 is difficult. Having done it on several servers, I&#8217;ve gotten it down to a 15 to 20 step process. It takes a while, but it&#8217;s straightforward. I thought I&#8217;d share, because help was sparse and noncontiguous at best.</p>
<p class="image right"><img src="/blog/images/rhel.png" alt="RedHat Enterprise Linux: Hard to upgrade PHP." width="96" height="31" /></p>
<p>RedHat Enterprise Linux 5 comes with PHP 5.1.6 and, as of this writing, this is the highest version available on yum. If you want to upgrade to 5.2.4, or even recompile 5.1.6 with a custom configuration, you&#8217;ll need to resolve several dependencies, first.</p>
<p class="note">Unless otherwise specified, whenever I run <code>./configure</code>, I always include <code>--enable-shared</code> and <code>--enable-static</code>.</p>
<p>The first step is to make sure you have a working APXS script installed. None of the servers on which I&#8217;ve done this had it. I installed Apache 2.2.4 over the default install, since it was the latest version. Be sure to enable APXS with <code>--enable-so</code>. Be careful configuring Apache, as it likes to install itself in <code>/usr/local/apache2/</code> instead of <code>/etc/httpd/</code>, which you may prefer.</p>
<p>Now we start resolving the dependencies. I&#8217;d start with <strong>libtool</strong> and <strong>libiconv</strong>. The former you should be able to install via yum. The latter you may have to download, and after you configure it, from the source directory copy <code>m4/iconv.m4</code> to <code>/usr/local/share/aclocal/iconv.m4</code>.</p>
<p>Use yum to make sure <code>mysql-devel</code> is installed, you&#8217;ll need it to link to mysql.</p>
<p>Then I&#8217;d do the image manipulation software, since it&#8217;s fairly easy. Use yum to install <code>libjpeg</code>, <code>libpng</code> and <code>freetype</code>. You can then use yum to make sure both <code>gd</code> and <code>gd-devel</code> are installed.</p>
<p>I installed <code>libmcrypt</code>, <code>libmhash</code>, and <code>ming</code> at this point. I&#8217;d say it&#8217;s a good time to get any of these more particular dependencies out of the way. I also installed Tidy, which you need to check out from their CVS repository. You can run <code>build/gnuauto/setup.sh</code> from the Tidy source directory to create the autoconf files.</p>
<p>Now we get to the crux of the matter: configuring PHP. All the major dependencies should have been taken care of. If you have other PHP options you&#8217;ll need, make sure those prerequisites are installed, as well. Run the configure script in the PHP source directory with everything you need enabled. I find it helpful to create a script like <code>php.config</code> with the following format:</p>
<pre>'./configure' \
'--with-cgi' \
'--with-fastcgi' \
'--with-gd' \
...
'--with-xml' \
"$@"</pre>
<p class="note">You need to include the slashes <code>\</code> at the end of every line. The last line, <code>"$@"</code> makes the script output the output of <code>configure</code>.</p>
<p>If you get an error running <code>make</code>, you may need to edit your <code>Makefile</code>. Find the <code>EXTRA_LIBS</code> section (in vi/vim, type <code>&lt;ctrl-c&gt; /EXTRA_LIBS &lt;return&gt;</code>) and add <code>-liconv</code> to the end of the line. Then try <code>make clean &amp;&amp; make</code> and it should work.</p>
<p>You may or may not have to edit your <code>httpd.conf</code>, after running <code>make install</code> from the PHP source directory, to add the <code>AddType</code> or <code>AddHandler</code> directive for PHP.</p>
<p>That should be it. You can install extensions via PECL or Pear and everything should run. Save the source directory and your <code>php.config</code> (or <code>config.nice</code>) file, and you&#8217;ll be able to recompile at any time, in case you forgot something. (I, for example, forgot to add <code>--with-mysql</code> the first time!)</p>
<p>Let me know if you run into other problems. Most can be solved by typing <code>yum install ###-devel</code> to resolve a dependency, but if not, I&#8217;ve done this enough to be of some help.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/how-to-upgrade-or-recompile-php-on-rhel5-59/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

