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.
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.
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. When to use?
Profile your application to find operations
that consume significant CPU time and/or
memory
Don’t be premature
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. Advantages
❖ Speed up application
❖ Less Resources Used
❖ Reuse content
Disadvantages
❖ Stale Data
❖ Overhead
❖ Complexity
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. 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.
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. 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. 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. 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. MemCached
➔ Dead Simple & Battle Tested
➔ Fast
➔ Non-Blocking get()/set()
➔ Multi-Threaded
➔ Consistent Hashing
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. 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. 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. Redis: Data Types
➔ Strings ( just like Memcached )
➔ Lists
➔ Sets
➔ Sorted Sets
➔ Hashes
22. Redis : Lists
➔ Stored in sorted order
➔ Can push/pop
➔ Fast head/tail access
➔ Index access