Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Rails BFF
Microservices Meetup vol.5 (API Gateway & BFF)
shotat @Recruit Lifestyle
• @shotat (GitHub, Twitter)
• 2015.04 ~ 2015.12

SIer
• 2016.01 ~ Now

• Ruby, Elixir, Vim, Docker
Case Study:




BFF
Agenda
• HPB
•
• API
• Rails & Spring
• BFF Rails
•
Agenda
• HPB ◁
•
• API
• Rails & Spring
• BFF Rails
•
•
• 2007
• ※ HPB
HPB
10 

=> & 

=> 😥
• 

- 

-
• 

- 

-
MicroServices ?
Monolithic App HPB
Client
(web)
Monolith
MicroServices & BFF
Client
(web)
BFF Services
BFF(API Gateway) API
Client
(web)
BFF Services
Services
Client
(web)
BFF Services
Client
(web)
BFF Services
HPB MicroServices
• View/Logic
• Service
•
MicroServices 

😢
Agenda
• HPB
• ◁
• Rails & Spring
• API
• BFF Rails
•


BFF / API Server

MicroServices & BFF
Client
(web)
BFF Services
BFF & API (services)
Client
(web)
BFF API Server
BFF & API (services)
Client
(web)
BFF API Server
BFF / API
BFF / API Server
Client
(web)
BFF API Server
New App
Old App
Strangler Application
Router(Proxy)

 
 



BFF / API Server
Client
(web)
BFF API Server
New App
Old App
New App
BFF API Server
Client
(web)
scope
Old App
Agenda
• HPB
•
• API ◁
• Rails & Spring
• BFF Rails
•
API
• REST 

(BFF or API Gateway API
)
• API
• => REST
Lorem ipsum dolor sit amet
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur
Lorem ipsum
Lorem
I...
Lorem ipsum dolor sit amet
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur
Lorem ipsum
Lorem
I...
Lorem ipsum dolor sit amet
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur
Lorem ipsum
Lorem
I...
Agenda
• HPB
•
• API
• Rails & Spring ◁
• BFF Rails
•
API Server: Spring Boot
• Java
•
•
• Doc Swagger

BFF Layer: Ruby on Rails
•
•
• Rails 

BFF Rails
•
•
•
➜ Node.js, Golang 

🤔
• : BFF / API Server
• :
• : SPA
➜ Ruby Rails
Agenda
• HPB
•
• API
• Rails & Spring
• BFF Rails ◁
•
BFF layer
• Web API Gateway

iOS, Android BFF
• API ( + caching)
• session
• (SS) rendering
Rails
Model
ControllerView
USER
DB
Ruby on Rails
ActiveRecord Pattern
Model
ControllerView
USER
DB
Ruby on Rails
ActiveRecord Pattern
Model
ControllerView
USER
DB
BFF API 

ActiveRecord Pattern
×
API
Rails BFF
• Active Record DB
• Rails 

Model
ControllerView
USER
DB
Ruby on Rails
ActiveRecord Pattern
BACKEND
Model
Repository
API
Data Accessor
API Aggregator
ControllerView
USER
BFF
BACKEND
Model
Repository
API
Data Accessor
API Aggregator
ControllerView
USER
BFF


MVC + R
• One Repository : One Endpoint
• Model Aggregator
• : Controller Rails 

• :
Rails
BFF 🤔
• API 

• API 



Invalidation
• API 

=> 

Blocking IO
• API 

=>
• API 

=> 

Blocking IO
• API 

=>
➜
Performance
API call
throughput: 30 rps/host
response time: 120 ms
※ Spec

CPU: 2 Cores, 3.20GHz, Intel(R) Xeon(R) CPU E5-...
Response Time 120 ms
• Rails : 40ms
• NW : 10ms
• API 

IO 70ms
Performance
• API
• NW
• 

Performance
• API
• NW
• 

➜ …?
• 120ms
• throughput 



• 120ms
• throughput 



➜
…
BFF
• Throughput
• ResponseTime
Throughput
Throughput :



Rack Puma
What’s Puma?
• Puma: Thread Rack
• IO wait Thread 



=> IO wait BFF
• HPB Unicorn 

Puma
• Request thread 

CPU throughput 

• API 

1
Throughput :



BFF
BFF
• CPU BFF 

= Ruby(Rails)
• View
• BFF 

API
ResponseTime
ResponseTime :



API request
• IO wait 

API
• Faraday + Typhoeus gem
• :

debug
ResponseTime :



API caching
API cache
• API
• :

caching invalidation
ResponseTime :



API Gateway
API Gateway
• BFF batch API Gateway
• App
• : mercari/widebullet

JSON-RPC batch request-> HTTP requests
• :

API Gateway
BFF API Server
API Gateway

e.g. Widebullet
API Gateway
BFF API Server
API Gateway

e.g. Widebullet


API Gateway
BFF API Server
API Gateway

e.g. Widebullet


NW IO wait
120ms 75ms
BFF
• Throughput 

=> BFF
• ResponseTime 

=> API 

API Gateway
Agenda
• HPB
•
• API
• Rails & Spring
• BFF Rails
• ◁
BFF Rails: pros/cons
naive 

Railsで作るBFFの功罪
Railsで作るBFFの功罪
Railsで作るBFFの功罪
Railsで作るBFFの功罪
Upcoming SlideShare
Loading in …5
×

Railsで作るBFFの功罪

4,380 views

Published on

2017/03/30に行われた Microservices Meetup vol.5 (API Gateway & BFF) の発表資料です。
Ruby on RailsでBFFを構築した際の知見についてご紹介致します。

Published in: Engineering
  • Be the first to comment

Railsで作るBFFの功罪

  1. 1. Rails BFF Microservices Meetup vol.5 (API Gateway & BFF) shotat @Recruit Lifestyle
  2. 2. • @shotat (GitHub, Twitter) • 2015.04 ~ 2015.12
 SIer • 2016.01 ~ Now
 • Ruby, Elixir, Vim, Docker
  3. 3. Case Study: 
 
 BFF
  4. 4. Agenda • HPB • • API • Rails & Spring • BFF Rails •
  5. 5. Agenda • HPB ◁ • • API • Rails & Spring • BFF Rails •
  6. 6. • • 2007 • ※ HPB
  7. 7. HPB 10 
 => & 
 => 😥
  8. 8. • 
 - 
 - • 
 - 
 -
  9. 9. MicroServices ?
  10. 10. Monolithic App HPB Client (web) Monolith
  11. 11. MicroServices & BFF Client (web) BFF Services
  12. 12. BFF(API Gateway) API Client (web) BFF Services
  13. 13. Services Client (web) BFF Services
  14. 14. Client (web) BFF Services
  15. 15. HPB MicroServices • View/Logic • Service •
  16. 16. MicroServices 
 😢
  17. 17. Agenda • HPB • ◁ • Rails & Spring • API • BFF Rails •
  18. 18. 
 BFF / API Server

  19. 19. MicroServices & BFF Client (web) BFF Services
  20. 20. BFF & API (services) Client (web) BFF API Server
  21. 21. BFF & API (services) Client (web) BFF API Server
  22. 22. BFF / API
  23. 23. BFF / API Server Client (web) BFF API Server New App Old App
  24. 24. Strangler Application Router(Proxy) 
 
 
 

  25. 25. BFF / API Server Client (web) BFF API Server New App Old App
  26. 26. New App BFF API Server Client (web) scope Old App
  27. 27. Agenda • HPB • • API ◁ • Rails & Spring • BFF Rails •
  28. 28. API • REST 
 (BFF or API Gateway API ) • API • => REST
  29. 29. Lorem ipsum dolor sit amet Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum dolor sit amet, consectetur Lorem ipsum Lorem Ipsum Lorem HPB •
  30. 30. Lorem ipsum dolor sit amet Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum dolor sit amet, consectetur Lorem ipsum Lorem Ipsum Lorem HPB • UI 
 ( ) • • + 
 +
  31. 31. Lorem ipsum dolor sit amet Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum dolor sit amet, consectetur Lorem ipsum Lorem Ipsum Lorem HPB • BFF call API 7 
 - x 5
 - 
 - 
 • API 
 - parameter 
 - View + 
 +
  32. 32. Agenda • HPB • • API • Rails & Spring ◁ • BFF Rails •
  33. 33. API Server: Spring Boot • Java • • • Doc Swagger

  34. 34. BFF Layer: Ruby on Rails • • • Rails 

  35. 35. BFF Rails • • • ➜ Node.js, Golang 
 🤔
  36. 36. • : BFF / API Server • : • : SPA ➜ Ruby Rails
  37. 37. Agenda • HPB • • API • Rails & Spring • BFF Rails ◁ •
  38. 38. BFF layer • Web API Gateway
 iOS, Android BFF • API ( + caching) • session • (SS) rendering
  39. 39. Rails
  40. 40. Model ControllerView USER DB Ruby on Rails ActiveRecord Pattern
  41. 41. Model ControllerView USER DB Ruby on Rails ActiveRecord Pattern
  42. 42. Model ControllerView USER DB BFF API 
 ActiveRecord Pattern × API
  43. 43. Rails BFF • Active Record DB • Rails 

  44. 44. Model ControllerView USER DB Ruby on Rails ActiveRecord Pattern
  45. 45. BACKEND Model Repository API Data Accessor API Aggregator ControllerView USER BFF
  46. 46. BACKEND Model Repository API Data Accessor API Aggregator ControllerView USER BFF 

  47. 47. MVC + R • One Repository : One Endpoint • Model Aggregator • : Controller Rails 
 • :
  48. 48. Rails
  49. 49. BFF 🤔 • API 
 • API 
 
 Invalidation
  50. 50. • API 
 => 
 Blocking IO • API 
 =>
  51. 51. • API 
 => 
 Blocking IO • API 
 => ➜
  52. 52. Performance API call throughput: 30 rps/host response time: 120 ms ※ Spec
 CPU: 2 Cores, 3.20GHz, Intel(R) Xeon(R) CPU E5-2667 v4
 Memory: 8GB
  53. 53. Response Time 120 ms • Rails : 40ms • NW : 10ms • API 
 IO 70ms
  54. 54. Performance • API • NW • 

  55. 55. Performance • API • NW • 
 ➜ …?
  56. 56. • 120ms • throughput 
 

  57. 57. • 120ms • throughput 
 
 ➜
  58. 58.
  59. 59. BFF
  60. 60. • Throughput • ResponseTime
  61. 61. Throughput
  62. 62. Throughput :
 
 Rack Puma
  63. 63. What’s Puma? • Puma: Thread Rack • IO wait Thread 
 
 => IO wait BFF • HPB Unicorn 

  64. 64. Puma • Request thread 
 CPU throughput 
 • API 
 1
  65. 65. Throughput :
 
 BFF
  66. 66. BFF • CPU BFF 
 = Ruby(Rails) • View • BFF 
 API
  67. 67. ResponseTime
  68. 68. ResponseTime :
 

  69. 69. API request • IO wait 
 API • Faraday + Typhoeus gem • :
 debug
  70. 70. ResponseTime :
 
 API caching
  71. 71. API cache • API • :
 caching invalidation
  72. 72. ResponseTime :
 
 API Gateway
  73. 73. API Gateway • BFF batch API Gateway • App • : mercari/widebullet
 JSON-RPC batch request-> HTTP requests • :

  74. 74. API Gateway BFF API Server API Gateway
 e.g. Widebullet
  75. 75. API Gateway BFF API Server API Gateway
 e.g. Widebullet 

  76. 76. API Gateway BFF API Server API Gateway
 e.g. Widebullet 

  77. 77. NW IO wait 120ms 75ms
  78. 78. BFF
  79. 79. • Throughput 
 => BFF • ResponseTime 
 => API 
 API Gateway
  80. 80. Agenda • HPB • • API • Rails & Spring • BFF Rails • ◁
  81. 81. BFF Rails: pros/cons naive 


×