67
Server Connect To API / 3rd Service Pattern Caesar Chi @clonncd Node.js 與多方服務串接實務 Monday, June 23, 14

Server Connect To API / 3rd Service Pattern

Embed Size (px)

Citation preview

Server Connect To API / 3rd Service Pattern

Caesar Chi @clonncd

Node.js 與多方服務串接實務

Monday, June 23, 14

@clonncd

Monday, June 23, 14

Monday, June 23, 14

https://github.com/nodejs-tw/nodejs-wiki-book

Monday, June 23, 14

Monday, June 23, 14

Structure

Clients

Server

Monday, June 23, 14

That means we thought done with something in Node.js

But actually we did nothing special.

Monday, June 23, 14

Monday, June 23, 14

Request of

As a Front end Server

Monday, June 23, 14

Structure

front Server

Services

secret information cache

send Request

Monday, June 23, 14

WWW are APIsWhen Node.js has Request

Monday, June 23, 14

Structure

front Server

Services

send Request

...

...

Monday, June 23, 14

But WhenService requirement

Grows Up

Monday, June 23, 14

Structure

front Server

Internal server

send Request

...

...

Too Much Code To Handle

Monday, June 23, 14

Service Definition

• Front server can be flexible to connect

• Support Restful API for internal connect

• Same Router rule for client

• Modules install / uninstall be easy

• Light way feedback data

Monday, June 23, 14

Server Definenation

• Front server can be flexible to connect

• Support Restful API for internal connect

• Same Router rule for client

• Modules install / uninstall be easy

• Light way feedback data

Service as a module, feature as a apps

https://github.com/clonn/module-loader

Tips

Monday, June 23, 14

app3

app2

app1

app4

Routers as apps, auto routeMonday, June 23, 14

app3

app2

app1

app4

Every developers can handle by themselvesMonday, June 23, 14

Make connect easy, A tiny distribution server

Monday, June 23, 14

Face Response Issue ...

Monday, June 23, 14

Wait ... Wait ... Wait ...https://www.flickr.com/photos/tormodspictures/

Monday, June 23, 14

伺服器反應很慢

http://chinese.engadget.com/2014/06/21/facebook-android-update-africa/

Monday, June 23, 14

Let’s refactoring code

Monday, June 23, 14

Monday, June 23, 14

Response back as soon as posible

Monday, June 23, 14

Heavy processing or request, put late

Monday, June 23, 14

Response as short as posiblehttps://www.flickr.com/photos/calliope/

Monday, June 23, 14

what can NOT be late

• real time date, have to be light (geo, data)

• robust data, we could not avoid (but save in cached, redis and process them later)

Monday, June 23, 14

What can late

• log data writing

• data saving (not for real time response)

• data caching

Monday, June 23, 14

Response First, Process LaterTips

Monday, June 23, 14

Too many callback event in waiting queue

https://www.flickr.com/photos/peter_curb/14318113011/sizes/c/in/photostream/

Monday, June 23, 14

WARNNING, It Cause High CPU Usage

https://www.flickr.com/photos/peter_curb/14318113011/sizes/c/in/photostream/

Monday, June 23, 14

We put process in

Task Queue.

http://learnboost.github.io/kue/

Monday, June 23, 14

Save process, prarams in Task Queue

Monday, June 23, 14

Monday, June 23, 14

Set callback event when things done

Monday, June 23, 14

Send request to 3rd party library / API / URL

Monday, June 23, 14

Feedback to Server or Storagetrigger callback event to Server

Monday, June 23, 14

Monday, June 23, 14

How feedback to clients, when task queue process task / jobs done, how could we notify clients?

How about Client?

Monday, June 23, 14

Time meet

http://socket.io/

or

Socket

Monday, June 23, 14

Client and Server connect via a Tunnel

Monday, June 23, 14

With handshake

Monday, June 23, 14

Structure

Clients

Server

Server push

Monday, June 23, 14

Structure

Clients

Server

Server push

From single way to double waysMonday, June 23, 14

Structure

Clients

Server

Server push

From single way to double ways

JSON

Ads

Habit

Monday, June 23, 14

Server push, that means

• Ajax mode changed

• Browser can do more things

• User request is getting more

• Server has to afford more currency

• Server has opportunity feedback data

Monday, June 23, 14

Let’s integrate with Socket.io / Socket and

Task Queue

Monday, June 23, 14

Feedback to Server or Storage

Monday, June 23, 14

Return data to User in a few minutes

Monday, June 23, 14

Return data to User in a few seconds

Monday, June 23, 14

Increase User Experience

Not Make Them Wait

Monday, June 23, 14

Monday, June 23, 14

Cache Layer

Monday, June 23, 14

Storage Layer

Monday, June 23, 14

Part of access

Monday, June 23, 14

Part of heavy access

Monday, June 23, 14

Not finished yet

Monday, June 23, 14

Monday, June 23, 14

http://letsnode.com/example-of-what-node-is-really-good-at

Monday, June 23, 14

http://mortarnpistol.com/2011/12/16/big-rigs-over-the-road-racing/

Monday, June 23, 14

Monday, June 23, 14

Monday, June 23, 14

Rules

• Make events as light as posible

• Divide things to tiny

• Heavy process throw to another way

• Monitor app.js after done

• Figure out bottlenecks

• Decrease users idle / waiting time as posible

Monday, June 23, 14

https://github.com/nodejs-tw/nodejs-book-beginner-guideMonday, June 23, 14

@clonncd

Monday, June 23, 14