building scalable web applications with google app engineae-book.appspot.com › static ›...

50
Building Scalable Web Applications with Google App Engine Dan Sanderson May 15, 2012 Wednesday, May 23, 12

Upload: others

Post on 24-Jun-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Building ScalableWeb Applications

withGoogle App Engine

Dan SandersonMay 15, 2012

Wednesday, May 23, 12

Page 2: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Wednesday, May 23, 12

Page 3: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Wednesday, May 23, 12

Page 4: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

scalability

Each user gets the same quality of experience, regardless of how many users there are.

Wednesday, May 23, 12

Page 5: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Google App Engine

• Platform for building scalable web applications

• Built on Google infrastructure

• Based on Google’s internal best practices

• Pay for what you use

• Apps, instance hours, storage, bandwidth, service calls

• Free to start!

• Preview opened 2008; out of preview in 2011

Wednesday, May 23, 12

Page 6: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Google App Engine

• Easy development

• Easy deployment

• No servers to manage, no OS to update;App Engine does this for you

• Based on standard technologies

Wednesday, May 23, 12

Page 7: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

• Request handling infrastructure

• Application runtime environments

• Services

• Tools and libraries

• Administration Console

Google App Engine

Wednesday, May 23, 12

Page 8: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Demo

Wednesday, May 23, 12

Page 9: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Request Handlers

Wednesday, May 23, 12

Page 10: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Request Handlers• Requests come in, responses go out

• Request handler computes the response for a request

• Call services to manipulate stored data,perform special tasks

• Handler created when request arrives,destroyed after response is sent

• “Stateless” → scalability

Wednesday, May 23, 12

Page 11: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Request Handlers

• All code runs in a request handler

• Web hooks

• Unit of computation for larger jobs

Wednesday, May 23, 12

Page 12: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Instances• Longer-lived containers for request handlers

• Reduce initialization costs

• Maximize CPU utilization

• Exploit instance RAM

• Parameters to tune instance creation and destruction

• “Instance hours” are the billable unit for computation

Wednesday, May 23, 12

Page 13: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Runtime Environments

Wednesday, May 23, 12

Page 14: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Runtime Environments• Python

• Python 2.7; Python 2.5

• WSGI; CGI

• Java

• Full Java 6 JVM, J2EE servlet container

• Java, Scala, Ruby (JRuby), PHP (Quercus), JavaScript (Rhino), Python (JPython)

• Go

Wednesday, May 23, 12

Page 15: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Runtime Environmentsimport loggingimport webapp2

class MainPage(webapp2.RequestHandler): def get(self): status = self.request.get(‘status’) if status not in ('running', 'error', 'success'): logging.warning('Invalid status') status = ‘error’ template = template_env.get_template('home.html') context = { 'status': status, } self.response.out.write(template.render(context))

application = webapp2.WSGIApplication([('/', MainPage)], debug=True)

Wednesday, May 23, 12

Page 16: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Runtime Environmentsimport javax.servlet.http.*;

@SuppressWarnings("serial")public class MainPageServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String status = req.getAttribute("status"); if (!status.equals("running") || !status.equals("success")) status = "error";

req.setAttribute("status", status); resp.setContentType("text/html"); RequestDispatcher jsp = req.getRequestDispatcher("/WEB-INF/home.jsp"); jsp.forward(req, resp); }}

Wednesday, May 23, 12

Page 17: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Runtime Environments

• Sandboxing

• Data isolation

• Performance isolation

• Sandboxing → scalability

Wednesday, May 23, 12

Page 18: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Runtime Environments

• Limits

• Request timer

• Restricted access to filesystem, sockets

• More performance isolation: RAM, CPU

• Data sizes: requests, responses, API calls, storage objects

• Limits → scalability

Wednesday, May 23, 12

Page 19: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Services

Wednesday, May 23, 12

Page 20: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Services

• Features with their own scalable infrastructure

• Architecturally distinct from the runtime environments

• Synchronous and asychronous calling APIs

Wednesday, May 23, 12

Page 21: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Services

• Data storage

• Communication

• Data processing and manipulation

• Computation primitives

Wednesday, May 23, 12

Page 22: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Datastore

• Scalable object storage

• Replication using Paxos

• Named properties, typed values

• “Schemaless;” data modeling libraries

• Keys, kinds, and indexed queries

Wednesday, May 23, 12

Page 23: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Datastorefrom google.appengine.ext import db

class UserPrefs(db.Model): user = db.UserProperty(auto_current_user_add=True) created_datetime = db.DateTimeProperty(auto_now_add=True) tz_offset = db.IntegerProperty(default=0) subscribed = db.BooleanProperty()

class NewUser(webapp2.RequestHandler): def post(self): new_user = UserPrefs() new_user.subscribed = \ self.request.get('subscribed')) is not None new_user.put() self.redirect('/welcome')

Wednesday, May 23, 12

Page 24: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Datastore

• Datastore queries

• All queries are pre-indexed

• Built-in indexes do most of the work

• Custom indexes for complex queries; SDK helps you out

• Cursors

Wednesday, May 23, 12

Page 25: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Datastore

query = UserPrefs.all()query.filter(‘subscribed =’, True)

for userPref in query: # ... userPref.user ...

Wednesday, May 23, 12

Page 26: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Datastore

• Fetch by key: O(1)

• Create and update: O(1)

• Queries: O(number of results)

• not the total number of entities!

Each user gets the same quality of experience, regardless of how many users there are.

Wednesday, May 23, 12

Page 27: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Datastore

• Transactional

• Local transactions

• Strong consistency

• Eventually consistent cross-group transactions

• Local and global indexes

Wednesday, May 23, 12

Page 28: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Memcache

• Non-durable key-value store

• Distributed global cache

• Atomic set/add/replace, get, incr/decr of a single value

• Essential technique for speeding up user requests

Wednesday, May 23, 12

Page 29: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Blobstore

• Datastore and Memcache limited to 1MB objects

• Blobstore object size is unlimited

• Direct uploads and downloads

• Limited read/write app access

Wednesday, May 23, 12

Page 30: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Communication

• HTTP

• Out: URL Fetch

• In: request handlers!

• app-id.appspot.com

• www.your-domain.com

• SSL: https://app-id.appspot.com/ ;custom domain support soon

Wednesday, May 23, 12

Page 31: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Communication

• Email

• Out: a simple service call

• In: request handlers!

[email protected]

[email protected]

• “From” addresses: administrator, receiving address, current user (Google Accounts)

Wednesday, May 23, 12

Page 32: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Communication

• Instant messages (XMPP)

• Out: a simple service call

• In: request handlers!

• Addresses (“JIDs”)

[email protected]@app-id.appspotchat.com

• (No custom domain support.)

Wednesday, May 23, 12

Page 33: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Communication

• Channels

• Real-time push messages to browsers

• Comet connection

• JavaScript adapter provided

Wednesday, May 23, 12

Page 34: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Data Processing

• Images

• Search (experimental!)

• Prospective search (experimental!)

Wednesday, May 23, 12

Page 35: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Task Queues

• Defer work out of the user request

• Add a task to a queue, loosely FIFO

• Producers and consumers

Wednesday, May 23, 12

Page 36: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Task Queues

• Push queues

• A task is a request to your app!URL path + data

• Processed at a configurable rate

• Retried until “successful”

• Task chains

• App is both producer and consumer

Wednesday, May 23, 12

Page 37: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Task Queues

• Pull queues

• A task is any kind of payload

• Consumer API can pull tasks individually or in batches

• App API; useful with backends!

• Authenticated REST API

Wednesday, May 23, 12

Page 38: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Task Queues

You can enqueue tasks in a datastore transaction!

Wednesday, May 23, 12

Page 39: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Tools and Libraries

Wednesday, May 23, 12

Page 40: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Language-specific APIs

• Access services using the idioms of the language (Python, Java, Go)

• Manage synchronous and asynchronous calls

• Stubs for testing

Wednesday, May 23, 12

Page 41: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Data Modeling

• Translate between datastore entities and program objects

• Enforce data schemas and structures

• Python: ext.db, ext.ndb

• Java: datastore API; JDO, JPA; Objectify

Wednesday, May 23, 12

Page 42: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Pipelines

• Execute multiple tasks over large-scale data with parallelism

• Construct task dependencies

• Job status monitor

• http://code.google.com/p/appengine-pipeline

Wednesday, May 23, 12

Page 43: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

MapReduce

• Large-scale data processing

• Simple programming model

• map(item) → (key, value)*

• shuffle

• reduce(key, values) → result

• Can chain multiple MapReduces using pipelines

Wednesday, May 23, 12

Page 44: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

MapReduce

• App Engine implementation mostly in application code!

• http://code.google.com/p/appengine-mapreduce

• Primitive operations in services, e.g. Blobstore

• Experimental

Wednesday, May 23, 12

Page 45: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Development Server

• Simulates runtime environment and services on your local computer

• Suitable for functional testing

• Python: Launcher, dev_appserver.py

• Java: Eclipse plugin, dev_appserver.sh

Wednesday, May 23, 12

Page 46: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Deployment Tools

• Deploy software, files, configuration

• Download logs for offline analysis

• Python: Launcher, appcfg.py

• Java: Eclipse plugin, appcfg.sh

Wednesday, May 23, 12

Page 47: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Administration Console

Wednesday, May 23, 12

Page 48: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

Administration Console

• View traffic graphs

• Browse and search logs

• Inspect live data; backup/restore

• Manage instances

• Adjust budget and billing settings

Wednesday, May 23, 12

Page 49: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

• Request handling infrastructure

• Computation infrastructure

• Storage: fast, durable, arbitrarily large

• Simple development model,high productivity

• Managed infrastructure: just add code

Google App Engine

Wednesday, May 23, 12

Page 50: Building Scalable Web Applications with Google App Engineae-book.appspot.com › static › pgae-preso-20120515.pdf · Google App Engine • Platform for building scalable web applications

developers.google.com/appengine

appengine.google.com

ae-book.appspot.com

Programming Google App Engine, 2nd ed.Summer 2012

Dan Sandersonprofiles.google.com/dan.sanderson

Wednesday, May 23, 12