Memcache BasicsApp Engine
Who? Why?Ido GreenSolutions Architect
plus.google.com/greenido
greenido.wordpress.com
App Engine: Memcache Basics
Topics We Cover in This Lesson
App Engine: Memcache Basics
● What is Memcache?
● Why do we need Memcache?
● How to Use Memcache?
● Caveats And Solutions
What Is Memcache?
App Engine: Memcache Basics
● Memcache is an in-memory Key-Value Pairs data store○ Put a value with a key○ Get a value with a key
"user001" : "John Doe"
"user002" : "Larry Page"
What is Memcache?
What is Memcache?
● key or value can be anything that is serializable● Memcache is a shared service accessed via App Engine APIs.
valuekey
What Do We Use Memcache For?
What Is Memcache
● Caching In Front of Datastore○ Cache entities for low-latency reads○ Integrated into most ORM frameworks (ndb, Obectify ..)
● Caching for Read heavy operations○ User authentication token and session data○ APIs call or other computation results
● Semi-durable Shared state Cross App Instances○ Sessions○ Counters / Metrics○ Application Configurations
Why Do We Need Memcache?
App Engine: Memcache Basics
Why Do We Use Memcache?
Why do we need Memcache?
● Improve Application Performance
● Reduce Application Cost
How Fast Can Memcache be? And How Much?
Why do we need Memcache?
Datastore Query Latency Memcache Read Latency
How to Use Memcache
Memcache APIs
How to Use Memcache
● Java
○ JCache APIs
○ GAE Low-Level Memcache APIs
○ Objectify for Datastore
● Python
○ google.appengine.api.memcache module
○ ndb for Datastore
General Memcache Usage Pattern
How to Use Memcache
Coordinate data read with Datastore:● Check if Memcache value exists
○ if it does, display/use cached value directly; otherwise○ fetch the value from Datastore and write the value to
Memcache
Coordinate data write with Datastore:● Update Memcache value
○ to handle race condition, leverage put if untouched/compare and set to detect race conditions
● Write the value to Datastore○ optionally, leverage the task queue for background writes
Using GAE Client Library (Java)import com.google.appengine.api.memcache;
...
MemcacheService syncCache = MemcacheServiceFactory.
getMemcacheService();
// Act on error as cache miss
syncCache.setErrorHandler(ErrorHandlers.
getConsistentLogAndContinue(Level.INFO));
value = (byte[]) syncCache.get(key); // read from cache
if (value == null) {
value = getDataFromDb(key); // fetch value from datastore
syncCache.put(key, value); // write to cache
// (key and value must be
serializable)
}
...
How to Use Memcache
Using GAE Client Library (Python)
from google.appengine.api import memcache
...
value = memcache.get(key)
if value is None:
value = get_value_from_db(key)
if not memcache.add(key, value):
logging.error('Memcache add failed.')
...
How to Use Memcache
Batch Operations● getAll(), putAll(), deleteAll()
A single read or write operation for multiple memcache entries
How to Use Memcache
Note
● Further improve Memcache performance by reducing network traffics● Batch size < 32 MB
Atomic Operations● increment(key, delta), incrementAll(...),
Provide atomic increment of numeric value(s)
● getIdentifiable(), putIfUntouched() in Java, or cas() in PythonA mechanism to update a value consistently by concurrent requests
How to Use Memcache
Note
● Helps managing memcache data consistency in multi-instances/concurrent environment
Some Other Features● Asynchronous calls
Provides a mechanism to make a non-blocking call for memcache operations
● NamespaceLogically separates data layers for different application purposes (like multi tenancy) across many GAE services such as Datastore, Memcache, Task Queue etc...
How to Use Memcache
Caveats And Solutions
Entries can be evicted anytime by various reasons:● entry reaches expiration● entry is evicted because memcache memory is full● memcache server fails
Caveat: Memcache Is Volatile
Caveats And Solutions
● It's important to handle cache-miss gracefully!
● Implements write-through logic by backing memcache with datastore in your application!○ Psst... Objectify and ndb do it for you.
Tip
Caveat: Memcache Is Not Transactional
Caveats And Solutions
$100 $100Instance 1 reads $100
Using getIdentifiable() and putIfUntouched(...) for optimistic locking
$100Instance 2 reads $100
$80 $80Instance 2 deducts $20
$70 $70Instance 1 deducts $30
$100
Tip
Caveat: Memcache Is A Limited Resource
Caveats And Solutions
● Only need to cache what is useful and necessary!
● Your application should function without memcache!
● Dedicated Memcache
My Application Does NOT Have Enough Memcache!
Tips
1. Provides fixed cache capacity that is exclusive to your application
2. Billed by the GB per hour of cache size. Charged in 15 minute increments
3. Gives you additional control over cache size. More predictable performance
Note: Whether shared or dedicated, Memcache is not a durable storage. Plan for your application to function without Memcache
Dedicated Memcache
Enabling dedicated memcache from the admin console
Caveats And Solutions
1. Memcache is supported natively in GAE. Take advantage of it to improve your GAE application performance.
2. Memcache supports open standard JCache API. Many advanced features are available by GAE Memcache APIs to suit your application's need i.e. Batch, Atomic, Asynchronous operations
3. Seamless integration with GAE Datastore in a few libraries like Python ndb and Java Objectify.
4. Read-frequently and write-rarely data is most suitable in combining with Memcache
5. Handle Memcache's volatility in your application
6. Use Memcache wisely, it is not an unlimited resource
Key Takeaways
Discussion And Questions
Questions?
Thank you!
App Engine: Memcache Basics