SlideShare a Scribd company logo
1 of 29
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 and leaving data closer to the end user.
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
StorageDatabase
Yes
No
Why we use?
➔ Better performance
◆ System loads faster/better responsiveness
➔ Better scalability
◆ Limit bottlenecks in a system
➔ Better robustness
◆ Can support more load
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 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
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
- 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)
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.
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 database calls, API calls, or
page rendering.
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.
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.
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)
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', '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
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"
}
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
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->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',
)
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->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',
)
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
Redis: The Bad
➔ Single-Threaded
➔ Limited client support for consistent hashing
➔ Significant overhead for persistence
➔ Not widely deployed
Memcached vs. Redis
Memcached Redis
(multi) get ✔ ✔
(multi) set ✔ ✔
increment/decrement ✔ ✔
delete ✔ ✔
expiration ✔ ✔
range queries ✔
data types ✔
persistence - ✔
multi-threaded ✔
replication - ✔
Die;
Questions, comments...

More Related Content

What's hot

HTTP request and response
HTTP request and responseHTTP request and response
HTTP request and responseSahil Agarwal
 
Hadoop File system (HDFS)
Hadoop File system (HDFS)Hadoop File system (HDFS)
Hadoop File system (HDFS)Prashant Gupta
 
Distributed file system
Distributed file systemDistributed file system
Distributed file systemAnamika Singh
 
RESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web APIRESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web API💻 Spencer Schneidenbach
 
HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)Gurjot Singh
 
Introduction to Distributed System
Introduction to Distributed SystemIntroduction to Distributed System
Introduction to Distributed SystemSunita Sahu
 
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...rahul kundu
 
Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.Meghaj Mallick
 
Hypertext transfer protocol (http)
Hypertext transfer protocol (http)Hypertext transfer protocol (http)
Hypertext transfer protocol (http)Shimona Agarwal
 
Web servers – features, installation and configuration
Web servers – features, installation and configurationWeb servers – features, installation and configuration
Web servers – features, installation and configurationwebhostingguy
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 
File Transfer Protocol
File Transfer ProtocolFile Transfer Protocol
File Transfer Protocolguest029bcd
 
What is Server? (Web Server vs Application Server)
What is Server? (Web Server vs Application Server)What is Server? (Web Server vs Application Server)
What is Server? (Web Server vs Application Server)Amit Nirala
 
Distributed File Systems
Distributed File Systems Distributed File Systems
Distributed File Systems Maurvi04
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 

What's hot (20)

HTTP request and response
HTTP request and responseHTTP request and response
HTTP request and response
 
Hadoop File system (HDFS)
Hadoop File system (HDFS)Hadoop File system (HDFS)
Hadoop File system (HDFS)
 
Dns ppt
Dns pptDns ppt
Dns ppt
 
Distributed file system
Distributed file systemDistributed file system
Distributed file system
 
RESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web APIRESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web API
 
HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)
 
Webservices
WebservicesWebservices
Webservices
 
Introduction to Distributed System
Introduction to Distributed SystemIntroduction to Distributed System
Introduction to Distributed System
 
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
 
Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.
 
Hypertext transfer protocol (http)
Hypertext transfer protocol (http)Hypertext transfer protocol (http)
Hypertext transfer protocol (http)
 
Introduction to Web Services
Introduction to Web ServicesIntroduction to Web Services
Introduction to Web Services
 
Web servers – features, installation and configuration
Web servers – features, installation and configurationWeb servers – features, installation and configuration
Web servers – features, installation and configuration
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
File Transfer Protocol
File Transfer ProtocolFile Transfer Protocol
File Transfer Protocol
 
What is Server? (Web Server vs Application Server)
What is Server? (Web Server vs Application Server)What is Server? (Web Server vs Application Server)
What is Server? (Web Server vs Application Server)
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Distributed File Systems
Distributed File Systems Distributed File Systems
Distributed File Systems
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Dhcp
DhcpDhcp
Dhcp
 

Similar to Caching

Simple server side cache for Express.js with Node.js
Simple server side cache for Express.js with Node.jsSimple server side cache for Express.js with Node.js
Simple server side cache for Express.js with Node.jsGokusen Newz
 
Caching objects-in-memory
Caching objects-in-memoryCaching objects-in-memory
Caching objects-in-memoryMauro Cassani
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slidesmkherlakian
 
Zend Server Data Caching
Zend Server Data CachingZend Server Data Caching
Zend Server Data CachingEl Taller Web
 
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetImproving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetAchieve Internet
 
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetImproving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetAchieve Internet
 
Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012Combell NV
 
phptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialphptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialWim Godden
 
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
 
DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...
DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...
DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...DataStax
 
Continuity Software 4.3 Detailed Gaps
Continuity Software 4.3 Detailed GapsContinuity Software 4.3 Detailed Gaps
Continuity Software 4.3 Detailed GapsGilHecht
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统yiditushe
 
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
 
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...Edureka!
 
Develop and deploy using Hybrid Cloud Strategies confoo2012
Develop and deploy using Hybrid Cloud Strategies confoo2012Develop and deploy using Hybrid Cloud Strategies confoo2012
Develop and deploy using Hybrid Cloud Strategies confoo2012Combell NV
 
Caching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTourCaching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTourWim Godden
 
From limited Hadoop compute capacity to increased data scientist efficiency
From limited Hadoop compute capacity to increased data scientist efficiencyFrom limited Hadoop compute capacity to increased data scientist efficiency
From limited Hadoop compute capacity to increased data scientist efficiencyAlluxio, Inc.
 

Similar to Caching (20)

Simple server side cache for Express.js with Node.js
Simple server side cache for Express.js with Node.jsSimple server side cache for Express.js with Node.js
Simple server side cache for Express.js with Node.js
 
Caching objects-in-memory
Caching objects-in-memoryCaching objects-in-memory
Caching objects-in-memory
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slides
 
Zend Server Data Caching
Zend Server Data CachingZend Server Data Caching
Zend Server Data Caching
 
1
11
1
 
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetImproving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve Internet
 
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetImproving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve Internet
 
Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012Hybrid Cloud PHPUK2012
Hybrid Cloud PHPUK2012
 
phptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialphptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorial
 
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
 
DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...
DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...
DataStax | Building a Spark Streaming App with DSE File System (Rocco Varela)...
 
Continuity Software 4.3 Detailed Gaps
Continuity Software 4.3 Detailed GapsContinuity Software 4.3 Detailed Gaps
Continuity Software 4.3 Detailed Gaps
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
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
 
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
 
Develop and deploy using Hybrid Cloud Strategies confoo2012
Develop and deploy using Hybrid Cloud Strategies confoo2012Develop and deploy using Hybrid Cloud Strategies confoo2012
Develop and deploy using Hybrid Cloud Strategies confoo2012
 
Caching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTourCaching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTour
 
Scaling 101 test
Scaling 101 testScaling 101 test
Scaling 101 test
 
Scaling 101
Scaling 101Scaling 101
Scaling 101
 
From limited Hadoop compute capacity to increased data scientist efficiency
From limited Hadoop compute capacity to increased data scientist efficiencyFrom limited Hadoop compute capacity to increased data scientist efficiency
From limited Hadoop compute capacity to increased data scientist efficiency
 

More from Nascenia IT

Introduction to basic data analytics tools
Introduction to basic data analytics toolsIntroduction to basic data analytics tools
Introduction to basic data analytics toolsNascenia IT
 
Communication workshop in nascenia
Communication workshop in nasceniaCommunication workshop in nascenia
Communication workshop in nasceniaNascenia IT
 
The Art of Statistical Deception
The Art of Statistical DeceptionThe Art of Statistical Deception
The Art of Statistical DeceptionNascenia IT
 
করোনায় কী করি!
করোনায় কী করি!করোনায় কী করি!
করোনায় কী করি!Nascenia IT
 
GDPR compliance expectations from the development team
GDPR compliance expectations from the development teamGDPR compliance expectations from the development team
GDPR compliance expectations from the development teamNascenia IT
 
Writing Clean Code
Writing Clean CodeWriting Clean Code
Writing Clean CodeNascenia IT
 
History & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer VisionHistory & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer VisionNascenia IT
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineNascenia IT
 
iphone 11 new features
iphone 11 new featuresiphone 11 new features
iphone 11 new featuresNascenia IT
 
Software quality assurance and cyber security
Software quality assurance and cyber securitySoftware quality assurance and cyber security
Software quality assurance and cyber securityNascenia IT
 
Job Market Scenario For Freshers
Job Market Scenario For Freshers Job Market Scenario For Freshers
Job Market Scenario For Freshers Nascenia IT
 
Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)Nascenia IT
 
CSS for Developers
CSS for DevelopersCSS for Developers
CSS for DevelopersNascenia IT
 
Big commerce app development
Big commerce app developmentBig commerce app development
Big commerce app developmentNascenia IT
 
Integrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails ApplicationIntegrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails ApplicationNascenia IT
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideNascenia IT
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsNascenia IT
 
COREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform frameworkCOREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform frameworkNascenia IT
 

More from Nascenia IT (20)

Introduction to basic data analytics tools
Introduction to basic data analytics toolsIntroduction to basic data analytics tools
Introduction to basic data analytics tools
 
Communication workshop in nascenia
Communication workshop in nasceniaCommunication workshop in nascenia
Communication workshop in nascenia
 
The Art of Statistical Deception
The Art of Statistical DeceptionThe Art of Statistical Deception
The Art of Statistical Deception
 
করোনায় কী করি!
করোনায় কী করি!করোনায় কী করি!
করোনায় কী করি!
 
GDPR compliance expectations from the development team
GDPR compliance expectations from the development teamGDPR compliance expectations from the development team
GDPR compliance expectations from the development team
 
Writing Clean Code
Writing Clean CodeWriting Clean Code
Writing Clean Code
 
History & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer VisionHistory & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer Vision
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding Guideline
 
iphone 11 new features
iphone 11 new featuresiphone 11 new features
iphone 11 new features
 
Software quality assurance and cyber security
Software quality assurance and cyber securitySoftware quality assurance and cyber security
Software quality assurance and cyber security
 
Job Market Scenario For Freshers
Job Market Scenario For Freshers Job Market Scenario For Freshers
Job Market Scenario For Freshers
 
Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)
 
CSS for Developers
CSS for DevelopersCSS for Developers
CSS for Developers
 
Big commerce app development
Big commerce app developmentBig commerce app development
Big commerce app development
 
Integrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails ApplicationIntegrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails Application
 
Shopify
ShopifyShopify
Shopify
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
Clean code
Clean codeClean code
Clean code
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
 
COREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform frameworkCOREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform framework
 

Recently uploaded

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Recently uploaded (20)

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

Caching

  • 1. CACHING MEMCACHED vs. REDIS vs. FILESYSTEM Brown Bag Session Rafi Adnan Faisal Islam
  • 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.
  • 4. Traditional Data Query (Without Cache) Query Get Rows From Database Return Rows to Application
  • 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. 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.
  • 12. In-Memory means We are Bound By RAM
  • 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
  • 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. Redis : Sets ➔ Unordered collections of strings ➔ Unique ( no repeated members ) ➔ diff, intersect, merge
  • 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. 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. Redis: The Bad ➔ Single-Threaded ➔ Limited client support for consistent hashing ➔ Significant overhead for persistence ➔ Not widely deployed
  • 28. Memcached vs. Redis Memcached Redis (multi) get ✔ ✔ (multi) set ✔ ✔ increment/decrement ✔ ✔ delete ✔ ✔ expiration ✔ ✔ range queries ✔ data types ✔ persistence - ✔ multi-threaded ✔ replication - ✔