Wednesday 1 February 2012

Enabling Memcached in Magento



The new local.xml.additional memcached portion that comes standard in Magento now looks like this:

   
<backend></backend><!-- apc / memcached / xcache / empty=file -->
<slow_backend></slow_backend> <!-- database / file (default) - used for 2 levels cache setup, necessary for all shared memory storages -->
<memcached><!-- memcached cache backend related config -->
    <servers><!-- any number of server nodes can be included -->
        <server>
            <host><![CDATA[]]></host>
            <port><![CDATA[]]></port>
            <persistent><![CDATA[]]></persistent>
            <weight><![CDATA[]]></weight>
            <timeout><![CDATA[]]></timeout>
            <retry_interval><![CDATA[]]></retry_interval>
            <status><![CDATA[]]></status>
        </server>
    </servers>
    <compression><![CDATA[0]]></compression>
    <cache_dir><![CDATA[]]></cache_dir>
    <hashed_directory_level><![CDATA[]]></hashed_directory_level>
    <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
    <file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>
</cache>




To get memcached up and working, this configuration can be used:
   
<cache>
<slow_backend>File</slow_backend>
<fast_backend>memcached</fast_backend>
<fast_backend_options>
  <servers>
    <server>
      <host>[ip_of_memcache_server]</host>
      <port>11211</port>
      <persistent>1</persistent>
    </server>
  </servers>
</fast_backend_options>

<backend>memcached</backend>
<memcached>
  <servers>
    <server>
      <host>[ip_of_memcache_server]</host>
      <port>11211</port>
      <persistent>1</persistent>
    </server>
  </servers>
</memcached>
</cache>


This new configuration is similar to the old one with some changes. The <slow_backend> option above is set to use files. This acts as a backup mechanism in the event that the primary memcached instance fails. In previous versions of Magento, there was no failover mechanism. If memcached were to die or stop responding, it would result in the entire Magento site to go down.

It is worth noting that the <slow_backend> cache could also be set to use a memcached config, If this were the same ip and port of the <fast_backend> configuration, the configuration will work but any failover would be negated as a memcached failure would still result in the site going down. As an option, a second memcached instance would be setup to listen on a different port than the default 11211 (or use a local unix socket). This would still allow the <slow_backend> to perform “fast” as it is not dependent on file system access. Alternatively you could also use the /dev/shm memory based file system for the <slow_backend> set to “file” so if the fast memcached backend failed you would still have faster failover to memory instead of disk.

To further decrease the chances that Magento would fail as result of a memcached failure, Magento does support multiple distributive memcached servers. You can run multiple memcached instances on the same server on different ports (or unix sockets) or if your store is running on multiple servers in a cluster environment, you can run a separate memcached instance on each server. In both of these cases, a single memcached failure will not result in the site going down. A distributive configuration can be placed with in the <servers></servers> section and looks like the following:

   
<servers>
  <server>
    <host>[ip_of_memcache_server1]</host>
    <port>11211</port>
    <persistent>1</persistent>
  </server>
  <server>
    <host>[ip_of_memcache_server2]</host>
    <port>11211</port>
    <persistent>1</persistent>
  </server>
  <server>
    <host>[ip_of_memcache_server3]</host>
    <port>11211</port>
    <persistent>1</persistent>
  </server>
</servers>


Please remember to always follow good security practices and never run memcached on an external public ip address. Always run memcached on a local unix socket or localhost. If a clustered environment exists with a private internal network that is in no way accessible from the internet, memcached may be set to listen on those private interfaces.

Magento: How to turn on 'Template Path Hints'

When viewing your theme, it’s helpful to see which template file is being used for which part of the page. Magento has an excellent debugging tool called ‘Template Path Hints’.
1. AdminSystemConfiguration.
2. Select your store from the drop-down in the upper-left corner and wait for the page to reload. Note that you have to be on the website level or lower. The ‘Template Path Hints’ option will not be visible if you are at a higher level.
3. AdvancedDeveloper (all the way at the bottom).
4. Template Path HintsYes.
5. Hit the orange Save Config button.
Go to your store and reload. You should see path specs in dashed boxes around all sections of the page. These paths tell you which template .phtml file is responsible for that section.