Pages

Saturday, March 15, 2014

Best practices for App Engine memcache and eventual vs strong consistency

We have published two new articles about best practices for App Engine. Are you aware of the best ways to keep Memcache and Datastore in sync? The article Best Practices for App Engine Memcache discusses concurrency, performanceand migration with Memcache to make you aware of potential pitfalls and to help you build more robust code.



Do you know how to make your App Engine application faster and more scalable by using eventual consistency? If not, take a look at a new article that explains the difference between eventual and strong consistency. The paper will help you leverage Datastore to scale your apps to millions of happy customers.



Concurrency, performance and migration in memcache

Memcache is a cache service for App Engine applications that is shared by multiple frontend instances and requests. It provides in-memory, temporary storage that is intended primarily as a cache for rapid retrieval of data thats backed by some form of persistent storage, such as Google Cloud Datastore.



Using Memcache will speed up your applications response to requests and reduce hits to the datastore (which in turn saves you money). However, keeping Memcache data synchronized with data in the persistent storage can be challenging when multiple clients modify the application data.



Transactional data sources, such as relational databases or Google Cloud Datastore, coordinate concurrent access by multiple clients. However, Memcache is not transactional, and theres a chance that two clients will simultaneously modify the same piece of data in Memcache. As a result, the data stored may be incorrect. Concurrency problems can be hard to detect because often they do not appear until the application is under load from many users.



With App Engine, you can use the “compare and set” (Client.cas()) function to coordinate concurrent access to memcache. However, if your application uses the compare and set function, it must be prepared to do the error handling and retry.



We recommend that you use the atomic Memcache functions where possible, including incr() and decr(), and use the cas() function for coordinating concurrent access. Use the Python NDB API if the application uses Memcache as a way to optimize reading and writing to Google Cloud Datastore. Read more Best Practices for App Engine Memcache in our newly published paper.



Balancing strong and eventual consistency

Web applications that require high-scalability often use NoSQL which offers eventual consistency for improved scalability. However, if youre used to the strong consistency that relational databases offer, it can be a bit of a mind shift to get your head around the eventual consistency of NoSQL data stores. Google Datastore allows you to choose between strong and eventual consistency, balancing the strengths of each.



Traditional relational databases provide strong consistency of their data, also called immediate consistency. This means that data viewed immediately after an update will be consistent for all observers of the entity. Use cases that require strong consistency include knowing “whether or not a user finished the billing process” or “the number of points a game player earned during a battle session.” It also means that all requests to view the updated data are blocked until all the writes required for strong consistency have finished.



Eventual consistency, on the other hand, means that all reads of the entity will eventually return the last updated value but might return inconsistent views of the data in the meantime. For example, knowing “who in your buddy list is online” or “how many users have +1’d your post” are cases where strong consistency is not required. Your application can get higher scalability and performance by leveraging eventual consistency, because your application wont have to wait for all the writes to complete before returning results.



The following two diagrams illustrate strong versus eventual consistency:




Eventual conistency




Strong consistency

To learn more about the differences between eventual and strong consistency and to learn how to take advantage of each read our article on the technical solutions portal at cloud.google.com/resources.



-Posted by Alex Amies, Cloud Solutions Technical Account Manager

Related Posts by Categories

0 comments:

Post a Comment