+ All Categories
Home > Software > Lightweight APIs in mRuby

Lightweight APIs in mRuby

Date post: 11-Apr-2017
Category:
Upload: pivorak-meetup
View: 2,255 times
Download: 0 times
Share this document with a friend
58
Lightweight APIs in mRuby 1 / 57
Transcript
Page 1: Lightweight APIs in mRuby

Lightweight APIs in mRuby

1 / 57

Page 2: Lightweight APIs in mRuby

Who am I

2 / 57

Page 3: Lightweight APIs in mRuby

Who am IName's Mikhail Bortnyk

3 / 57

Page 4: Lightweight APIs in mRuby

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music Ukraine

4 / 57

Page 5: Lightweight APIs in mRuby

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music Ukraine

Co-creator of Kottans school

5 / 57

Page 6: Lightweight APIs in mRuby

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music Ukraine

Co-creator of Kottans school

Geek, polyglot, software development addict

6 / 57

Page 7: Lightweight APIs in mRuby

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music Ukraine

Co-creator of Kottans school

Geek, polyglot, software development addict

github: @vessi

7 / 57

Page 8: Lightweight APIs in mRuby

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music Ukraine

Co-creator of Kottans school

Geek, polyglot, software development addict

github: @vessi

twitter: @mikhailbortnyk

8 / 57

Page 9: Lightweight APIs in mRuby

What is mRuby

9 / 57

Page 10: Lightweight APIs in mRuby

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to

ISO/IEC 30170:2012

10 / 57

Page 11: Lightweight APIs in mRuby

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to

ISO/IEC 30170:2012

can be linked and embedded into your application

11 / 57

Page 12: Lightweight APIs in mRuby

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to

ISO/IEC 30170:2012

can be linked and embedded into your application

can compile your software into bytecode

12 / 57

Page 13: Lightweight APIs in mRuby

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to

ISO/IEC 30170:2012

can be linked and embedded into your application

can compile your software into bytecode

can generate C code for embedding bytecode

13 / 57

Page 14: Lightweight APIs in mRuby

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to

ISO/IEC 30170:2012

can be linked and embedded into your application

can compile your software into bytecode

can generate C code for embedding bytecode

author: Yukihiro 'matz' Matsumoto

14 / 57

Page 15: Lightweight APIs in mRuby

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to

ISO/IEC 30170:2012

can be linked and embedded into your application

can compile your software into bytecode

can generate C code for embedding bytecode

author: Yukihiro 'matz' Matsumoto

github: github://mruby/mruby

15 / 57

Page 16: Lightweight APIs in mRuby

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to

ISO/IEC 30170:2012

can be linked and embedded into your application

can compile your software into bytecode

can generate C code for embedding bytecode

author: Yukihiro 'matz' Matsumoto

github: github://mruby/mruby

latest version: 1.1.0

16 / 57

Page 17: Lightweight APIs in mRuby

Typical Rails project

17 / 57

Page 18: Lightweight APIs in mRuby

Typical Rails project (I am so sorry for it,mom!)

18 / 57

Page 19: Lightweight APIs in mRuby

Typical Rails projectit's based on Rack

19 / 57

Page 20: Lightweight APIs in mRuby

Typical Rails projectit's based on Rack

it contains a lot of business processes

20 / 57

Page 21: Lightweight APIs in mRuby

Typical Rails projectit's based on Rack

it contains a lot of business processes

it responds to many routes

21 / 57

Page 22: Lightweight APIs in mRuby

Typical Rails projectit's based on Rack

it contains a lot of business processes

it responds to many routes

often too heavy to serve tons of lightweight requests without scaling

22 / 57

Page 23: Lightweight APIs in mRuby

Typical Rails projectit's based on Rack

it contains a lot of business processes

it responds to many routes

often too heavy to serve tons of lightweight requests without scaling

So, what is the solution?

23 / 57

Page 24: Lightweight APIs in mRuby

Solutions

24 / 57

Page 25: Lightweight APIs in mRuby

SolutionsYou have a problem and decide to use threads. Two now problems have

you.

25 / 57

Page 26: Lightweight APIs in mRuby

SolutionsYou have a problem and decide to use threads. Two now problems have

you.

You have a problem and decide to use functional languages. Now your

problems are immutable.

26 / 57

Page 27: Lightweight APIs in mRuby

SolutionsYou have a problem and decide to use threads. Two now problems have

you.

You have a problem and decide to use functional languages. Now your

problems are immutable.

You have a problem and decide to use Java. Now you have

ProblemsFactory.

27 / 57

Page 28: Lightweight APIs in mRuby

SolutionsYou have a problem and decide to use threads. Two now problems have

you.

You have a problem and decide to use functional languages. Now your

problems are immutable.

You have a problem and decide to use Java. Now you have

ProblemsFactory.

You have a problem and decide...

28 / 57

Page 29: Lightweight APIs in mRuby

Kill it with fire!

29 / 57

Page 30: Lightweight APIs in mRuby

Kill it with fire!

Microservices

30 / 57

Page 31: Lightweight APIs in mRuby

Microservices

31 / 57

Page 32: Lightweight APIs in mRuby

Microservices

32 / 57

Page 33: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

33 / 57

Page 34: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

34 / 57

Page 35: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

be fast

35 / 57

Page 36: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

be fast

be lightweight

36 / 57

Page 37: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

be fast

be lightweight

be stable

37 / 57

Page 38: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

be fast

be lightweight

be stable

don't switch technology stack aggresively

38 / 57

Page 39: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

be fast

be lightweight

be stable

don't switch technology stack aggresively

So, I decided to use mRuby. What do we have for serving web here?

39 / 57

Page 40: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

be fast

be lightweight

be stable

don't switch technology stack aggresively

So, I decided to use mRuby. What do we have for serving web here?

mod_mruby. Apache module that executes mRuby code inside Apache

process.

40 / 57

Page 41: Lightweight APIs in mRuby

MicroservicesThe idea is to split big project in bunch of independent lightweight services.

Requirements:

be fast

be lightweight

be stable

don't switch technology stack aggresively

So, I decided to use mRuby. What do we have for serving web here?

mod_mruby. Apache module that executes mRuby code inside Apache

process.

ngx_mruby. Perfectly the same but with Nginx flavour.

41 / 57

Page 42: Lightweight APIs in mRuby

Little demonstrationMicroservice without any service

42 / 57

Page 43: Lightweight APIs in mRuby

Little demonstrationUsed software:

nginx (ALL)

docker (ALL)

docker-compose (ALL)

mongodb (ALL)

ab (ALL)

unicorn gem (rack)

rack gem (rack)

rack-routing gem (rack)

mongo gem (rack)

ngx_mruby (mRuby)

mruby-mongo (mRuby)

43 / 57

Page 44: Lightweight APIs in mRuby

Rack execution$> cd testlab/rack$> docker-compose up$> ab -n 10000 -c 100 http://192.168.0.100:10080/api/contacts# and kill docker-compose with ^C

44 / 57

Page 45: Lightweight APIs in mRuby

Rack stats (page 1)Document Path: /api/contactsDocument Length: 537 bytes

Concurrency Level: 100Time taken for tests: 7.897 secondsComplete requests: 10000Failed requests: 4399 (Connect: 0, Receive: 0, Length: 4399, Exceptions: 0)Non-2xx responses: 5601Total transferred: 5080859 bytesHTML transferred: 3434440 bytesRequests per second: 1266.27 [#/sec] (mean)Time per request: 78.972 [ms] (mean)Time per request: 0.790 [ms] (mean, across all concurrent requests)Transfer rate: 628.29 [Kbytes/sec] received

45 / 57

Page 46: Lightweight APIs in mRuby

Rack stats (page 2)Connection Times (ms) min mean[+/-sd] median maxConnect: 0 4 4.1 2 34Processing: 1 74 72.1 23 423Waiting: 1 74 72.0 22 423Total: 2 78 72.2 27 426

Percentage of the requests served within a certain time (ms) 50% 27 66% 135 75% 147 80% 154 90% 174 95% 192 98% 216 99% 232 100% 426 (longest request)

46 / 57

Page 47: Lightweight APIs in mRuby

mRuby execution$> cd testlab/rack$> docker-compose up$> ab -n 10000 -c 100 http://192.168.0.100:10080/api/contacts# and kill docker-compose with ^C

47 / 57

Page 48: Lightweight APIs in mRuby

mRuby stats (page 1)Document Path: /api/contactsDocument Length: 98 bytes

Concurrency Level: 100Time taken for tests: 6.721 secondsComplete requests: 10000Failed requests: 0Total transferred: 2140000 bytesHTML transferred: 980000 bytesRequests per second: 1487.83 [#/sec] (mean)Time per request: 67.212 [ms] (mean)Time per request: 0.672 [ms] (mean, across all concurrent requests)Transfer rate: 310.93 [Kbytes/sec] received

48 / 57

Page 49: Lightweight APIs in mRuby

mRuby stats (page 2)Connection Times (ms) min mean[+/-sd] median maxConnect: 0 3 5.9 0 29Processing: 5 64 27.5 68 132Waiting: 5 64 27.6 68 131Total: 14 67 24.4 69 132

Percentage of the requests served within a certain time (ms) 50% 69 66% 78 75% 83 80% 88 90% 102 95% 106 98% 112 99% 120 100% 132 (longest request)

49 / 57

Page 50: Lightweight APIs in mRuby

Problems

50 / 57

Page 51: Lightweight APIs in mRuby

ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.

51 / 57

Page 52: Lightweight APIs in mRuby

ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.

you have to recompile mRuby each time you add some dependency

52 / 57

Page 53: Lightweight APIs in mRuby

ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.

you have to recompile mRuby each time you add some dependency

oftenly gems are written in pure C

53 / 57

Page 54: Lightweight APIs in mRuby

ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.

you have to recompile mRuby each time you add some dependency

oftenly gems are written in pure C

mRuby has no require keyword, so you code will not be so DRY as you

want

54 / 57

Page 55: Lightweight APIs in mRuby

ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.

you have to recompile mRuby each time you add some dependency

oftenly gems are written in pure C

mRuby has no require keyword, so you code will not be so DRY as you

want

still not so fast as pure C implementation :(

55 / 57

Page 56: Lightweight APIs in mRuby

Questions?

56 / 57

Page 57: Lightweight APIs in mRuby

Thank you!

57 / 57

Page 58: Lightweight APIs in mRuby

Recommended