Configure Memcached in Magento 2
Memcached is a general-purpose, distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM. Memcached provides a very large hash table that can be distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order.
To configure or integrate Memcached in magento 2, you please follow this article and below steps:
Note: Please remember one thing Memcache and Memcached are different modules. So please install Memcached.
Magento Official URL: https://devdocs.magento.com/guides/v2.4/config-guide/memcache/memcache_magento.html
To configure Magento to use memcached:
Open /app/etc/env.php in a text editor.
- Open /app/etc/env.php in a text editor.
- Locate the following:
'session' => array ( 'save' => 'files', ),
3. Change it as follows:
'session' => array ( 'save' => 'memcached', 'save_path' => '<memcache ip or host>:<memcache port>' ),
To verify memcached works with Magento:
- Delete the contents of the following directories under your Magento installation directory:
rm -rf var/cache/ var/page_cache/ var/session/*
- Go to any page on the storefront.
- Log in to the Magento Admin and browse to several pages.
If no errors display, congratulations! memcached is working! You can optionally look at memcached storage as discussed in the next step.
If errors display (such as an HTTP 500 (Internal Server Error)), enable developer mode and diagnose the issue. Make sure memcached is running, configured properly, and that env.php has no syntax errors.
- (Optional.) Use Telnet to look at memcached storage.
telnet <memcached host or ip> <memcached port> stats items
The results display similar to the following:
“`terminal STAT items:3:number 1 STAT items:3:age 7714 STAT items:3:evicted 0 STAT items:3:evicted_nonzero 0 STAT items:3:evicted_time 0 STAT items:3:outofmemory 0 STAT items:3:tailrepairs 0
Fix below erros if you got –
- Warning: SessionHandler::read(): open(127.0.0.1:11211\/sess_38f2a2cc6bf9d5dcb055613ecb24cb56, O_RDWR) failed: No such file or directory (2)
Fix – Find your php.ini file and set session.save_handler = memcached
- Warning: SessionHandler::read(): Unable to clear session lock record in
Fix – Setting the following ini settings (also from PHP directly) helped me mitigate this issue on a high load project (~300 concurrent users)
memcached.sess_lock_retries = 10 memcached.sess_lock_wait_min = 1000 memcached.sess_lock_wait_max = 2000