Google App Engine Best Practices

I'm following the "Best Practices - Building a Production Quality Application on Google App Engine" session at Google IO. Ken Ashcraft is giving this presentation.

movie = Movie() movie.title = self.request.get('title', None) movie.director = self.request.get('director', None) movie.put()

Possible Errors: Out of Memory Deadline Exceeded OverQuotaError Server Crash DataStore Crash Indentical Entity Already Exists

Use logging module: logging.error, logging.debug -- which shows up in Admin Console

This is called whenever the Request Handler encounters an exception

Email upon exception: def handle_exception(self, exception, debug_mode): lines = ".join(traceback.format_exception(*sys.exc.info())) logging.error(lines) mail.send_mail_to_admins(sender='myapp-noreply@gmail.com', subject='Caught Exceptoin', body=lines) template_values = {} if users.is_current_user_admin(): template_values = lines (FIX LATER) self.response.out.write(template.render('error.html', template_values))

Python Profiler

def profile_main(): logging.info("Profiler data: %s, profile_data) if random.randint(0,100) == 4: profile_main() else: non_profile_main()

Load Tests Reveal which part of webapp are slow, which resources are scare (CPU, Bandwidth, etc)

Remember, the dev_appserver is not designed for performance

Performance has been optimized for organic load increases (whatever that means) versus synthetic benchmarks.

Under load the App Engine front end will create extra python interpreters, and keeps the recently spawned interpreters warm. Talking about load test characteristics , ramp up slowly, and test sustained load over many minutes (10, 15) vs 1min.

Httperf is being recomened as good tool for driving http requests.

Talking about realistic values 50,000 users per day 2 page views per user 100,000 pageviews per day 5 requests per page view 500,000 requests per day

Leads to 5.8 requests per second

Qualities of good load test Use production system, not dev_appserver Gradual ramp up to allow BigTable warm up Sustained Load, so any hiccups will be averaged out Realistic Load

Safe Deployment Upload code and don't worry about pushing bug live to users -- Versions -- On every upload App Engine increments revision and points to latest revision -- However, you can have multiple versions running in parallel (0.1, 0.2) can be accessed via http://2.1.appname.appspot.com -- However, both will point to same BigTable data-store

http://...appspot.com

You control the version via app.yaml -- Suggestion: Create a build script to populate version from SCM revision number

Test deployment with Selenium, use the log viewer to look for errors

Feature requests: Easy A/B testing Integration with SVN Edit Running Code (Google not really in favor of this)

Summary: Write Code to handle errors, use logging and catch exceptions Do realistic load tests Use versions for safe deployment