Pattern: API Gateway

Pattern: API Gateway / Backend for Front-End
Context
Let’s imagine you are building an online store that uses the Microservice architecture pattern and that you are implementing the product details page. You need to develop multiple versions of the product details user interface:
HTML5/JavaScript-based UI for desktop and mobile browsers - HTML is generated by a server-side web application
Native Android and iPhone clients - these clients interact with the server via REST APIs

In addition, the online store must expose product details via a REST API for use by 3rd party applications.
A product details UI can display a lot of information about a product. For example, the Amazon.com details page for POJOs in Actiondisplays:
Basic information about the book such as title, author, price, etc.
Your purchase history for the book
Availability
Buying options
Other items that are frequently bought with this book
Other items bought by customers who bought this book
Customer reviews
Sellers ranking

Since the online store uses the Microservice architecture pattern the product details data is spread over multiple services. For example,
Product Info Service - basic information about the product such as title, author
Pricing Service - product price
Order service - purchase history for product
Inventory service - product availability
Review service - customer reviews …

Consequently, the code that displays the product details needs to fetch information from all of these services.
Problem
How do the clients of a Microservices-based application access the individual services?
Forces
The granularity of APIs provided by microservices is often different than what a client needs. Microservices typically provide fine-grained APIs, which means that clients need to interact with multiple services. For example, as described above, a client needing the details for a product needs to fetch data from numerous services.

Different clients need different data. For example, the desktop browser version of a product details page desktop is typically more elaborate then the mobile version.

Network performance is different for different types of clients. For example, a mobile network is typically much slower and has much higher latency than a non-mobile network. And, of course, any WAN is much slower than a LAN. This means that a native mobile client uses a network that has very difference performance characteristics than a LAN used by a server-side web application. The server-side web application can make multiple requests to backend services without impacting the user experience where as a mobile client can only make a few.

The number of service instances and their locations (host+port) changes dynamically

Partitioning into services can change over time and should be hidden from clients

Services might use a diverse set of protocols, some of which might not be web friendly

Solution
Implement an API gateway that is the single entry point for all clients. The API gateway handles requests in one of two ways. Some requests are simply proxied/routed to the appropriate service. It handles other requests by fanning out to multiple services.


Rather than provide a one-size-fits-all style API, the API gateway can expose a different API for each client. For example, the Netflix API gateway runs client-specific adapter code that provides each client with an API that’s best suited to its requirements.
The API gateway might also implement security, e.g. verify that the client is authorized to perform the request
Variation: Backend for front-end
A variation of this pattern is the Backend for Front-End pattern. It defines a separate API gateway for each kind of client.

In this example, there are three kinds of clients: web application, mobile application, and external 3rd party application. There are three different API gateways. Each one is provides an API for its client.
Examples
Netflix API gateway
A simple Java/Spring API gateway from the Money Transfer example application.

Resulting context
Using an API gateway has the following benefits:
Insulates the clients from how the application is partitioned into microservices
Insulates the clients from the problem of determining the locations of service instances
Provides the optimal API for each client
Reduces the number of requests/roundtrips. For example, the API gateway enables clients to retrieve data from multiple services with a single round-trip. Fewer requests also means less overhead and improves the user experience. An API gateway is essential for mobile applications.
Simplifies the client by moving logic for calling multiple services from the client to API gateway
Translates from a “standard” public web-friendly API protocol to whatever protocols are used internally

The API gateway pattern has some drawbacks:
Increased complexity - the API gateway is yet another moving part that must be developed, deployed and managed
Increased response time due to the additional network hop through the API gateway - however, for most applications the cost of an extra roundtrip is insignificant.

Issues:
How implement the API gateway? An event-driven/reactive approach is best if it must scale to scale to handle high loads. On the JVM, NIO-based libraries such as Netty, Spring Reactor, etc. make sense. NodeJS is another option.

Related patterns
The Microservice architecture pattern creates the need for this pattern.
The API gateway must use either the Client-side Discovery pattern or Server-side Discovery pattern to route requests to available service instances.
The API Gateway may authenticate the user and pass an Access Token containing information about the user to the services
An API Gateway will use a Circuit Breaker to invoke services
An API gateway often implements the API Composition pattern

Known uses
Netflix API gateway

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末逾一,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子扎运,更是在濱河造成了極大的恐慌茂附,老刑警劉巖独榴,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谍失,死亡現(xiàn)場(chǎng)離奇詭異琼了,居然都是意外死亡谨究,警方通過(guò)查閱死者的電腦和手機(jī)恩袱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)胶哲,“玉大人畔塔,你說(shuō)我怎么就攤上這事⊙煊欤” “怎么了澈吨?”我有些...
    開(kāi)封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)寄摆。 經(jīng)常有香客問(wèn)我谅辣,道長(zhǎng),這世上最難降的妖魔是什么婶恼? 我笑而不...
    開(kāi)封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任桑阶,我火速辦了婚禮,結(jié)果婚禮上勾邦,老公的妹妹穿的比我還像新娘蚣录。我一直安慰自己,他們只是感情好眷篇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布包归。 她就那樣靜靜地躺著,像睡著了一般铅歼。 火紅的嫁衣襯著肌膚如雪公壤。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天椎椰,我揣著相機(jī)與錄音厦幅,去河邊找鬼。 笑死慨飘,一個(gè)胖子當(dāng)著我的面吹牛确憨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播瓤的,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼休弃,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了圈膏?” 一聲冷哼從身側(cè)響起塔猾,我...
    開(kāi)封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎稽坤,沒(méi)想到半個(gè)月后丈甸,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體糯俗,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年睦擂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了得湘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡顿仇,死狀恐怖淘正,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情臼闻,我是刑警寧澤鸿吆,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站些阅,受9級(jí)特大地震影響伞剑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜市埋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一黎泣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缤谎,春花似錦抒倚、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至频敛,卻和暖如春项郊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背斟赚。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工着降, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人拗军。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓任洞,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親发侵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子交掏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容