title: Fixed: PHP Session Store Error Using Memcached
body: <p>The error message displays on my server:</p>
  <pre><code>$ sudo tail /var/log/php-fpm/5.5/www-error.log
  PHP Warning:  session_start(): Unable to clear session lock record
  </code></pre>
  <p>When my server stores session, it uses the memcached on ElastiCache(AWS).<br />
  Here is my environments:</p>
  <pre><code>*Memcached(ElastiCache)
  -version 1.4.14
  -1 Node
  -cache.t2.micro
  
  $ nginx -v
  nginx version: nginx/1.6.2
  # use unix domain socket  
  (fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;)
  
  $ php-fpm -v
  PHP 5.5.24 (fpm-fcgi) (built: Apr 17 2015 18:04:41)
  Copyright (c) 1997-2015 The PHP Group
  Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
      with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
  
  $ pecl list
  Installed packages, channel pecl.php.net:
  =========================================
  Package   Version State
  ...
  memcached 2.2.0   stable
  ...
  </code></pre>
  <p>Here is data:</p>
  <pre><code>$ cat /etc/php-5.5.ini
  ...
  ...
  ; http://php.net/session.save-handler
  ; session.save_handler = files
  session.hash_bits_per_character = 5
  session.hash_function = 0
  session.use_trans_sid = 0
  session.cache_expire = 180
  session.cache_limiter = nocache
  session.referer_check =
  session.bug_compat_warn = Off
  session.bug_compat_42 = Off
  session.gc_maxlifetime = 1440
  session.gc_divisor = 1000
  session.gc_probability = 1
  session.serialize_handler = php
  session.cookie_httponly =
  session.cookie_path = /
  session.cookie_lifetime = 0
  session.auto_start = 0
  session.name = PHPSESSID
  session.use_only_cookies = 1
  session.use_cookies = 1
  ...
  ...
  
  $ cat /etc/php-5.5.d/memcached.ini
  extension=memcached.so
  
  session.save_handler=memcached
  session.save_path=&quot;*******.cache.amazonaws.com:11211&quot;
  memcached.sess_locking = On
  memcached.sess_lock_wait = 150000
  memcached.sess_lock_max_wait = 0;
  memcached.sess_lock_expire = 0;
  memcached.sess_prefix = &quot;memc.sess.key.&quot;
  memcached.sess_consistent_hash = Off
  memcached.sess_remove_failed = 0
  memcached.sess_number_of_replicas = 0
  memcached.sess_binary = Off
  memcached.sess_randomize_replica_read = Off
  memcached.sess_connect_timeout = 1000
  memcached.sess_sasl_username = NULL
  memcached.sess_sasl_password = NULL
  memcached.compression_type = &quot;fastlz&quot;
  memcached.compression_factor = &quot;1.3&quot;
  memcached.compression_threshold = 2000
  memcached.serializer = &quot;igbinary&quot;
  memcached.use_sasl = Off
  memcached.store_retry_count = 2
  
  $ php -i | grep memcached
  /etc/php-5.5.d/memcached.ini,
  memcached
  memcached support =&gt; enabled
  libmemcached version =&gt; 1.0.8
  memcached.compression_factor =&gt; 1.3 =&gt; 1.3
  memcached.compression_threshold =&gt; 2000 =&gt; 2000
  memcached.compression_type =&gt; fastlz =&gt; fastlz
  memcached.serializer =&gt; igbinary =&gt; igbinary
  memcached.sess_binary =&gt; no value =&gt; no value
  memcached.sess_connect_timeout =&gt; 1000 =&gt; 1000
  memcached.sess_consistent_hash =&gt; no value =&gt; no value
  memcached.sess_lock_expire =&gt; 0 =&gt; 0
  memcached.sess_lock_max_wait =&gt; 0 =&gt; 0
  memcached.sess_lock_wait =&gt; 150000 =&gt; 150000
  memcached.sess_locking =&gt; no value =&gt; no value
  memcached.sess_number_of_replicas =&gt; 0 =&gt; 0
  memcached.sess_prefix =&gt; memc.sess.key. =&gt; memc.sess.key.
  memcached.sess_randomize_replica_read =&gt; no value =&gt; no value
  memcached.sess_remove_failed =&gt; 0 =&gt; 0
  memcached.sess_sasl_password =&gt; no value =&gt; no value
  memcached.sess_sasl_username =&gt; no value =&gt; no value
  memcached.store_retry_count =&gt; 2 =&gt; 2
  memcached.use_sasl =&gt; no value =&gt; no value
  Registered save handlers =&gt; files user memcached
  session.save_handler =&gt; memcached =&gt; memcached
  
  $ yum list installed | grep memcached
  libmemcached.x86_64                  1.0.8-2.6.amzn1               @amzn-main
  memcached.x86_64                     1.4.15-9.12.amzn1             @amzn-main
  php55-pecl-memcached.x86_64          2.2.0-5.14.amzn1              @amzn-updates
  
  $ cat /etc/php-fpm-5.5.d/www.conf
  php_value[session.save_handler] = memcached
  php_value[session.save_path] = &quot;*******.cache.amazonaws.com:11211&quot;
  
  $ memcached-tool *******.cache.amazonaws.com:11211 display
    #  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM
    1      96B         0s       1       0     yes        0        0    0
    2     120B     55039s       1       4     yes        0        0    0
    4     192B      1439s       2    2226     yes        0        0    0
    5     240B         0s       1       0     yes        0        0    0
    6     304B     14477s       1       1     yes        0        0    0
    7     384B      9557s       1       1     yes        0        0    0
    8     480B       268s       1       1     yes        0        0    0
    9     600B      2014s       1       1     yes        0        0    0
  
  $ memcached-tool *******.cache.amazonaws.com:11211 stats
           accepting_conns           1
                 auth_cmds           0
               auth_errors           0
                     bytes      470256
                bytes_read  5006739226
             bytes_written  2668634300
                cas_badval           0
                  cas_hits           0
                cas_misses           0
            cmd_config_get      489750
            cmd_config_set           1
                 cmd_flush           1
                   cmd_get    15601270
                   cmd_set    33168188
                 cmd_touch           0
               conn_yields           0
     connection_structures         260
               curr_config           1
          curr_connections           9
                curr_items        2581
                 decr_hits           0
               decr_misses           0
               delete_hits    15438339
             delete_misses       10047
         evicted_unfetched           0
                 evictions           0
         expired_unfetched     7474894
                  get_hits     8113028
                get_misses     7488242
                hash_bytes      524288
         hash_is_expanding           0
          hash_power_level          16
                 incr_hits           0
               incr_misses           0
                  libevent 1.4.13-stable
            limit_maxbytes   477102080
       listen_disabled_num           0
                       pid           1
              pointer_size          64
                 reclaimed     7476127
              reserved_fds           5
             rusage_system 1539.028000
               rusage_user  838.464000
                   threads           1
                      time  1436772530
         total_connections    15600239
               total_items    31043112
                touch_hits           0
              touch_misses           0
                    uptime     7346275
                   version      1.4.14
  </code></pre>
  <p>Strangely, this error happens about once time per weekly,
  but yesterday, it happens about 2000 times.<br />
  When this error is happens, the server also happens busy status.<br />
  It disappears over time.</p>
  <p>■Cloud Watch Log:
  <img src="https://i.stack.imgur.com/h3Bzg.png" alt="cloud_watch" /></p>
  <p>■New Relic Application Log:
  <img src="https://i.stack.imgur.com/fQA4O.png" alt="new_relic" /></p>
  <p>I don't know what it's happening.
  If you have good solution, please let me know.</p>
  <hr />
  <p><strong>Update at 2020/10/8</strong></p>
  <p>It was long time, I solved this issue:</p>
  <p>When user access came many, it seems that the DNS can't solve on AWS EC2.</p>
  <p><a href="https://aws.amazon.com/jp/premiumsupport/knowledge-center/dns-resolution-failures-ec2-linux/" rel="nofollow noreferrer">https://aws.amazon.com/jp/premiumsupport/knowledge-center/dns-resolution-failures-ec2-linux/</a></p>
  <p>Here is fixed patch.</p>
  <pre><code>$ wget https://raw.githubusercontent.com/awslabs/aws-support-tools/master/EC2/AutomateDnsmasq/AutomateDnsmasq.sh
  $ chmod +x AutomateDnsmasq.sh
  $ sudo ./AutomateDnsmasq.sh
  -&gt; Finally, you must restart php server.
  </code></pre>