Mobile Api and Caching

42
MOBILE API AND CACHING Making the most of your APIs

description

Presentation from Pierre-Luc Simard, CTO of Mirego.

Transcript of Mobile Api and Caching

Page 1: Mobile Api and Caching

MOBILE API AND CACHINGMaking the most of your APIs

Page 2: Mobile Api and Caching

PERFORMANCE MATTERS.

Page 3: Mobile Api and Caching

MOBILE APPLICATIONAVERAGE API RESPONSE TIME.

1.24seconds

source: New Relic

Page 4: Mobile Api and Caching

TIME SPENT.

SERVER 20%

NETWORK 80%

Page 5: Mobile Api and Caching

BASIC MATHEMATICS.

session durationrequest duration = requests

per session

Page 6: Mobile Api and Caching

WILDLY INACCURATE TESTS.Check my balance43 requests in 70 seconds

Check-in46 requests in 30 seconds

Open a message 24 requests in 40 seconds

Page 7: Mobile Api and Caching

AVERAGE:

0.92second

Page 8: Mobile Api and Caching

THE HARD STUFF.

•INCREASE SESSION TIME. •REDUCE REQUEST TIME.

Page 9: Mobile Api and Caching

DOING MORE.

session durationrequest duration = requests

per session

Page 10: Mobile Api and Caching

MAKE FEWER REQUESTS PER ACTION.

Page 11: Mobile Api and Caching

BE SPECIFIC.

Page 12: Mobile Api and Caching

WEB BROWSER CACHES EVERYTHINGUNLESS TOLD OTHERWISE.

Page 13: Mobile Api and Caching

MOBILE APPLICATIONCACHES NOTHINGUNLESS TOLD OTHERWISE.

Page 14: Mobile Api and Caching

BASIC REQUIREMENT.

ONE =objectONE

URI

Page 15: Mobile Api and Caching

FOLLOWING JSON API GUIDELINES HELPS ENSURE OBJECT TO URI EQUIVALENCY.http://jsonapi.org

Page 16: Mobile Api and Caching

HTTP, FOR YOU IT WORKS.

Page 17: Mobile Api and Caching

GET /collections/12 HTTP/1.1 Host: flipbit.dev !HTTP/1.1 200 OK Date: Thu, 17 Oct 2013 01:42:00 GMT Accept-Ranges: bytes Content-Type: application/vnd.api+json Content-Length: 42 Connection: keep-alive Location: http://flipbit.dev/collections/12

Page 18: Mobile Api and Caching

GET /collections/12 HTTP/1.1 Host: flipbit.dev !HTTP/1.1 200 OK Date: Thu, 17 Oct 2013 01:42:00 GMT Last-Modified: Thu, 17 Oct 2013 01:38:57 GMT ETag: "b79f914dde3ab5e3095372de46591b46" Expires: Thu, 24 Oct 2013 01:38:57 GMT Cache-Control: max-age=604800, private Accept-Ranges: bytes Content-Type: application/vnd.api+json Content-Length: 42 Connection: keep-alive Location: http://flipbit.dev/collections/12

Page 19: Mobile Api and Caching

GET /collections/12 HTTP/1.1 Host: flipbit.dev If-Modified-Since: Thu, 10 Oct 2013 20:14:48 GMT If-None-Match: "a1846ac92492d2347c6235b4d2611184" !HTTP/1.1 200 OK Date: Thu, 17 Oct 2013 01:42:00 GMT Last-Modified: Thu, 17 Oct 2013 01:38:57 GMT ETag: "b79f914dde3ab5e3095372de46591b46" Expires: Thu, 24 Oct 2013 01:38:57 GMT Cache-Control: max-age=604800, private Accept-Ranges: bytes Content-Type: application/vnd.api+json Content-Length: 42 Connection: keep-alive Location: http://flipbit.dev/collections/12

Page 20: Mobile Api and Caching

CACHE-CONTROL ON AMAZON S3.

Page 21: Mobile Api and Caching

GET /buket.mycompany.com/welcome.json HTTP/1.1 Host: s3.amazonaws.com !HTTP/1.1 200 OK x-amz-id-2: UleVE3wbOPZa2EW+RpWj834yy5OOMLNmi/w… x-amz-request-id: 4ADBB2EBF80F5D46 Date: Thu, 17 Oct 2013 01:42:00 GMT Last-Modified: Thu, 17 Oct 2013 01:38:57 GMT ETag: "b79f914dde3ab5e3095372de46591b46" Accept-Ranges: bytes Content-Type: application/octet-stream Content-Length: 23 Connection: keep-alive Server: AmazonS3

Page 22: Mobile Api and Caching

GET /buket.mycompany.com/welcome.json HTTP/1.1 Host: s3.amazonaws.com !HTTP/1.1 200 OK x-amz-id-2: UleVE3wbOPZa2EW+RpWj834yy5OOMLNmi/w… x-amz-request-id: 4ADBB2EBF80F5D46 Date: Thu, 17 Oct 2013 01:42:00 GMT Last-Modified: Thu, 17 Oct 2013 01:38:57 GMT ETag: “b79f914dde3ab5e3095372de46591b46" Cache-Control: ??? Expires: ??? Accept-Ranges: bytes Content-Type: application/octet-stream Content-Length: 23 Connection: keep-alive Server: AmazonS3

Page 23: Mobile Api and Caching

CACHING IS UNCERTAIN.

•There’s no cache-control header so it’s left to the client to decide.

•iOS most likely will cache between 6 hours and 1 day and will always revalidate.

•Android it depends, but most likely won’t cache.

Page 24: Mobile Api and Caching

DON’T LET THE CLIENT DECIDE.

•Add the x-amz-meta-Cache-Control header when doing a PUT to S3.

•It’s recommended to use a max-age of at least one month.

•Don’t use x-amz-meta-Expires. The object will not be cached after its expiry date.

Page 25: Mobile Api and Caching

CACHE-CONTROL WITH RUBY ON RAILS.

Page 26: Mobile Api and Caching

GET /collections/12 HTTP/1.1 Host: app.mycompany.com !Cache-Control: max-age=0, private, must-revalidate Content-Type: application/json; charset=utf-8 ETag: "4a200c9d8fb14925e7461d63b59f4e82" Server: nginx/1.2.3 + Phusion Passenger 3.0.17 (mod_rails/mod_rack) Set-Cookie: request_method=GET; path=/ Status: 200 Content-Length: 144 Connection: Close

Page 27: Mobile Api and Caching

FIXING CACHE-CONTROL.class FlipsController < ApplicationController respond_to :json ! def index # ... ! # Last-Modified and ETag flipbit_etag = Digest::MD5.digest(@flipbit.to_s).to_s fresh_when last_modified: DateTime.now, etag: flipbit_etag ! # Cache-Control expires_in 3.day, :must_revalidate => false, :public => true # ... end end

Page 28: Mobile Api and Caching

GET /collections/12 HTTP/1.1 Host: app.mycompany.com !Cache-Control: max-age=86400, public Content-Type: application/json; charset=utf-8 ETag: "4a200c9d8fb14925e7461d63b59f4e82" Server: nginx/1.2.3 + Phusion Passenger 3.0.17 (mod_rails/mod_rack) Set-Cookie: request_method=GET; path=/ Status: 200 Content-Length: 144 Connection: Close

Page 29: Mobile Api and Caching

CACHING IS BETTER.

•iOS will honour the cache-control header as long as there’s cache space.

•Android apache’s HttpClient does not handle caching, android-query does.

Page 30: Mobile Api and Caching

CACHING ON THE DEVICE.

Page 31: Mobile Api and Caching

NEVER HARD CODE CACHE LOGIC.

Page 32: Mobile Api and Caching

MAKE SURE YOU HAVE ENOUGH STORAGE.

Page 33: Mobile Api and Caching

CACHE ON iOS.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSUInteger mb = 1024*1024; ! // Configure NSURLCache with larger storage [NSURLCache setSharedURLCache: [[NSURLCache alloc] initWithMemoryCapacity:(50*mb) diskCapacity:(100*mb) diskPath:nil] ]; ! // ... // ... return YES; }

Page 34: Mobile Api and Caching

MAKE EVERY ROUND-TRIP COUNT.

Page 35: Mobile Api and Caching

BONUS MATERIAL.Amazon S3 performance.

Page 36: Mobile Api and Caching

S3 GET PERFORMANCE.

•Make sure you set your Cache-Control headers using x-amz-meta-Cache-Control.

•Intensive objects benefit from CloudFront by a factor of 2 or more.

Page 37: Mobile Api and Caching

S3 PUT PERFORMANCE.

•Avoid sequential keys. S3 stores key names in alphabetical order; key name dictates which partition the key is stored in.http://bit.ly/s3performance

Page 38: Mobile Api and Caching

AVOID DATES AS PREFIX.examplebucket/2013-26-05-15-00-00/cust1234234/photo1.jpg examplebucket/2013-26-05-15-00-00/cust3857422/photo2.jpg examplebucket/2013-26-05-15-00-00/cust1248473/photo2.jpg examplebucket/2013-26-05-15-00-00/cust8474937/photo2.jpg examplebucket/2013-26-05-15-00-00/cust1248473/photo3.jpg ... examplebucket/2013-26-05-15-00-01/cust1248473/photo4.jpg examplebucket/2013-26-05-15-00-01/cust1248473/photo5.jpg examplebucket/2013-26-05-15-00-01/cust1248473/photo6.jpg examplebucket/2013-26-05-15-00-01/cust1248473/photo7.jpg ...

Page 39: Mobile Api and Caching

USE HASH AS PREFIX.examplebucket/232a-2013-26-05-15-00-00/cust1234234/photo1.jpg examplebucket/7b54-2013-26-05-15-00-00/cust3857422/photo2.jpg examplebucket/921c-2013-26-05-15-00-00/cust1248473/photo2.jpg examplebucket/ba65-2013-26-05-15-00-00/cust8474937/photo2.jpg examplebucket/8761-2013-26-05-15-00-00/cust1248473/photo3.jpg examplebucket/2e4f-2013-26-05-15-00-01/cust1248473/photo4.jpg examplebucket/9810-2013-26-05-15-00-01/cust1248473/photo5.jpg examplebucket/7e34-2013-26-05-15-00-01/cust1248473/photo6.jpg examplebucket/c34a-2013-26-05-15-00-01/cust1248473/photo7.jpg ...

Page 40: Mobile Api and Caching

SHARING A BUCKET?examplebucket/my_beautiful_app/40434b70/avatar.jpg examplebucket/my_beautiful_app/4ee66b70/avatar.jpg examplebucket/my_beautiful_app/753da383/avatar.jpg examplebucket/my_beautiful_app/91255df0/avatar.jpg examplebucket/my_beautiful_app/98e26030/avatar.jpg examplebucket/my_very_nice_app/a2cb3fdb/video.mpg examplebucket/my_very_nice_app/cf763412/video.mpg examplebucket/my_very_nice_app/d6e93451/video.mpg examplebucket/my_very_nice_app/e68f7670/video.mpg ...

Page 41: Mobile Api and Caching

USE SHORT FIXED PREFIX!examplebucket/mba/40434b70/avatar.jpg examplebucket/mba/4ee66b70/avatar.jpg examplebucket/mba/753da383/avatar.jpg examplebucket/mba/91255df0/avatar.jpg examplebucket/mba/98e26030/avatar.jpg examplebucket/mvna/a2cb3fdb/video.mpg examplebucket/mvna/cf763412/video.mpg examplebucket/mvna/d6e93451/video.mpg examplebucket/mvna/e68f7670/video.mpg ...

Page 42: Mobile Api and Caching