52
Boxcars and Cabooses When one more XHR is too much Peter Chittum Developer Evangelist @pchittum github.com/pchittum Cleaning up your CRUDdy API

Boxcars and Cabooses: When one more XHR is too much - Peter Chittum - Codemotion Amsterdam 2016

Embed Size (px)

Citation preview

Boxcars and Cabooses When one more XHR is too much

Peter Chittum Developer Evangelist @pchittum github.com/pchittum

Cleaning up your CRUDdy API

Forward Looking Statement Safe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services. The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with completed and any possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most recent fiscal quarter. These documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site. Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.

?

Agile and elastic platform that developers love

Smarter infrastructure lets you build apps that scale

Open and extensible

Modern language support and ecosystem of 150+ add-ons

Connected to Force.com

Sync customer apps with business processes

Build Customer-Facing Apps with Heroku

Force.com - Employee Facing Apps  

Apps

HR Product Supply Chain IT Finance Ops

Multi Tenant

150k customers 40 Prod POD’s 40 Prod DB’s

Pre-Built Apps  AppExchange is the #1 Business App Marketplace

Customized for Salesforce

Trusted and Secure

Reviewed by Peers

Over 2,800 apps 3 million installs

It’s About the API

Comprehensive Suite of APIs and Toolkits

Web Service Endpoint

Web Service Endpoint

Apex WS/REST

Outbound Messaging

Business Logic

Sync Bulk API

Streaming API Topic

CRUD

Data

Bayeux Client

Applications and Middleware Java SDK Ruby gem PHP

Toolkit Mobile

SDK 3rd Party Adapters

Apex Callouts

 Overall site peak day

•  >4 Billion transactions

•  200-250 milliseconds average

•  ~30% of transactions via API

requests

Salesforce’s Daily API Performance

Yesterday: ???

 Source: trust.salesforce.com

Automatic REST Endpoint Creation

 POST

/services/data/v35.0/sobjects/Account

BODY: {

"Name" : "CodeMotion Amsterdam",

"BillingCountry" : "Netherlands"

}

Create Record

 POST

/services/data/v35.0/sobjects/Account

BODY: {

"Name" : "CodeMotion Amsterdam",

"BillingCountry" : "Netherlands"

}

Fetch Record  GET

/services/data/v35.0/sobjects/Account/0012400000NBMWyAAP

SUCCESS RESPONSE: {

"attributes" : {"type" : "Account”,"url" : "...”},

"Id" : "0012400000NBMWyAAP",

"Name" : "CodeMotion Amsterdam",

"BillingCountry" : "Netherlands",

...

}

Query Endpoint  GET

/services/data/v35.0/query?q=SELECT ... FROM Account WHERE ...

SUCCESS RESPONSE: { "totalSize" : 2,

"done" : true,

"records" : [

{"attributes" : {"type" : "Account","url" : "..."},

"Id" : "0012400000NBMWyAAP",

"Name" : "Test 123",...},

{...}, ...]}

Describe (Discover)  GET

/services/data/v35.0/sobjects/Account/describe

SUCCESS RESPONSE: {

...

"queryable" : true,

"searchable" : true,

"updateable" : true,

...

}

Limits  GET

/services/data/v35.0/limits

SUCCESS RESPONSE: {

...

"DailyBulkApiRequests": {"Max" : 5000,"Remaining" : 5000},

"DailyStreamingApiEvents": {"Max" : 10000,"Remaining" : 9996},

"DataStorageMB" : {"Max" : 5,"Remaining" : 5},

...

}

Many Requests Many Server Trips

POST Record

GET Server Gen Data

GET API Limits

Composite Batch REST API

.../composite/batch POST Batch

{ "batchRequests":[ {POST}, {GET}, {GET} ] }

Sample Batch Request  POST: <salesforcedomain>/services/data/v35.0/composite/batch

 {"batchRequests" : [   {"method" : "POST",   "url" : "v35.0/sobjects/account/",   "richInput" : {"Name" : "NewName", "Industry" : "Tech"}},   {"method" : "GET",   "url" : "v35.0/query?q=select id, name, industry from account   order by createddate desc limit 10"},   {"method" : "GET",   "url" : "v35.0/limits"   }]  }

Sample Batch Response Object

 {   "hasErrors": false,   "results": [   { "statusCode": 201, "result": {...} },   { "statusCode": 200, "result": {...} },   { "statusCode": 200, "result": {...} }   ]  }

Batch Request Behavior •  Resource which accepts multiple REST calls to execute

• Up to 25 sub-requests

• URI, Method, and optional Body

•  Sub-requests can be unrelated API calls

•  Sub-requests are executed serially, in order, and as the running user

• Commit each subrequest on completion

•  Optional parameter: haltOnError •  Do not continue after error occurs

•  But check subrequests for errors

Hierarchy

Parent/Child Related Data

POST Account

POST related Contacts

POST related Cases

RESP: Account ID

RESP: Contact IDs

RESP: Case IDs

Composite Tree REST API

.../composite/tree/entity

POST Tree

”records":[ {parent1}, {parent2}, {parent3} ]

Sample Tree Request  POST: <salesforcedomain>/services/data/v35.0/composite/tree/Account  {"records" :[   {"attributes": {"type":"Account", "referenceId":"ref1"},   "name" : "CodeMotion", "phone" : "1234567890",   "type" : "Customer", "industry" : "Events",   "Contacts" : {   "records" : [   {"attributes": {"type":"Contact", "referenceId":"ref2"},   "lastname" : "Smith", "title" : "Organizer"},   ...]},   "Cases" : {   "records" : [   {"attributes": {"type":"Case", "referenceId":"ref3"},   "" : "", "" : "", "" : ""}   ...]}, }}, ...] }

Sample Tree Request: Many Records  POST: <salesforcedomain>/services/data/v35.0/composite/tree/Account  {"records" :[   {"attributes": {"type":"Account", "referenceId":"ref1"},   "name" : "CodeMotion", "phone" : "1234567890",   "type" : "Customer", "industry" : "Events",   "Contacts" : {   "records" : [   {"attributes": {"type":"Contact", "referenceId":"ref2"},   "lastname" : "Smith", "title" : "Organizer"},   ...]},   "Cases" : {   "records" : [   {"attributes": {"type":"Case", "referenceId":"ref3"},   "" : "", "" : "", "" : ""}   ...]}, }}, ...] }

Sample Tree Request: Parent Record  POST: <salesforcedomain>/services/data/v35.0/composite/tree/Account  {"records" :[   {"attributes": {"type":"Account", "referenceId":"ref1"},   "name" : "CodeMotion", "phone" : "1234567890",   "type" : "Customer", "industry" : "Events",   "Contacts" : {   "records" : [   {"attributes": {"type":"Contact", "referenceId":"ref2"},   "lastname" : "Smith", "title" : "Organizer"},   ...]},   "Cases" : {   "records" : [   {"attributes": {"type":"Case", "referenceId":"ref3"},   "" : "", "" : "", "" : ""}   ...]}, }}, ...] }

Sample Tree Request: Child Records  POST: <salesforcedomain>/services/data/v35.0/composite/tree/Account  {"records" :[   {"attributes": {"type":"Account", "referenceId":"ref1"},   "name" : "CodeMotion", "phone" : "1234567890",   "type" : "Customer", "industry" : "Events",   "Contacts" : {   "records" : [   {"attributes": {"type":"Contact", "referenceId":"ref2"},   "lastname" : "Smith", "title" : "Organizer"},   ...]},   "Cases" : {   "records" : [   {"attributes": {"type":"Case", "referenceId":"ref3"},   "" : "", "" : "", "" : ""}   ...]}, }}, ...] }

Sample Tree Request: Client Ids  POST: <salesforcedomain>/services/data/v35.0/composite/tree/Account  {"records" :[   {"attributes": {"type":"Account", "referenceId":"ref1"},   "name" : "CodeMotion", "phone" : "1234567890",   "type" : "Analyst", "industry" : "Events",   "Contacts" : {   "records" : [   {"attributes": {"type":"Contact", "referenceId":"ref2"},   "lastname" : "Smith", "title" : "Organizer"},   ...]},   "Cases" : {   "records" : [   {"attributes": {"type":"Case", "referenceId":"ref3"},   "" : "", "" : "", "" : ""}   ...]}, }}, ...] }

Demo

The (Near) Future

 Extension of Batch and Composite

 Outputs from one sub request to be used as inputs for the another

Possible Future Features

 Parameter-based values

 Basic orchestration baked into /batch

 Updates on /tree

The Client Problem

Aura and Lightning Component Framework

Lightning Component Framework

Lightning Component Design Principles

•  Component author namespacing

•  Automatic component-based CSS namespacing

•  Everything is a component

•  Allow for programmatic or point-and-click UI composition

•  Enable Salesforce, customers, and partners to build composite UIs

•  Works on any form factor

The Composite App

Many Components Many Server Trips

XMLHttpRequest

XMLHttpRequest

XMLHttpRequest

Actions: Interact with the Server

•  Apex Method Surfaced to Lightning Components •  @AuraEnabled annotation

Boxcarring: Many Actions, One XHR

Action S

ervice

Caboose: Postpone High Volume Actions

Action S

ervice •  Defer High-Volume Actions •  Action.setCaboose()

Action Service: Server Side API  Apex: Code on Force.com

Action Service Client API

Upcoming Features

 Integration of offline data store with Action service

 Ability to prioritize actions

Takeaways

 Minimize server requests

 Optimize CRUD-based APIs with aggregation

 Optimize creation of hierarchical data

 Client-side libraries to support request batching

 Meetup: bit.ly/ams-sf-devs

 startups.salesforce.com

 trailhead.com

Q & A Peter Chittum Developer Evangelist @pchittum github.com/pchittum

 Learn: developer.salesforce.com/trailhead

 Meetup: bit.ly/ams-sf-devs

Thank you