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 (20)

File system node js
File system node jsFile system node js
File system node js
 
Caching Strategies
Caching StrategiesCaching Strategies
Caching Strategies
 
Database System Architectures
Database System ArchitecturesDatabase System Architectures
Database System Architectures
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Asp.net caching
Asp.net cachingAsp.net caching
Asp.net caching
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
NOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQLNOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQL
 
Json
JsonJson
Json
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Database
 
Node.js File system & Streams
Node.js File system & StreamsNode.js File system & Streams
Node.js File system & Streams
 
Apache Spark Introduction
Apache Spark IntroductionApache Spark Introduction
Apache Spark Introduction
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Parquet overview
Parquet overviewParquet overview
Parquet overview
 
Hadoop Map Reduce
Hadoop Map ReduceHadoop Map Reduce
Hadoop Map Reduce
 
Introduction to NoSQL Databases
Introduction to NoSQL DatabasesIntroduction to NoSQL Databases
Introduction to NoSQL Databases
 
An Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBAn Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDB
 
Spark architecture
Spark architectureSpark architecture
Spark architecture
 

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

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
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
 
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
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
#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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
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
 
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
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
#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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

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 - ✔