69
Lightweight APIs in mRuby 1 / 68

Lightweight APIs in mRuby (Михаил Бортник)

  • Upload
    fwdays

  • View
    300

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lightweight APIs in mRuby (Михаил Бортник)

Lightweight APIs in mRuby

1 / 68

Page 2: Lightweight APIs in mRuby (Михаил Бортник)

Who am I

2 / 68

Page 3: Lightweight APIs in mRuby (Михаил Бортник)

Who am IName's Mikhail Bortnyk

3 / 68

Page 4: Lightweight APIs in mRuby (Михаил Бортник)

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

4 / 68

Page 5: Lightweight APIs in mRuby (Михаил Бортник)

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans school

5 / 68

Page 6: Lightweight APIs in mRuby (Михаил Бортник)

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans schoolGeek, polyglot, software development addict

6 / 68

Page 7: Lightweight APIs in mRuby (Михаил Бортник)

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans schoolGeek, polyglot, software development addictgithub: @vessi

7 / 68

Page 8: Lightweight APIs in mRuby (Михаил Бортник)

Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans schoolGeek, polyglot, software development addictgithub: @vessitwitter: @mikhailbortnyk

8 / 68

Page 9: Lightweight APIs in mRuby (Михаил Бортник)

What is mRuby

9 / 68

Page 10: Lightweight APIs in mRuby (Михаил Бортник)

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012

10 / 68

Page 11: Lightweight APIs in mRuby (Михаил Бортник)

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your application

11 / 68

Page 12: Lightweight APIs in mRuby (Михаил Бортник)

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecode

12 / 68

Page 13: Lightweight APIs in mRuby (Михаил Бортник)

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecode

13 / 68

Page 14: Lightweight APIs in mRuby (Михаил Бортник)

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecodeauthor: Yukihiro 'matz' Matsumoto

14 / 68

Page 15: Lightweight APIs in mRuby (Михаил Бортник)

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecodeauthor: Yukihiro 'matz' Matsumotogithub: github://mruby/mruby

15 / 68

Page 16: Lightweight APIs in mRuby (Михаил Бортник)

What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecodeauthor: Yukihiro 'matz' Matsumotogithub: github://mruby/mrubylatest version: 1.1.0

16 / 68

Page 17: Lightweight APIs in mRuby (Михаил Бортник)

Typical Rails project

17 / 68

Page 18: Lightweight APIs in mRuby (Михаил Бортник)

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

18 / 68

Page 19: Lightweight APIs in mRuby (Михаил Бортник)

Typical Rails projectit's based on Rack

19 / 68

Page 20: Lightweight APIs in mRuby (Михаил Бортник)

Typical Rails projectit's based on Rackit contains a lot of business processes

20 / 68

Page 21: Lightweight APIs in mRuby (Михаил Бортник)

Typical Rails projectit's based on Rackit contains a lot of business processesit responds to many routes

21 / 68

Page 22: Lightweight APIs in mRuby (Михаил Бортник)

Typical Rails projectit's based on Rackit contains a lot of business processesit responds to many routesoften too heavy to serve tons of lightweight requests without scaling

22 / 68

Page 23: Lightweight APIs in mRuby (Михаил Бортник)

Typical Rails projectit's based on Rackit contains a lot of business processesit responds to many routesoften too heavy to serve tons of lightweight requests without scaling

So, what is the solution?

23 / 68

Page 24: Lightweight APIs in mRuby (Михаил Бортник)

Solutions

24 / 68

Page 25: Lightweight APIs in mRuby (Михаил Бортник)

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

25 / 68

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 areimmutable.

26 / 68

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 areimmutable.You have a problem and decide to use Java. Now you have ProblemsFactory.

27 / 68

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 areimmutable.You have a problem and decide to use Java. Now you have ProblemsFactory.You have a problem and decide...

28 / 68

Page 29: Lightweight APIs in mRuby (Михаил Бортник)

Kill it with fire!

29 / 68

Page 30: Lightweight APIs in mRuby (Михаил Бортник)

Kill it with fire!

Microservices

30 / 68

Page 31: Lightweight APIs in mRuby (Михаил Бортник)

Microservices

31 / 68

Page 32: Lightweight APIs in mRuby (Михаил Бортник)

Microservices

32 / 68

Page 33: Lightweight APIs in mRuby (Михаил Бортник)

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

33 / 68

Page 34: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

34 / 68

Page 35: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

be fast

35 / 68

Page 36: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

be fastbe lightweight

36 / 68

Page 37: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

be fastbe lightweightbe stable

37 / 68

Page 38: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

be fastbe lightweightbe stabledon't switch technology stack aggresively

38 / 68

Page 39: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

be fastbe lightweightbe stabledon't switch technology stack aggresively

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

39 / 68

Page 40: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

be fastbe lightweightbe stabledon'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 / 68

Page 41: Lightweight APIs in mRuby (Михаил Бортник)

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

Requirements:

be fastbe lightweightbe stabledon'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 / 68

Page 42: Lightweight APIs in mRuby (Михаил Бортник)

Little demonstrationMicroservice without any service

42 / 68

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 / 68

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 / 68

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 / 68

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 / 68

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 / 68

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 / 68

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 / 68

Page 50: Lightweight APIs in mRuby (Михаил Бортник)

Problems

50 / 68

Page 51: Lightweight APIs in mRuby (Михаил Бортник)

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

51 / 68

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 / 68

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 dependencyoftenly gems are written in pure C

53 / 68

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 dependencyoftenly gems are written in pure CmRuby has no require keyword, so you code will not be so DRY as you want

54 / 68

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 dependencyoftenly gems are written in pure CmRuby has no require keyword, so you code will not be so DRY as you wantstill not so fast as pure C implementation :(

55 / 68

Page 56: 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 dependencyoftenly gems are written in pure CmRuby has no require keyword, so you code will not be so DRY as you wantWRONG!still not so fast as pure C implementation :(

56 / 68

Page 57: Lightweight APIs in mRuby (Михаил Бортник)

mruby-require to the rescue!

57 / 68

Page 58: Lightweight APIs in mRuby (Михаил Бортник)

mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.

58 / 68

Page 59: Lightweight APIs in mRuby (Михаил Бортник)

mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.

located at: github://mattn/mruby-require

59 / 68

Page 60: Lightweight APIs in mRuby (Михаил Бортник)

mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.

located at: github://mattn/mruby-requiregems that are located in build_config.rb before mruby-require - are included in mRubyVM, after - are not

60 / 68

Page 61: Lightweight APIs in mRuby (Михаил Бортник)

mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.

located at: github://mattn/mruby-requiregems that are located in build_config.rb before mruby-require - are included in mRubyVM, after - are notrequirements are loaded at each execution

61 / 68

Page 62: Lightweight APIs in mRuby (Михаил Бортник)

Miss Rack?

62 / 68

Page 63: Lightweight APIs in mRuby (Михаил Бортник)

Miss Rack?mruby-hibari is the abstraction that is pretty similiar to well-known Rack interface. But justfaster a lot.

63 / 68

Page 64: Lightweight APIs in mRuby (Михаил Бортник)

Miss Rack?mruby-hibari is the abstraction that is pretty similiar to well-known Rack interface. But justfaster a lot.

Find it at github://kentaro/mruby-hibari

64 / 68

Page 65: Lightweight APIs in mRuby (Михаил Бортник)

Seeking for opensourcedevelopers!I am looking for support to continue development of mruby-mongo. Any help will beappreciated.

65 / 68

Page 66: Lightweight APIs in mRuby (Михаил Бортник)

Still actual :(

66 / 68

Page 67: Lightweight APIs in mRuby (Михаил Бортник)

Questions?

67 / 68

Page 68: Lightweight APIs in mRuby (Михаил Бортник)

Thank you!

68 / 68

Page 69: Lightweight APIs in mRuby (Михаил Бортник)