20
PAUL PARTINGTON Magento Developer at CTI Digital @paul_partington

Mage Titans 2015

Embed Size (px)

Citation preview

Page 1: Mage Titans 2015

PAUL PARTINGTONMagento Developer at CTI Digital

@paul_partington

Page 2: Mage Titans 2015

What are We Talking About?

• Caching approaches

• Look at an issue with the configuration cache that we found

• How we fixed it

Page 3: Mage Titans 2015

Web Server 1

Web Server 2

Magento

BlockLayout

ConfigurationCollections

EAVAPI

Full Page Cache

Database

APC Cache

Query Cache

Varnish

Page 4: Mage Titans 2015

Cache Storage

• File

• Database

• Redis or Memcached

Page 5: Mage Titans 2015

The Configuration Cache

• Combines the XML files in the system

• Adds in the system configuration

• Every time you add a new module the size increases

Page 6: Mage Titans 2015

• Every time you add a new website you increase the size of the configuration XML

• Adds around 125KB on Magento EE 1.14.2.0

Multisite

Page 7: Mage Titans 2015

I think I see where you’re going with this

• Multiple Websites (9)

• High Traffic

• Caches are going to get cleared

• The configuration cache was cleared and took the site down

Page 8: Mage Titans 2015

Why’s this happening?

• While the configuration cache is being created, every request causes the cache generation process to start

• CPU load increased as the request uses more memory and takes longer to finish

Page 9: Mage Titans 2015

app/code/core/Mage/Model/App.php

protected function _initModules(){ if (!$this->_config->loadModulesCache() { $this->_config->loadModules(); ... }}

Page 10: Mage Titans 2015

-20 -10 0 10 20 300

0.25

0.5

0.75

1

1.25 CPU Load

Time

CP

U L

oad

Page 11: Mage Titans 2015

Cache Policy

• Caches should only be cleared at non-peak times

• Clients should be aware of the effects that clearing a cache can have

Page 12: Mage Titans 2015

What did we do?

• Created a module

• https://github.com/ctidigital/Configuration-Cache-Lock

• Similar fix - https://github.com/madepeople/Made_Cache

Page 13: Mage Titans 2015

How does the module work?

• Creates a cache lock in a Redis database

• Keep the cache creation to a single request

Page 14: Mage Titans 2015

<global>...<cache_lock>

<enable>1</enable> <!-- If cache locking is enabled or not --><server>127.0.0.1</server> <!-- The Redis database IP --><port>6379</port> <!-- The Redis port --><database>0</database> <!-- The Redis Database to use --><password></password> <!-- The password of the Redis server --><timeout>2.5</timeout> <!-- The Redis timeout --><key>cache_lock</key> <!-- The name of lock in the Redis Database --><max_attempts>10</max_attempts> <!-- The maximum amount of times to retry

the connection. Defaults to 10. --><retry_time>10000000</retry_time> <!-- The amount of microseconds to wait

between checking the cache has generated. Defaults to 10000000 -->

</cache_lock>...

Edit your local.xml

Page 15: Mage Titans 2015

• Creates a local override of Mage_Core_Model_App (sorry!)

• New method called _initModulesWithLocking()

Page 16: Mage Titans 2015

protected function _initModulesWithLocking (){ if ($this->_config->loadModulesCache()) { return $this; } ... // If the cache is currently being regenerated if ($ctiCache->getIsCacheLocked()) { $cacheLock = true; } // If the cache needs to be created if ($cacheLock === false) { // Create a new cache lock $ctiCache->acquireCacheLock(); $this->_config->loadModules(); ... // Release the cache lock $ctiCache->releaseCacheLock(); return $this; } ... while ($cacheLock === true && $attempts < $maxAttempts) { $attempts++; usleep($ctiCache->getRetryTime()); if ($this->_config->loadModulesCache()) { return $this; } }

Page 17: Mage Titans 2015

-20 -10 0 10 20 300

0.25

0.5

0.75

1

1.25Without the Cache Lock With the Cache Lock

Time

CPU

Load

Page 18: Mage Titans 2015

An Unexpected Extra

• Prevents module updates from running more than once

• Poorly written update scripts may display an error if ran more than once

Page 19: Mage Titans 2015

Further Reading

• http://alanstorm.com/magento_config_a_critique_and_caching

Page 20: Mage Titans 2015

Thank You

• @paul_partington

• @CTIDigitalUK