SlideShare a Scribd company logo
1 of 77
INTRODUCTION
     TO
 MEMCACHED
Tags
memcached,
performance,
scalability, php,
mySQL, caching
techniques, #ikdoeict
jurriaanpersyn.com
lead web dev at Netlog
since 4 years
php + mysql +
frontend
working on Gatcha
For who?
talk for students
professional bachelor
ICT www.ikdoeict.be
Why this talk?
One of the first things
I’ve learnt at Netlog.
Using it every single
day.
Program
- About caching
- About memcached
- Examples
- Tips & tricks
- Toolsets and other
  solutions
What is caching?
A copy of real data
with faster (and/or
cheaper) access
What is caching?



 • From Wikipedia: "A cache is a collection of
   data duplicating original values stored
   elsewhere or computed earlier, where the
   original data is expensive to fetch (owing
   to longer access time) or to compute,
   compared to the cost of reading the
   cache."

 • Term introducted by IBM in the 60’s
The anatomy



 • simple key/value storage
 • simple operations
  • save
  • get
  • delete
Terminology


 •   storage cost

 •   retrieval cost (network load / algorithm load)

 •   invalidation (keeping data up to date / removing
     irrelevant data)

 •   replacement policy (FIFO/LFU/LRU/MRU/RANDOM
     vs. Belady’s algorithm)

 •   cold cache / warm cache
Terminology


 •   cache hit and cache miss

 •   typical stats:

     •   hit ratio (hits / hits + misses)

     •   miss ratio (1 - hit ratio)



     •   45 cache hits and 10 cache misses

         •   45/(45+10) = 82% hit ratio

         •   18% miss ratio
When to cache?



 • caches are only efficient when the benefits
   of faster access outweigh the overhead of
   checking and keeping your cache up to
   date

   • more cache hits then cache misses
Where are caches used?



 • at hardware level (cpu, hdd)
 • operating systems (ram)
 • web stack
 • applications
 • your own short term vs long term memory
Caches in the web stack



 • Browser cache
 • DNS cache
 • Content Delivery Networks (CDN)
 • Proxy servers
 • Application level
  • full output caching
     plugin)
                          (eg. Wordpress WP-Cache



   • ...
Caches in the web stack (cont’d)



 • Application level
  • opcode cache (APC)
  • query cache (MySQL)
  • storing denormalized results in the
     database

   • object cache
   • storing values in php objects/classes
Efficiency of caching?



 • the earlier in the process, the closer to the
    original request(er), the faster
   •   browser cache will be faster then cache on a proxy


 • but probably also the harder to get it
    right
   •   the closer to the requester the more parameters the cache
       depends on
What to cache on the server-side?



 • As PHP backend developer, what to cache?
  • expensive operations: operations that
     work with slower resources

     • database access
     • reading files(in fact, any filesystem access)


     • API calls
     • Heavy computations
     • XML
Where to cache on the server-side?



 • As PHP backend developer, where to store
   cache results?

     • in database (computed values,
       generated html)
       •   you’ll still need to access your database


     • in static files (generated html or
       serialized php values)
       •   you’ll still need to access your file system
in memory!
memcached
About memcached



 • Free & open source, high-performance,
   distributed memory object caching system

 • Generic in nature, intended for use in
   speeding up dynamic web applications by
   alleviating database load.

 • key/value dictionary
About memcached (cont’d)



 • Developed by Brad Fitzpatrick for
   LiveJournal in 2003

 • Now used by Netlog, Facebook, Flickr,
   Wikipedia, Twitter, YouTube ...
Technically



 • It’s a server
 • Client access over TCP or UDP
 • Servers can run in pools
  • eg. 3 servers with 64GB mem each give
     you a single pool of 192GB storage for
     caching

   • Servers are independent, clients manage
     the pool
What to store in memcache?



 • high demand (used often)
 • expensive (hard to compute)
 • common (shared accross users)
 • Best? All three
What to store in memcache? (cont’d)



 • Typical:
  • user sessions (often)
  • user data (often, shared)
  • homepage data (eg. often, shared,
     expensive)
What to store in memcache? (cont’d)



 • Workflow:
  • monitor application (query logs /
     profiling)

   • add a caching level
   • compare speed gain
Memcached principles



 • Fast network access (memcached servers close
   to other application servers)

 • Nomemcached is gone) server goes down, data
   in
      persistency (if your


 • No redundancy / fail-over
 • No replication (single item in cache lives on one
   server only)

 • No authentication (not in shared environments)
Memcached principles (cont’d)



 • 1 key is maximum 1MB
 • keys are strings of 250 characters (in
   application typically MD5 of user readable
   string)

 • No enumeration of keys (thus no list of
   valid keys in cache at certain moment, list
   of keys beginnen with “user_”, ...)

 • No active clean-up (only clean up when
   more space needed, LRU)
$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get foo
VALUE foo 0 2
hi
END
stats
STAT pid 8861
(etc)
Client Access



 • both ASCII as Binary protocol
 • in real life:
  • clients available for all major languages
     • C, C++, PHP, Python, Ruby, Java, Perl,
       Windows, ...
PHP Clients



 • Support the basics such as multiple
   servers, setting values, getting values,
   incrementing, decrementing and getting
   stats.

 • pecl/memcache
 • pecl/memcached
  • newer, in beta, a couple more features
PHP Client Comparison


                
    
    
     pecl/memcache
       
   
   pecl/memcached
 First Release Date
 
    
     2004-06-08
 
        
   2009-01-29 (beta)
 Actively Developed?
     
     
    Yes
 
  
       
   
   Yes
 External Dependency
     
     
    None
   
       
   
   
    libmemcached
 
     Features
 Automatic Key Fixup
     
     
    Yes
 
   
      
    
    No
 Append/Prepend
 
        
     No
 
     
   
      
    Yes
 Automatic Serialzation2
 
     Yes
 
    
   
      
    Yes
 Binary Protocol
    
    
     
    No
 
    
      
    
    Optional
 CAS
 
     
     
  
    
     No
 
     
   
      
    Yes
 Compression
 
      
    
     Yes
 
    
   
      
    Yes
 Communication Timeout
   
     Connect Only
 
      
    Various Options
 Consistent Hashing
 
    
     Yes
 
    
   
      
    Yes
 Delayed Get
 
      
    
     No
 
     
   
      
    Yes
 Multi-Get
 
     
  
    
     Yes
 
    
   
      
    Yes
 Session Support
    
    
     
    Yes
 
   
      
    
    Yes
 Set/Get to a specific server
   
    No
 
    
      
    
    Yes
 Stores Numerics
    
    
     
    Converted to   Strings
   
    Yes
PHP Client functions


 •   Memcached::add — Add an item under a new key
 •   Memcached::addServer — Add a server to the server
     pool
 •   Memcached::decrement — Decrement numeric item's
     value
 •   Memcached::delete — Delete an item
 •   Memcached::flush — Invalidate all items in the cache
 •   Memcached::get — Retrieve an item
 •   Memcached::getMulti — Retrieve multiple items
 •   Memcached::getStats — Get server pool statistics
 •   Memcached::increment — Increment numeric item's
     value
 •   Memcached::set — Store an item
 •   ...
Output caching



 • Pages with high load / expensive to
   generate

 • Very easy
 • Very fast
 • But: all the dependencies ...
  • language, css, template, logged in
     user’s details, ...
<?php

$html = $cache->get('mypage');
if (!$html)
{
	 ob_start();
	 echo "<html>";
	 // all the fancy stuff goes here
	 echo "</html>";
	 $html = ob_get_contents();
	 ob_end_clean();
	 $cache->set('mypage', $html);
}
echo $html;

?>
Data caching



 • on a lower level
 • easier to find all dependencies
 • ideal solution for offloading database
   queries

   • the database is almost always the
     biggest bottleneck in backend
     performance problems
<?php

function getUserData($UID)
{
	 $key = 'user_' . $UID;
	 $userData = $cache->get($key);
	 if (!$userData)
	 {
	 	 $queryResult = Database::query("SELECT * FROM USERS
WHERE uid = " . (int) $UID);
	 	 $userData = $queryResult->getRow();
	 	 $cache->set($userData);
	 }
	 return $userData;
}

?>
“There are only two
hard things in
Computer Science:
cache invalidation and
naming things.”
    Phil Karlton
Invalidation



 • Caching for a certain amount of time
  • eg. 10 minutes
  • don’t delete caches
  • thus: You can’t trust that data coming
     from cache is correct
Invalidation (cont’d)



   • Use: Great for summaries
    • Overview
    • Pages where it’s not that big a
        problem if data is a little bit out of dat
        (eg. search results)

   • Good for quick and dirty optimizations
Invalidation (cont’d)



 • Store forever, and expire on certain events
  • the userdata example
    • store userdata for ever
    • when user changes any of his
        preferences, throw cache away
Invalidation



   • Use:
    • data that is fetched more then it’s
       updated

     • where it’s critical the data is correct
   • Improvement: instead of delete on
     event, update cache on event. (Mind:
     race conditions. Cache invalidation
     always as close to original change as
     possible!)
Uses at Netlog



 • sessions (cross server)
 • database results (via database class, or
   object caching)

 • flooding checks
 • output caching (eg. for RSS feeds)
 • locks
<?php
function getUserData($UID)
{
	 $db = DB::getInstance();
	 $db->prepare("SELECT *
	 	 	 	 	 FROM USERS
	 	 	 	 	 WHERE uid = {UID}");
	 $db->assignInt('UID', $UID);
	 $db->execute();
	 return $db->getRow();
}
?>
<?php
function getUserData($UID)
{
	 $db = DB::getInstance();
	 $db->prepare("SELECT *
	 	 	 	 	 FROM USERS
	 	 	 	 	 WHERE uid = {UID}");
	 $db->assignInt('UID', $UID);
	 $db->setCacheTTL(0); // cache forever
	 $db->execute();
	 return $db->getRow();
}
?>
<?php
function getUserData($UID, $invalidateCache = false)
{
	 $db = DB::getInstance();
	 $db->prepare("SELECT *
	 	 	 	 	 FROM USERS
	 	 	 	 	 WHERE uid = {UID}");
	 $db->assignInt('UID', $UID);
	 $db->setCacheTTL(0); // cache forever
	 if ($invalidateCache)
	 {
	 	 return $db->invalidateCache();
	 }
	 $db->execute();
	 return $db->getRow();
}
?>
<?php
function updateUserData($UID, $data)
{
	 $db = DB::getInstance();
	 $db->prepare("UPDATE USERS
	 	 	 	 	 SET ...
	 	 	 	 	 WHERE uid = {UID}");

	 ...
	
	 getUserData($UID, true); // invalidate cache
	
	 return $result;
}
?>
<?php
function getLastBlogPosts($UID, $start = 0,
   	 $limit = 10, $invalidateCache = false)
{
	 $db = DB::getInstance();
	 $db->prepare("SELECT blogid
	 	 	 	 	 FROM BLOGS WHERE uid = {UID}
	 	 	 	 	 ORDER BY dateadd DESC LIMIT {start}, {limit}");
	
	 $start; $limit; $UID;
	 $db->setCacheTTL(0); // cache forever
	 if ($invalidateCache)
	 {
	 	 return $db->invalidateCache();
	 }
	 $db->execute();
	 return $db->getResults();
}
?>
<?php
function addNewBlogPost($UID, $data)
{
	 $db = DB::getInstance();
	 $db->prepare("INSERT INTO BLOGS
	 	 	 	 	 ...");
	 ...
	
   // invalidate caches
	 getLastBlogPosts($UID, 0, 10);
	 getLastBlogPosts($UID, 11, 20);
   ... // ???
	
	 return $result;
}
?>
<?php
function getLastBlogPosts($UID, $start = 0,
	 $limit = 10)
{
	 $cacheVersionNumber = CacheVersionNumbers::
	 	 get('lastblogsposts_' . $UID);
	 $db = DB::getInstance();
	 $db->prepare("SELECT blogid FROM ...");
	 ...
	 $db->setCacheVersionNumber($cacheVersionNumber);
	 $db->setCacheTTL(0); // cache forever
	 $db->execute();
	 return $db->getResults();
}
?>
<?php
class CacheVersionNumbers
{
	 public static function get($name)
	 {
	 	 $result = $cache->get('cvn_' . $name);
	 	 if (!$result)
	 	 {
	 	 	 $result = microtime() . rand(0, 1000);
	 	 	 $cache->set('cvn_' . $name, $result);
	 	 }
	 	 return $result;
	 }
	
	 public static function bump($name)
	 {
	 	 return $cache->delete('cvn_' . $name);
	 }
}
?>
<?php
function addNewBlogPost($UID, $data)
{
	 $db = DB::getInstance();
	 $db->prepare("INSERT INTO BLOGS
	 	 	 	 	 ...");

	 ...
	
	 CacheVersionNumbers::bump('lastblogsposts_' . $UID);
	
	 return $result;
}
?>
Query Caching (cont’d)



 • queries with JOIN and WHERE statements
   are harder to cache

   • often not easy to find the cache key on
     update/change events

   • solution: JOIN in PHP
Query Caching (cont’d)



 • queries with JOIN and WHERE statements
   are harder to cache

   • often not easy to find the cache key on
     update/change events

   • solution: JOIN in PHP

   • In following example: what if nickname
     of user changes?
<?php
$db = DB::getInstance();
$db->prepare("SELECT c.comment_message,
	 	 	 	 	 c.comment_date, u.nickname
	 	 	 	 FROM COMMENTS c
	 	 	 	 	 JOIN USERS u
	 	 	 	 	 ON u.uid = c.commenter_uid
        WHERE c.postid = {postID}");
...
?>
<?php
$db = DB::getInstance();
$db->prepare("SELECT c.comment_message,
	 	 	 	 	 c.comment_date
                       ,


	 	 	 	 	 c.commenter_uid AS uid
	 	 	 	 FROM COMMENTS c
	 	 	 	 WHERE c.postid = {postID}");
...
$comments = Users::addUserDetails($comments);
...
?>
<?php
...
public static function addUserDetails($array)
{
	 foreach($array as &$item)
	 {
	 	 $item = array_merge($item,
      self::getUserData($item['uid']));
          // assume high hit ratio
	 }
	 return $item;
}
...
?>
So?



 • Pro’s:
  • speed, duh.
  • queries get simpler (better for your db)
  • easier porting to key/value storage
      solutions

 • Cons:
  • You’re relying on memcached to be up
      and have good hit ratios
Multi-Get Optimisations



 • We reduced database access
 • Memcached is faster, but access to
   memcache still has it’s price

 • Solution: multiget
  • fetch multiple keys from memcached in
     one single call

   • result is array of items
Multi-Get Optimisations (cont’d)



 • back to addUserDetails example
 • find UID’s from array
 • multiget to memcached for details of
   UID’s

 • for UID’s without result, do a query
   •   SELECT ... FROM USERS WHERE uid IN (...)

   •   for each fetched user, store in cache

   •   worst case (no hits): 1 query


 • return merged cache/db results
Consistent Hashing



 • client is responsible for managing pool
  • hashes a certain key to a certain server
 • clients can be naïve: distribute keys on size
   of pool

 • if one server goes down, all keys will now be
   queried on other servers > cold cache

 • use a client with consistent hashing
   algorithms, so if server goes down, only data
   on that server gets lost
Memcached Statistics



 • available stats from servers include:
  • uptime, #calls (get/set/...), #hits (since
     uptime), #misses (since uptime)

 • no enumeration, no distinguishing on types
   of caches

 • add own logging / statistics to monitor
   effectiveness of your caching strategy
More tips ...



 • Be carefull when security matters.
    (Remember ‘no authentication’?)
   •   Working on authentication for memcached via SASL Auth
       Protocol

 • Caching is not an excuse not to do database
    tuning. (Remember cold cache?)

 • Make sure to write unit tests for your
    caching classes and places where you use it.
    (Debugging problems related to out-of-date
    cache data is hard and boring. Very boring.)
Libraries for memcached



 • Zend framework has Zend_Cache with
   support for a memcached backend

 • Wordpress has 3 plugins for working with
   memcached

 • all of the other major frameworks have some
   sort of support (built in or via plugins):
   Symfony, Django, CakePHP, Drupal, ...

 • Gear6: memcached servers in the cloud
memcached isn’t the only caching solution



 • memcachedb (persistent memcached)
 • opcode caching
  • APC (php compiled code cache, usable for
     other purposes too)

   • xCache
   • eAccelerator
   • Zend optimizer
Last thought



 • main bottleneck in php backends is
   database
   •   adding php servers is easier then scaling databases

 • a complete caching layer before your
   database layer solves a lot of performance
   and scalability issues
   •   but being able to scale takes more then memcached

   •   performance tuning, beginning with identifying the slowest
       and most used parts stays important, be it tuning of your
       php code, memcached calls or database queries
VELO PERS
FO R DE
ME
      R GA                High-score Handling
YOU                          Tournaments
                   A ME    Challenge builder
           IA LG            Achievements
       S OC
  OP
AT


 Got an idea for a game? Great!
Gatcha For Game Developers



Game tracking
Start game and end game calls results in accurate gameplay
tracking and allows us to show who is playing the game at any
given moment, compute popularity, target games.

High-scores
You push your high-score to our API, we do the hard work of
creating different types of leader boards and rankings.

Achievements
Pushing achievements reached in your game, just takes one API
call, no configuration needed.
Gatcha For Game Developers



Multiplayer Games
We run SmartFox servers that enable you to build real-time
multiplayer games, with e.g.. in game chat


coming:

Challenges & Tournaments
Allow your game players to challenge each other, or build
challenges & contests yourself.
Gatcha For Game Developers

                  How to integrate?
Flash Games
We offer wrapper for AS3 and AS2 games with full
implementation of our API

Unity3D Games

OpenSocial Games
Talk to the supported containers via the Gatcha OpenSocial
Extension

Other Games
Simple iframe implementation. PHP Client API available for the
Gatcha API

           Start developing in our sandbox.
Job openings



Weʼre searching for great developers!

  PHP Talents
  Working on integrations and the gaming platform


  Flash Developers
  Working on Flash Games and the gaming platform


  Design Artists
  Designing games and integrations
jurriaan@netlog.com
Resources, a.o.:
• memcached & apc: http://www.slideshare.net/benramsey/
  caching-with-memcached-and-apc
• speed comparison: http://dealnews.com/developers/
  memcachedv2.html
• php client comparison: http://code.google.com/p/memcached/
  wiki/PHPClientComparison
• cakephp-memcached: http://teknoid.wordpress.com/
  2009/06/17/send-your-database-on-vacation-by-using-
  cakephp-memcached/
• caching basics: http://www.slideshare.net/soplakanets/caching-
  basics
• caching w php: http://www.slideshare.net/JustinCarmony/
  effectice-caching-w-php-caching

More Related Content

What's hot

Scalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsJonas Bonér
 
Introduction to Apache ZooKeeper
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeperSaurav Haloi
 
Kafka replication apachecon_2013
Kafka replication apachecon_2013Kafka replication apachecon_2013
Kafka replication apachecon_2013Jun Rao
 
Stability Patterns for Microservices
Stability Patterns for MicroservicesStability Patterns for Microservices
Stability Patterns for Microservicespflueras
 
Handle Large Messages In Apache Kafka
Handle Large Messages In Apache KafkaHandle Large Messages In Apache Kafka
Handle Large Messages In Apache KafkaJiangjie Qin
 
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...HostedbyConfluent
 
Introduction to Apache Kafka
Introduction to Apache KafkaIntroduction to Apache Kafka
Introduction to Apache KafkaJeff Holoman
 
Etl is Dead; Long Live Streams
Etl is Dead; Long Live StreamsEtl is Dead; Long Live Streams
Etl is Dead; Long Live Streamsconfluent
 
SeaweedFS introduction
SeaweedFS introductionSeaweedFS introduction
SeaweedFS introductionchrislusf
 
A visual introduction to Apache Kafka
A visual introduction to Apache KafkaA visual introduction to Apache Kafka
A visual introduction to Apache KafkaPaul Brebner
 
Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021
Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021
Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021StreamNative
 
Kafka Tutorial - Introduction to Apache Kafka (Part 1)
Kafka Tutorial - Introduction to Apache Kafka (Part 1)Kafka Tutorial - Introduction to Apache Kafka (Part 1)
Kafka Tutorial - Introduction to Apache Kafka (Part 1)Jean-Paul Azar
 
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016DataStax
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinC* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinDataStax Academy
 
[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)NAVER D2
 
Producer Performance Tuning for Apache Kafka
Producer Performance Tuning for Apache KafkaProducer Performance Tuning for Apache Kafka
Producer Performance Tuning for Apache KafkaJiangjie Qin
 

What's hot (20)

Scalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability Patterns
 
Introduction to Apache ZooKeeper
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeper
 
Apache Spark Architecture
Apache Spark ArchitectureApache Spark Architecture
Apache Spark Architecture
 
Kafka replication apachecon_2013
Kafka replication apachecon_2013Kafka replication apachecon_2013
Kafka replication apachecon_2013
 
Stability Patterns for Microservices
Stability Patterns for MicroservicesStability Patterns for Microservices
Stability Patterns for Microservices
 
Handle Large Messages In Apache Kafka
Handle Large Messages In Apache KafkaHandle Large Messages In Apache Kafka
Handle Large Messages In Apache Kafka
 
Apache Kafka Best Practices
Apache Kafka Best PracticesApache Kafka Best Practices
Apache Kafka Best Practices
 
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
 
Introduction to Apache Kafka
Introduction to Apache KafkaIntroduction to Apache Kafka
Introduction to Apache Kafka
 
Etl is Dead; Long Live Streams
Etl is Dead; Long Live StreamsEtl is Dead; Long Live Streams
Etl is Dead; Long Live Streams
 
Prometheus 101
Prometheus 101Prometheus 101
Prometheus 101
 
SeaweedFS introduction
SeaweedFS introductionSeaweedFS introduction
SeaweedFS introduction
 
A visual introduction to Apache Kafka
A visual introduction to Apache KafkaA visual introduction to Apache Kafka
A visual introduction to Apache Kafka
 
Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021
Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021
Trino: A Ludicrously Fast Query Engine - Pulsar Summit NA 2021
 
Kafka Tutorial - Introduction to Apache Kafka (Part 1)
Kafka Tutorial - Introduction to Apache Kafka (Part 1)Kafka Tutorial - Introduction to Apache Kafka (Part 1)
Kafka Tutorial - Introduction to Apache Kafka (Part 1)
 
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016
 
Kafka presentation
Kafka presentationKafka presentation
Kafka presentation
 
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadinC* Summit 2013: The World's Next Top Data Model by Patrick McFadin
C* Summit 2013: The World's Next Top Data Model by Patrick McFadin
 
[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)[211] HBase 기반 검색 데이터 저장소 (공개용)
[211] HBase 기반 검색 데이터 저장소 (공개용)
 
Producer Performance Tuning for Apache Kafka
Producer Performance Tuning for Apache KafkaProducer Performance Tuning for Apache Kafka
Producer Performance Tuning for Apache Kafka
 

Similar to Introduction to memcached

Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APCBen Ramsey
 
Improve WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of codeImprove WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of codeDanilo Ercoli
 
Where Django Caching Bust at the Seams
Where Django Caching Bust at the SeamsWhere Django Caching Bust at the Seams
Where Django Caching Bust at the SeamsConcentric Sky
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails applicationArrrrCamp
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep Dive(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep DiveAmazon Web Services
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Wim Godden
 
phptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialphptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialWim Godden
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...smallerror
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...xlight
 
Fixing twitter
Fixing twitterFixing twitter
Fixing twitterRoger Xia
 
Memcached B box presentation
Memcached B box presentationMemcached B box presentation
Memcached B box presentationNagesh Chinkeri
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Wim Godden
 
Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World DominationcPanel
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 

Similar to Introduction to memcached (20)

Top ten-list
Top ten-listTop ten-list
Top ten-list
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
 
Improve WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of codeImprove WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of code
 
Where Django Caching Bust at the Seams
Where Django Caching Bust at the SeamsWhere Django Caching Bust at the Seams
Where Django Caching Bust at the Seams
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails application
 
Drupal performance
Drupal performanceDrupal performance
Drupal performance
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep Dive(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep Dive
 
Mini-Training: To cache or not to cache
Mini-Training: To cache or not to cacheMini-Training: To cache or not to cache
Mini-Training: To cache or not to cache
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011
 
phptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialphptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorial
 
AppFabric Velocity
AppFabric VelocityAppFabric Velocity
AppFabric Velocity
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
 
Fixing twitter
Fixing twitterFixing twitter
Fixing twitter
 
Fixing_Twitter
Fixing_TwitterFixing_Twitter
Fixing_Twitter
 
Memcached B box presentation
Memcached B box presentationMemcached B box presentation
Memcached B box presentation
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011
 
Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World Domination
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 

More from Jurriaan Persyn

An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.Jurriaan Persyn
 
Developing Social Games in the Cloud
Developing Social Games in the CloudDeveloping Social Games in the Cloud
Developing Social Games in the CloudJurriaan Persyn
 
Database Sharding At Netlog
Database Sharding At NetlogDatabase Sharding At Netlog
Database Sharding At NetlogJurriaan Persyn
 
Database Sharding at Netlog
Database Sharding at NetlogDatabase Sharding at Netlog
Database Sharding at NetlogJurriaan Persyn
 
Meet the OpenSocial Containers: Netlog
Meet the OpenSocial Containers: NetlogMeet the OpenSocial Containers: Netlog
Meet the OpenSocial Containers: NetlogJurriaan Persyn
 
Get Your Frontend Sorted (Barcamp Gent 2008)
Get Your Frontend Sorted (Barcamp Gent 2008)Get Your Frontend Sorted (Barcamp Gent 2008)
Get Your Frontend Sorted (Barcamp Gent 2008)Jurriaan Persyn
 

More from Jurriaan Persyn (7)

An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.
 
Engagor Walkthrough
Engagor WalkthroughEngagor Walkthrough
Engagor Walkthrough
 
Developing Social Games in the Cloud
Developing Social Games in the CloudDeveloping Social Games in the Cloud
Developing Social Games in the Cloud
 
Database Sharding At Netlog
Database Sharding At NetlogDatabase Sharding At Netlog
Database Sharding At Netlog
 
Database Sharding at Netlog
Database Sharding at NetlogDatabase Sharding at Netlog
Database Sharding at Netlog
 
Meet the OpenSocial Containers: Netlog
Meet the OpenSocial Containers: NetlogMeet the OpenSocial Containers: Netlog
Meet the OpenSocial Containers: Netlog
 
Get Your Frontend Sorted (Barcamp Gent 2008)
Get Your Frontend Sorted (Barcamp Gent 2008)Get Your Frontend Sorted (Barcamp Gent 2008)
Get Your Frontend Sorted (Barcamp Gent 2008)
 

Recently uploaded

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 

Recently uploaded (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 

Introduction to memcached

  • 1. INTRODUCTION TO MEMCACHED
  • 3. jurriaanpersyn.com lead web dev at Netlog since 4 years php + mysql + frontend working on Gatcha
  • 4. For who? talk for students professional bachelor ICT www.ikdoeict.be
  • 5. Why this talk? One of the first things I’ve learnt at Netlog. Using it every single day.
  • 6. Program - About caching - About memcached - Examples - Tips & tricks - Toolsets and other solutions
  • 7. What is caching? A copy of real data with faster (and/or cheaper) access
  • 8. What is caching? • From Wikipedia: "A cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (owing to longer access time) or to compute, compared to the cost of reading the cache." • Term introducted by IBM in the 60’s
  • 9. The anatomy • simple key/value storage • simple operations • save • get • delete
  • 10. Terminology • storage cost • retrieval cost (network load / algorithm load) • invalidation (keeping data up to date / removing irrelevant data) • replacement policy (FIFO/LFU/LRU/MRU/RANDOM vs. Belady’s algorithm) • cold cache / warm cache
  • 11. Terminology • cache hit and cache miss • typical stats: • hit ratio (hits / hits + misses) • miss ratio (1 - hit ratio) • 45 cache hits and 10 cache misses • 45/(45+10) = 82% hit ratio • 18% miss ratio
  • 12. When to cache? • caches are only efficient when the benefits of faster access outweigh the overhead of checking and keeping your cache up to date • more cache hits then cache misses
  • 13. Where are caches used? • at hardware level (cpu, hdd) • operating systems (ram) • web stack • applications • your own short term vs long term memory
  • 14. Caches in the web stack • Browser cache • DNS cache • Content Delivery Networks (CDN) • Proxy servers • Application level • full output caching plugin) (eg. Wordpress WP-Cache • ...
  • 15. Caches in the web stack (cont’d) • Application level • opcode cache (APC) • query cache (MySQL) • storing denormalized results in the database • object cache • storing values in php objects/classes
  • 16. Efficiency of caching? • the earlier in the process, the closer to the original request(er), the faster • browser cache will be faster then cache on a proxy • but probably also the harder to get it right • the closer to the requester the more parameters the cache depends on
  • 17. What to cache on the server-side? • As PHP backend developer, what to cache? • expensive operations: operations that work with slower resources • database access • reading files(in fact, any filesystem access) • API calls • Heavy computations • XML
  • 18. Where to cache on the server-side? • As PHP backend developer, where to store cache results? • in database (computed values, generated html) • you’ll still need to access your database • in static files (generated html or serialized php values) • you’ll still need to access your file system
  • 21. About memcached • Free & open source, high-performance, distributed memory object caching system • Generic in nature, intended for use in speeding up dynamic web applications by alleviating database load. • key/value dictionary
  • 22. About memcached (cont’d) • Developed by Brad Fitzpatrick for LiveJournal in 2003 • Now used by Netlog, Facebook, Flickr, Wikipedia, Twitter, YouTube ...
  • 23. Technically • It’s a server • Client access over TCP or UDP • Servers can run in pools • eg. 3 servers with 64GB mem each give you a single pool of 192GB storage for caching • Servers are independent, clients manage the pool
  • 24. What to store in memcache? • high demand (used often) • expensive (hard to compute) • common (shared accross users) • Best? All three
  • 25. What to store in memcache? (cont’d) • Typical: • user sessions (often) • user data (often, shared) • homepage data (eg. often, shared, expensive)
  • 26. What to store in memcache? (cont’d) • Workflow: • monitor application (query logs / profiling) • add a caching level • compare speed gain
  • 27. Memcached principles • Fast network access (memcached servers close to other application servers) • Nomemcached is gone) server goes down, data in persistency (if your • No redundancy / fail-over • No replication (single item in cache lives on one server only) • No authentication (not in shared environments)
  • 28. Memcached principles (cont’d) • 1 key is maximum 1MB • keys are strings of 250 characters (in application typically MD5 of user readable string) • No enumeration of keys (thus no list of valid keys in cache at certain moment, list of keys beginnen with “user_”, ...) • No active clean-up (only clean up when more space needed, LRU)
  • 29. $ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. get foo VALUE foo 0 2 hi END stats STAT pid 8861 (etc)
  • 30. Client Access • both ASCII as Binary protocol • in real life: • clients available for all major languages • C, C++, PHP, Python, Ruby, Java, Perl, Windows, ...
  • 31. PHP Clients • Support the basics such as multiple servers, setting values, getting values, incrementing, decrementing and getting stats. • pecl/memcache • pecl/memcached • newer, in beta, a couple more features
  • 32. PHP Client Comparison pecl/memcache pecl/memcached First Release Date 2004-06-08 2009-01-29 (beta) Actively Developed? Yes Yes External Dependency None libmemcached Features Automatic Key Fixup Yes No Append/Prepend No Yes Automatic Serialzation2 Yes Yes Binary Protocol No Optional CAS No Yes Compression Yes Yes Communication Timeout Connect Only Various Options Consistent Hashing Yes Yes Delayed Get No Yes Multi-Get Yes Yes Session Support Yes Yes Set/Get to a specific server No Yes Stores Numerics Converted to Strings Yes
  • 33. PHP Client functions • Memcached::add — Add an item under a new key • Memcached::addServer — Add a server to the server pool • Memcached::decrement — Decrement numeric item's value • Memcached::delete — Delete an item • Memcached::flush — Invalidate all items in the cache • Memcached::get — Retrieve an item • Memcached::getMulti — Retrieve multiple items • Memcached::getStats — Get server pool statistics • Memcached::increment — Increment numeric item's value • Memcached::set — Store an item • ...
  • 34. Output caching • Pages with high load / expensive to generate • Very easy • Very fast • But: all the dependencies ... • language, css, template, logged in user’s details, ...
  • 35. <?php $html = $cache->get('mypage'); if (!$html) { ob_start(); echo "<html>"; // all the fancy stuff goes here echo "</html>"; $html = ob_get_contents(); ob_end_clean(); $cache->set('mypage', $html); } echo $html; ?>
  • 36. Data caching • on a lower level • easier to find all dependencies • ideal solution for offloading database queries • the database is almost always the biggest bottleneck in backend performance problems
  • 37. <?php function getUserData($UID) { $key = 'user_' . $UID; $userData = $cache->get($key); if (!$userData) { $queryResult = Database::query("SELECT * FROM USERS WHERE uid = " . (int) $UID); $userData = $queryResult->getRow(); $cache->set($userData); } return $userData; } ?>
  • 38. “There are only two hard things in Computer Science: cache invalidation and naming things.” Phil Karlton
  • 39. Invalidation • Caching for a certain amount of time • eg. 10 minutes • don’t delete caches • thus: You can’t trust that data coming from cache is correct
  • 40. Invalidation (cont’d) • Use: Great for summaries • Overview • Pages where it’s not that big a problem if data is a little bit out of dat (eg. search results) • Good for quick and dirty optimizations
  • 41. Invalidation (cont’d) • Store forever, and expire on certain events • the userdata example • store userdata for ever • when user changes any of his preferences, throw cache away
  • 42. Invalidation • Use: • data that is fetched more then it’s updated • where it’s critical the data is correct • Improvement: instead of delete on event, update cache on event. (Mind: race conditions. Cache invalidation always as close to original change as possible!)
  • 43. Uses at Netlog • sessions (cross server) • database results (via database class, or object caching) • flooding checks • output caching (eg. for RSS feeds) • locks
  • 44. <?php function getUserData($UID) { $db = DB::getInstance(); $db->prepare("SELECT * FROM USERS WHERE uid = {UID}"); $db->assignInt('UID', $UID); $db->execute(); return $db->getRow(); } ?>
  • 45. <?php function getUserData($UID) { $db = DB::getInstance(); $db->prepare("SELECT * FROM USERS WHERE uid = {UID}"); $db->assignInt('UID', $UID); $db->setCacheTTL(0); // cache forever $db->execute(); return $db->getRow(); } ?>
  • 46. <?php function getUserData($UID, $invalidateCache = false) { $db = DB::getInstance(); $db->prepare("SELECT * FROM USERS WHERE uid = {UID}"); $db->assignInt('UID', $UID); $db->setCacheTTL(0); // cache forever if ($invalidateCache) { return $db->invalidateCache(); } $db->execute(); return $db->getRow(); } ?>
  • 47. <?php function updateUserData($UID, $data) { $db = DB::getInstance(); $db->prepare("UPDATE USERS SET ... WHERE uid = {UID}"); ... getUserData($UID, true); // invalidate cache return $result; } ?>
  • 48. <?php function getLastBlogPosts($UID, $start = 0, $limit = 10, $invalidateCache = false) { $db = DB::getInstance(); $db->prepare("SELECT blogid FROM BLOGS WHERE uid = {UID} ORDER BY dateadd DESC LIMIT {start}, {limit}"); $start; $limit; $UID; $db->setCacheTTL(0); // cache forever if ($invalidateCache) { return $db->invalidateCache(); } $db->execute(); return $db->getResults(); } ?>
  • 49. <?php function addNewBlogPost($UID, $data) { $db = DB::getInstance(); $db->prepare("INSERT INTO BLOGS ..."); ... // invalidate caches getLastBlogPosts($UID, 0, 10); getLastBlogPosts($UID, 11, 20); ... // ??? return $result; } ?>
  • 50. <?php function getLastBlogPosts($UID, $start = 0, $limit = 10) { $cacheVersionNumber = CacheVersionNumbers:: get('lastblogsposts_' . $UID); $db = DB::getInstance(); $db->prepare("SELECT blogid FROM ..."); ... $db->setCacheVersionNumber($cacheVersionNumber); $db->setCacheTTL(0); // cache forever $db->execute(); return $db->getResults(); } ?>
  • 51. <?php class CacheVersionNumbers { public static function get($name) { $result = $cache->get('cvn_' . $name); if (!$result) { $result = microtime() . rand(0, 1000); $cache->set('cvn_' . $name, $result); } return $result; } public static function bump($name) { return $cache->delete('cvn_' . $name); } } ?>
  • 52. <?php function addNewBlogPost($UID, $data) { $db = DB::getInstance(); $db->prepare("INSERT INTO BLOGS ..."); ... CacheVersionNumbers::bump('lastblogsposts_' . $UID); return $result; } ?>
  • 53. Query Caching (cont’d) • queries with JOIN and WHERE statements are harder to cache • often not easy to find the cache key on update/change events • solution: JOIN in PHP
  • 54. Query Caching (cont’d) • queries with JOIN and WHERE statements are harder to cache • often not easy to find the cache key on update/change events • solution: JOIN in PHP • In following example: what if nickname of user changes?
  • 55. <?php $db = DB::getInstance(); $db->prepare("SELECT c.comment_message, c.comment_date, u.nickname FROM COMMENTS c JOIN USERS u ON u.uid = c.commenter_uid WHERE c.postid = {postID}"); ... ?>
  • 56. <?php $db = DB::getInstance(); $db->prepare("SELECT c.comment_message, c.comment_date , c.commenter_uid AS uid FROM COMMENTS c WHERE c.postid = {postID}"); ... $comments = Users::addUserDetails($comments); ... ?>
  • 57. <?php ... public static function addUserDetails($array) { foreach($array as &$item) { $item = array_merge($item, self::getUserData($item['uid'])); // assume high hit ratio } return $item; } ... ?>
  • 58. So? • Pro’s: • speed, duh. • queries get simpler (better for your db) • easier porting to key/value storage solutions • Cons: • You’re relying on memcached to be up and have good hit ratios
  • 59. Multi-Get Optimisations • We reduced database access • Memcached is faster, but access to memcache still has it’s price • Solution: multiget • fetch multiple keys from memcached in one single call • result is array of items
  • 60. Multi-Get Optimisations (cont’d) • back to addUserDetails example • find UID’s from array • multiget to memcached for details of UID’s • for UID’s without result, do a query • SELECT ... FROM USERS WHERE uid IN (...) • for each fetched user, store in cache • worst case (no hits): 1 query • return merged cache/db results
  • 61. Consistent Hashing • client is responsible for managing pool • hashes a certain key to a certain server • clients can be naïve: distribute keys on size of pool • if one server goes down, all keys will now be queried on other servers > cold cache • use a client with consistent hashing algorithms, so if server goes down, only data on that server gets lost
  • 62. Memcached Statistics • available stats from servers include: • uptime, #calls (get/set/...), #hits (since uptime), #misses (since uptime) • no enumeration, no distinguishing on types of caches • add own logging / statistics to monitor effectiveness of your caching strategy
  • 63. More tips ... • Be carefull when security matters. (Remember ‘no authentication’?) • Working on authentication for memcached via SASL Auth Protocol • Caching is not an excuse not to do database tuning. (Remember cold cache?) • Make sure to write unit tests for your caching classes and places where you use it. (Debugging problems related to out-of-date cache data is hard and boring. Very boring.)
  • 64. Libraries for memcached • Zend framework has Zend_Cache with support for a memcached backend • Wordpress has 3 plugins for working with memcached • all of the other major frameworks have some sort of support (built in or via plugins): Symfony, Django, CakePHP, Drupal, ... • Gear6: memcached servers in the cloud
  • 65. memcached isn’t the only caching solution • memcachedb (persistent memcached) • opcode caching • APC (php compiled code cache, usable for other purposes too) • xCache • eAccelerator • Zend optimizer
  • 66. Last thought • main bottleneck in php backends is database • adding php servers is easier then scaling databases • a complete caching layer before your database layer solves a lot of performance and scalability issues • but being able to scale takes more then memcached • performance tuning, beginning with identifying the slowest and most used parts stays important, be it tuning of your php code, memcached calls or database queries
  • 68. ME R GA High-score Handling YOU Tournaments A ME Challenge builder IA LG Achievements S OC OP AT Got an idea for a game? Great!
  • 69. Gatcha For Game Developers Game tracking Start game and end game calls results in accurate gameplay tracking and allows us to show who is playing the game at any given moment, compute popularity, target games. High-scores You push your high-score to our API, we do the hard work of creating different types of leader boards and rankings. Achievements Pushing achievements reached in your game, just takes one API call, no configuration needed.
  • 70. Gatcha For Game Developers Multiplayer Games We run SmartFox servers that enable you to build real-time multiplayer games, with e.g.. in game chat coming: Challenges & Tournaments Allow your game players to challenge each other, or build challenges & contests yourself.
  • 71. Gatcha For Game Developers How to integrate? Flash Games We offer wrapper for AS3 and AS2 games with full implementation of our API Unity3D Games OpenSocial Games Talk to the supported containers via the Gatcha OpenSocial Extension Other Games Simple iframe implementation. PHP Client API available for the Gatcha API Start developing in our sandbox.
  • 72.
  • 73.
  • 74.
  • 75. Job openings Weʼre searching for great developers! PHP Talents Working on integrations and the gaming platform Flash Developers Working on Flash Games and the gaming platform Design Artists Designing games and integrations
  • 77. Resources, a.o.: • memcached & apc: http://www.slideshare.net/benramsey/ caching-with-memcached-and-apc • speed comparison: http://dealnews.com/developers/ memcachedv2.html • php client comparison: http://code.google.com/p/memcached/ wiki/PHPClientComparison • cakephp-memcached: http://teknoid.wordpress.com/ 2009/06/17/send-your-database-on-vacation-by-using- cakephp-memcached/ • caching basics: http://www.slideshare.net/soplakanets/caching- basics • caching w php: http://www.slideshare.net/JustinCarmony/ effectice-caching-w-php-caching

Editor's Notes

  1. and of course, some shameless self promotion
  2. * i&amp;#x2019;m not a dba * i&amp;#x2019;m not a system administrator
  3. and of course, some shameless self promotion
  4. What&apos;s next then?
  5. What&apos;s next then?
  6. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  7. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  8. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  9. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  10. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  11. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  12. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  13. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  14. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  15. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  16. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  17. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  18. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  19. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  20. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance
  21. * started of - as all hobby projects * one single database (virtual server on shared hosting?) * all functionality one instance