PHP session in Mysql VS Memcached

In my previous companies we used to serve sessions from mysql via a custom made session handlers and to be honest we never really had any problems with it. I cant actually remember the traffics and concurrency rates but it was not that low. Currently the solution im working with uses memcached for sessions storage, the default build in session handler. I am a bit worried about the way it works with memcached.

Problems with memcached sessions in PHP

First of all memcached is a cache storge engine and it was not designed with sessions in mind. The way php extension uses memcache causes a few more problems in the long run. Here are my concerns:

Using early cache rebuild to optimize web cache performance

Having a busy website requires a few techniques to optimize the performance of caches. It is not that difficult and should help you reduce the load times (pessimistic load times) a whole lot.

Imagine you have home page cache (as fragments or entire page). Its all good and beautiful as long as it sits in the cache. All threads connect to cache, get the content and send to the user. What happens when the cache expires? Well all threads have to rebuild it. Unfortunately on very busy content elements it can cause dozens of processes trying to rebuild the same page!

Cache expiration consequences on busy pages

Mixed feelings about PHP vs THE BIG GUYS

Lately i was readin some more about current Java based frameworks and development platforms. We even had some presentations from TOP players (Ill call them the Bestests) in Java world. To be honest i was very unimpressed. I started to look more critical on the Java community.

For years i had some Java fascination that was planted in my brain by Bestests when i was in high school. I always though 'well damn! if they have the best people and best minds, if they have shitload of money ... that has to lead to something great right?!'. Well im not sure any more. Maybe its that online content managment is not Java's good side? maybe we were unlucky with the presentations and frameworks we saw so far?

Dec 2009, Artur Ejsmont

PALM v3 - bundle of Php Apache Linux and Memcached Cacti scripts (APC included)

Finally i had some time to gather all the files together and make third release of my cacti monitoring scripts and graph templates. Monitoring of application and server health is a important task while trying to maintain high availability. Without monitoring you dont know what goes wrong nor when. Some time ago i realized that some of the graphs i used to use were at least not perfect. I searched the web, read a bit of documentation and decided to put together a set of simple scripts gathering key server metrics of average web server. I still dont have postgres nor mysql stats included but .... who knows .... maybe in version 4! :- )

New version has a few new metrics as well as some bugfixes. I also added better support for non pure Debian systems

Web Security - Part 1 - Google Hacking

Doing security reviews from time to time of web applications you can find real beauties. You see GET variables in eval, system, include and all sorts of things you would not even imagine that could run live for years without anyone noticing it :- )

I dont really have great english nor writing skills but i thought i will try to write a few parts of the security article just to give something back to the community. Its not really rocket science nor my invention or discovery. Its just day and weeks and years spent working with web apps and reading here and there. What i hope is to write something you will enjoy and maybe put some new spin on some issues.

PHP display_errors => You will be Google-Hacked for sure

Loading URL with CURL from specified IP address

Sometimes we have to automate some pages testing or loading content or whatever. CURL is an excelent library for any HTTP interfacing.

If you ever have to connect to a server and ask for particular server name you can do it with curl setopt parameters. If you want to get file /index.php from www.mytopsite.com from particular IP (without adding any /etc/hosts mappings) you can do it like this:

$headers = array("Host: www.myfaketopsite.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://10.1.2.3/index.php");

This is really helpful if you have a farm of 40 web servers and you have to ask each and every one, load some file and process it. Im sure you can think of other usages.

Wget page from server using IP

Nov 2009, Artur Ejsmont

PHP Memcache extension API Pitfall

It happened again, i spent like 15 minutes trying to figure out what am i doing wrong before i realized that i got caught in a API snare ;- )

Problem i had was caused that PHP extension for Memcached communication has a strange way of interpretation time to live parameter. If you provide 3rd parameter to set function it will set value in cache and mark it to expire after X amount of seconds. Well that all cool and nice but if you try to pass amount of seconds greater than 2.5 mln seconds (about 30 days) it will be actually interpreted as unix timestamp! Oh noooooo! : -)

Too many magic methods in PHP code can be a real pain!

Magic methods in PHP can be a bit confusing especially if you do everything via __get and __call. They can be very usefull but adding them
by default everywhere is definetly a bad idea. I dont like __get/__set accessors that let you access enything. That defeats purpose of
encapsulation and promotes cross dependencies.

Recently i came across a silly problem .... i think its interesting enough to have a look at it.

So what would you expect from that code?

	var_dump(empty($param->reference_class));
	var_dump($param->reference_class);

Any reasonable person would say that there are two options:

1. first one bool:true, second one some value

2. first one bool:false, second one 0,’’,null or empty array

PHP Soap Error - Procedure 'xxx' not present

In PHP while using SOAP extension and calling external service, you might get into problem that PHP keeps on saying that method does not exist.
You are calling a method on a service that has the method and you are still getting "Procedure 'xxx' not present" error.
Error would look more or less like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Server</faultcode>
         <faultstring>Procedure 'gettwotandomitems' not present</faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Oct 2009, Artur Ejsmont

PALM - bundle of Linux system, Network, Apache2, PHP, APC and Memcached scripts and Cacti graphs

After some more work on the weekend and playing around with cacti i have fixed up some of the previous scripts and joined input methods with data sources and graphs all together.

I have also added a host template so now adding servers will be much easier you just enter host name and click ccreate graphs ... job done.

Package includes graphs to monitor Linux system metrics, disk IO, network IO, apache status, APC op code cache, memcached.

Im still adding stuff to it so make sure to come back for an update : -)

You can see some of the graphs in earlier versions here:
Linux graphs

Monitoring APC op code cache for PHP with cacti graphs

APC is one of the best PHP op code caches. To be sure its performing the best it can you should monitor its status and make sure it has enough memory as well as correct ini settings.

You want to monitor memory allocated for op code cache as well as user stored items. Its important to get it right as wrong settings may reset your complete cache once it gets full:

You should also monitor the hit and miss ratio for op code file cache:

Aug 2009, Artur Ejsmont

Monitoring Memcached status with cacti graphs on rrdtool

Memcache is an awesome caching server but managing it is a bit more complex than most people think. It used to have quite many bugs in its history plus the algorithms of memory allocation and replacement of items might cause confusion. To manage it efficiently you have to monitor hits and misses ratio:

Then another important information you want to track is evictions rate as climbing evictions might indicate that you cache too much or have not enough memory.

Aug 2009, Artur Ejsmont

A few memcache gotchas

Memcache is a great caching tool thats true. Unfortunately optimizing performance it sometimes works not in the way we would expect it to.

In general new versions of memcached are great and you should not expect many bugs but older versions like debian's 1.1.12 seem to have some issues. We could not upgrade yet but very soon we should run latest stable version.

So far we seem to have to restart memcache every few days on our servers as they get stuck. You are not able to set values - and there is no reason for it except that there has to be some bug. We also memcached crash ocassionally - but thats very rare.

The biggest gotcha is definitely how memcache is managing the memory.

Couch DB performance seems to suck

We are looking for some nice solution to store persistently object-like data. Few weeks ago we had a look at Couch DB and performance looked horrible. We did not even bother to make some more realistic tests as initial data was just disqualifying.

We want to store objects with very dynamic schema and be able to index some of the properties. For example we want all objects of type book with title equal to X. We need it to be very fast. And when i mean very fast i mean memcache-kind of fast ;- )

Jul 2009, Artur Ejsmont

Review - Head First - Ajax

Well i read this book recently because i loved all the other head first books. Unfortunately i was very disappointed. Book seems to focus on completly unimportant things not trying to explain real concepts behind ajax and modern websites development.

I would definitely not recommend it no matter who you are. It wont be good for css guys nor for beginners nor for business people nor web developers. Its simply a poor book, well or i just did not like it at all.

Final score: 3/10

Jul 2009, Artur Ejsmont

Review - Pro Drupal Development

I have read it last year when i was working for a while on Drupal 6 project and i can say its a nice book. I have read 2 more Drupal books and this one was by far better than the other ones.

Language was easy and readable, structure was good and the drupal api was explained well. Book will give you decent understanding of drupal internals and how to create your own modules.

Jul 2009, Artur Ejsmont

Another cookie rejection pitfall - problems with null path and domain

There are another two pitfalls easy to stumble upon when it comes to cookies. These are path and domain related issues. Not setting path on your persistent cookies might be frustrating. Read for more details.

First version of memcache stats script based on memcache.php

My work requires me to work more on performance optimizations so i need to do some home work from time to time :-) lately i started getting deeper into memcache to learn its internal structure and algorithms used. I know i wont make survive without a simple and quick way of checking status and statistics of my memcache. As a result of last weekend i am happy to share a nice stats tool.

Enjoy.

Excluding parts of PHP code from code coverage report

A funny thing happened to me the other day when i noticed a new operator (adnotation) supported by php unit.

May 2009, Artur Ejsmont

How to convert drupal 6 site from postgres to mysql

I have a project that was set up on drupal 6 on postgres database as i always prefered it over Mysql.

I am currently in progress of moving some stuff to dreamhost.com (which RULES!) and i finally have shell access and i can actually do stuff. The only problem is they dont support pgsql at all! LOL! So i have decided that it takes too much effort and time to maintain it.

Here are 3 simple steps to migrate your drupal installation (possibly other apps) from postgres to mysql.

Static methods and variables in PHP are bad - they are a real pain in the test!

First of all, i was never a fan of static calls and variables as i always thought they cause problems. Using a lot of statics you might experience values leaking through different parts of application and you cant isolate functions (cron/testing/overrided environments). In general the encapsulation is gone and coupling explodes.

What i did not realize until this year, was the fact how difficult it is to create new unit tests for code filled with static calls and references. Its just impossible to deal with code that calls stuff like this:

self::myMethod();
ClassName::method();

What is even worse you might get direct access to static variables which looks like this:

self::$value;
ClassName::$value;
Apr 2009, Artur Ejsmont

How to enable apc extension under command line php interface (Debian and Ubuntu)

PHP has a very useful extension for caching op codes as well as custom values from php code. The problem is that it does need additional parameter to be enabled in the command line interface (unit tests or cron scripts).

Apr 2009, Artur Ejsmont

Missing Soap extension in command line PHP cli in Xampp on Mac OS

Seems that creators of xampp did not include the soap extension in the cli executable. Its not available as module either so we have to compile it ourselves. Notes inside.

Apr 2009, Artur Ejsmont

Drupal clone is just a shallow copy - deep copy VS shallow copy in PHP

Drupal has in its api a drupal_clone function. The only problem with it is that people could be under impression it gives them a deep copy of objects tree. It does not its only cloning the first object. Everything else is still oryginal.

Apr 2009, Artur Ejsmont

Problems with older versions of memcache and get array - segmentation fault

I used to have some strange problems with memcache extensions compiled into the mac Xampp bundle. Few details inside.

Apr 2009, Artur Ejsmont

Xdebug is sometimes better than Zend Debugger

I worked lately a lot with both zend debugger and xdebug and to be honest the profiler included in Zend Debugger is worse than xdebug. Some more details below.

Syndicate content
Artur Ejsmont Travel Gallery and Computer Blog   |   Dublin 2007-2009   |   All rights reserved   |
digg stumble slashdot furl delicious reddit netscape google yahoo newsvine spurl facebook mixx netvouz blinklist