I have a Facebook application with 250K DAU. I use Memcached to cache the files and MySql selects because without that my server is going crazy.
I have set up the code like this:
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
// FOR FILES
$doc = $memcache->get('mem_index_html');
if ($doc == null)
{
$doc = new Document( HTML.'index.html' );
$memcache->set('mem_index_html', $doc, 0, 86400);
}
// FOR SQL
$sql=sprintf('count(qtn_id) FROM tb_questions where qtn_lang="EN"));
$total_pages = $memcache->get($sql);
if ($total_pages == null)
{
$query_id = DB::query($sql);
list( $total_pages ) = DB::nextRow( $query_id );
DB::free( $query_id );
$memcache->set($sql, $total_pages, 0, 86400);
}
The problem is that after some time – usually 24 hours, errors are starting to show. No log is created but I receive a timeouts if I try to reload the page. I assume that there is a lots of traffic in that moments, the Memcached is not working correct and everything is slowing down.
My temporary solution is to Flush the memcache - $memcache->flush(),
but that is not good solution.
Am I doing something wrong? Is there something to optimize or I don’t know. I found searching thru web this http://code.google.com/p/memcached/wiki/TutorialCachingStory about making more that one Memecached servers. Is that the best solution?
Thanks for your answers.
Darko