Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
CACHING
MEMCACHED vs. REDIS vs. FILESYSTEM
Brown Bag Session
Rafi Adnan
Faisal Islam
What is caching in web
application?
Web application caching is the process of storing dynamically generated
data for reuse...
How Caching Works
Traditional Data Query
(Without Cache)
Query
Get Rows From
Database
Return Rows to
Application
Cached Data Query
Query
Data
In
Cache
Get Rows
From Database
Return Row to
Application
Insert Rows
Into
Cache
Cache
Storag...
Why we use?
➔ Better performance
◆ System loads faster/better responsiveness
➔ Better scalability
◆ Limit bottlenecks in a...
When to use?
Profile your application to find operations
that consume significant CPU time and/or
memory
Don’t be premature
Caching
Terminology
[ Cache Hit ]
when requested data is contained in the cache
[ Cache Miss ]
when requested not in the c...
Advantages
❖ Speed up application
❖ Less Resources Used
❖ Reuse content
Disadvantages
❖ Stale Data
❖ Overhead
❖ Complexity
Type Of Caching
❑ Client Caching
- browser caches URLs for future uses
- Mozilla Firefox, Google Chrome
❑ Proxy Caching
- ...
File Cache
A file of data on a local hard drive. When
downloaded data are temporarily stored on the
user's local disk or o...
In-Memory means
We are Bound By RAM
MemCached
Memcached is an in-memory key-
value store for small chunks of
arbitrary data (strings, objects) from
results of...
Redis
Redis is an open source, advanced key-
value store. It is often referred to as a
"data structure server" since keys ...
How to use?
[ File System Caching ]
➔ It works without any
additional server. Most of the
CMS by default use file cache
wi...
In-Memory Cache ( Server - Client )
Installing memcached server (ubuntu):
sudo apt-get install memcached
Installing memcac...
MemCached
➔ Dead Simple & Battle Tested
➔ Fast
➔ Non-Blocking get()/set()
➔ Multi-Threaded
➔ Consistent Hashing
Code Example - Memcached
$memCached = new Memcached();
//connect with memcached server
$memCached->addServer('127.0.0.1', ...
Code Example - Memcached
$memCached = new Memcached();
//connect with memcached server
$memCached->addServer('127.0.0.1', ...
What if I don't want
all the data?
➔ What if I just want the name?
➔ 64 bytes for the object vs 16 bytes for just the name...
Redis: Data Types
➔ Strings ( just like Memcached )
➔ Lists
➔ Sets
➔ Sorted Sets
➔ Hashes
Redis : Lists
➔ Stored in sorted order
➔ Can push/pop
➔ Fast head/tail access
➔ Index access
Code Example - Redis : Lists
$redisClient = new PredisClient();
$redisClient->lpush('employees', 'Faisal');
$redisClient->...
Redis : Sets
➔ Unordered collections of strings
➔ Unique ( no repeated members )
➔ diff, intersect, merge
Code Example - Redis : Sets
$redisClient = new PredisClient();
$redisClient->sadd('employees', 'Rafi Adnan');
$redisClient...
Code Example - Redis : Hashes
$redisClient = new PredisClient();
$redisClient->hset('employees', 'teamCount', 10);
$redisC...
Redis: The Bad
➔ Single-Threaded
➔ Limited client support for consistent hashing
➔ Significant overhead for persistence
➔ ...
Memcached vs. Redis
Memcached Redis
(multi) get ✔ ✔
(multi) set ✔ ✔
increment/decrement ✔ ✔
delete ✔ ✔
expiration ✔ ✔
rang...
Die;
Questions, comments...
Nächste SlideShare
Wird geladen in …5
×

Caching

3.656 Aufrufe

Veröffentlicht am

A brief overview of caching mechanisms in a web application. Taking a look at the different layers of caching and how to utilize them in a PHP code base. We also compare Redis and MemCached discussing their advantages and disadvantages.

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

Caching

  1. 1. CACHING MEMCACHED vs. REDIS vs. FILESYSTEM Brown Bag Session Rafi Adnan Faisal Islam
  2. 2. What is caching in web application? Web application caching is the process of storing dynamically generated data for reuse and leaving data closer to the end user.
  3. 3. How Caching Works
  4. 4. Traditional Data Query (Without Cache) Query Get Rows From Database Return Rows to Application
  5. 5. Cached Data Query Query Data In Cache Get Rows From Database Return Row to Application Insert Rows Into Cache Cache StorageDatabase Yes No
  6. 6. Why we use? ➔ Better performance ◆ System loads faster/better responsiveness ➔ Better scalability ◆ Limit bottlenecks in a system ➔ Better robustness ◆ Can support more load
  7. 7. When to use? Profile your application to find operations that consume significant CPU time and/or memory Don’t be premature
  8. 8. Caching Terminology [ Cache Hit ] when requested data is contained in the cache [ Cache Miss ] when requested not in the cached, has to be recomputed or fetched from original storage [ Cache Key ] unique identifier for a data item in the cache [ Expiration ] item expires at a specific date (absolute), specifies how long after an item was last accessed that is expires (sliding) [ Backing store ] persist cached data on disk [ Cache Scavenging ] deleting items from the cache when memory is scarce [ Local Cache ] caching data on clients rather than on servers [ Distributed Cache ] extension of the traditional concept of cache that may span multiple servers
  9. 9. Advantages ❖ Speed up application ❖ Less Resources Used ❖ Reuse content Disadvantages ❖ Stale Data ❖ Overhead ❖ Complexity
  10. 10. Type Of Caching ❑ Client Caching - browser caches URLs for future uses - Mozilla Firefox, Google Chrome ❑ Proxy Caching - proxy server caches most requested URLs - Varnish ( reverse proxy ) - CDN ( forward proxy ) ❑ Server-side Caching - server side cache reduces load on server - File System Cache - In-Memory Cache (MemCached, Redis)
  11. 11. File Cache A file of data on a local hard drive. When downloaded data are temporarily stored on the user's local disk or on a local network disk, it speeds up retrieval the next time the user wants that same data (Web page, graphic, etc.) from the Internet or other remote source.
  12. 12. In-Memory means We are Bound By RAM
  13. 13. MemCached Memcached is an in-memory key- value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
  14. 14. Redis Redis is an open source, advanced key- value store. It is often referred to as a "data structure server" since keys can contain strings, hashes, lists, sets and sorted sets.
  15. 15. How to use? [ File System Caching ] ➔ It works without any additional server. Most of the CMS by default use file cache without any configurations. [ In-Memory Caching ] ➔ Need to install and configure cache server ➔ Need client library to access cache ➔ Most of the popular framework has very good built-in or third-party library.
  16. 16. In-Memory Cache ( Server - Client ) Installing memcached server (ubuntu): sudo apt-get install memcached Installing memcached client(ubuntu): PHP : sudo apt-get install php5- memcached Ruby on Rails : Popular gem called Dalli (https://github.com/mperham/dalli ) Installing redis server (ubuntu): sudo apt-get install redis-server Redis client 3rd-party packages: PHP : Predis , phpredis Ruby on Rails : redis-rb, em-hiredis, redic Python : redis-py, txredisapi, brukva (http://redis.io/clients)
  17. 17. MemCached ➔ Dead Simple & Battle Tested ➔ Fast ➔ Non-Blocking get()/set() ➔ Multi-Threaded ➔ Consistent Hashing
  18. 18. Code Example - Memcached $memCached = new Memcached(); //connect with memcached server $memCached->addServer('127.0.0.1', '11211'); const INT_EXPIRATION_TIME_IN_SECONDS = 2; //Set cache $memCached->set('key_1', 'serialize data', INT_EXPIRATION_TIME_IN_SECONDS); //get cache //if call unique_key after 2 seconds it will return false $uniqueKeyValue = $memCached->get('key_1'); var_export($uniqueKeyValue); Output : 'serialize data' Client Port Server Addr
  19. 19. Code Example - Memcached $memCached = new Memcached(); //connect with memcached server $memCached->addServer('127.0.0.1', '11211'); //consider our data set below $employeeId = 1000; $employeeData = array( 'name' => 'Rafi Adnan', 'designation' => 'Software Engineer', 'joining_date' => '2014-09-15' ); $memCached->set($employeeId, json_encode($employeeData)); // Returns employee details as json $employeeDetail = $memCached->get($employeeId); var_export($employeeDetail); Output : { "name":"Rafi Adnan", "designation":"Software Engineer", "joining_date":"2014-09-15" }
  20. 20. What if I don't want all the data? ➔ What if I just want the name? ➔ 64 bytes for the object vs 16 bytes for just the name? ➔ 4X network traffic ➔ More work for the application
  21. 21. Redis: Data Types ➔ Strings ( just like Memcached ) ➔ Lists ➔ Sets ➔ Sorted Sets ➔ Hashes
  22. 22. Redis : Lists ➔ Stored in sorted order ➔ Can push/pop ➔ Fast head/tail access ➔ Index access
  23. 23. Code Example - Redis : Lists $redisClient = new PredisClient(); $redisClient->lpush('employees', 'Faisal'); $redisClient->lpush('employees', 'Rafi'); $arrEmployeeList = $redisClient->lrange('employees', 0, -1); var_export($arrEmployeeList); $redisClient->rpush('employees', 'Akhtar'); $arrEmployeeList = $redisClient->lrange('employees', 0, -1); var_export($arrEmployeeList); array ( 0 => 'Rafi', 1 => 'Faisal', ) array ( 0 => 'Rafi', 1 => 'Faisal', 2 => 'Akhtar', )
  24. 24. Redis : Sets ➔ Unordered collections of strings ➔ Unique ( no repeated members ) ➔ diff, intersect, merge
  25. 25. Code Example - Redis : Sets $redisClient = new PredisClient(); $redisClient->sadd('employees', 'Rafi Adnan'); $redisClient->sadd('employees', 'Saeed Ahmed'); $redisClient->sadd('employees', 'Faisal Islam'); $redisClient->sadd('formerEmployees', 'Saeed Ahmed'); $currentEmployees = $redisClient->sdiff('employees', 'formerEmployees'); var_export($currentEmployees); array ( 0 => 'Rafi Adnan', 1 => 'Faisal Islam', )
  26. 26. Code Example - Redis : Hashes $redisClient = new PredisClient(); $redisClient->hset('employees', 'teamCount', 10); $redisClient->hset('employees', 'rubyTeam', 9); $redisClient->hset('employees', 'phpTeam', 1); $teamCount = $redisClient->hget('employees', 'teamCount'); var_export($teamCount); $teamStatus = $redisClient->hgetall('employees'); var_export($teamStatus); array ( 'teamCount' => '10', 'rubyTeam' => '9', 'phpTeam' => '1', ) ‘10’ - string
  27. 27. Redis: The Bad ➔ Single-Threaded ➔ Limited client support for consistent hashing ➔ Significant overhead for persistence ➔ Not widely deployed
  28. 28. Memcached vs. Redis Memcached Redis (multi) get ✔ ✔ (multi) set ✔ ✔ increment/decrement ✔ ✔ delete ✔ ✔ expiration ✔ ✔ range queries ✔ data types ✔ persistence - ✔ multi-threaded ✔ replication - ✔
  29. 29. Die; Questions, comments...

×