memcache basics on google app engine

24
Memcache Basics App Engine

Upload: ido-green

Post on 10-May-2015

3.769 views

Category:

Technology


1 download

DESCRIPTION

In this talk we will cover: 1. What is Memcache? 2. Why do we need Memcache? 3. How to Use Memcache on Google App Engine? 4. Caveats And Solutions

TRANSCRIPT

Page 1: Memcache basics on google app engine

Memcache BasicsApp Engine

Page 2: Memcache basics on google app engine

Who? Why?Ido GreenSolutions Architect

plus.google.com/greenido

greenido.wordpress.com

App Engine: Memcache Basics

Page 3: Memcache basics on google app engine

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

Page 4: Memcache basics on google app engine

What Is Memcache?

App Engine: Memcache Basics

Page 5: Memcache basics on google app engine

● 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

Page 6: Memcache basics on google app engine

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

Page 7: Memcache basics on google app engine

Why Do We Need Memcache?

App Engine: Memcache Basics

Page 8: Memcache basics on google app engine

Why Do We Use Memcache?

Why do we need Memcache?

● Improve Application Performance

● Reduce Application Cost

Page 9: Memcache basics on google app engine

How Fast Can Memcache be? And How Much?

Why do we need Memcache?

Datastore Query Latency Memcache Read Latency

Page 10: Memcache basics on google app engine

How to Use Memcache

Page 11: Memcache basics on google app engine

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

Page 12: Memcache basics on google app engine

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

Page 13: Memcache basics on google app engine

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

Page 14: Memcache basics on google app engine

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

Page 15: Memcache basics on google app engine

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

Page 16: Memcache basics on google app engine

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

Page 17: Memcache basics on google app engine

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

Page 18: Memcache basics on google app engine

Caveats And Solutions

Page 19: Memcache basics on google app engine

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

Page 20: Memcache basics on google app engine

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

Page 21: Memcache basics on google app engine

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

Page 22: Memcache basics on google app engine

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

Page 23: Memcache basics on google app engine

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

Page 24: Memcache basics on google app engine

Questions?

Thank you!

App Engine: Memcache Basics