7. Service Oriented Design
Behind linkedcare’s micro services
!
“The goal of SOD is to split up the parts of an
application into components that can be iterated
on, improved and fixed…”
Service Oriented Design with ROR Book by Paul Dix
9. Service Oriented Design
Behind linkedcare’s micro services
Client
Schedule
Bio &
Vital
SensorsUID
Pro
• Current topology;
• Micro services control well
defined and secure;
• Best for monolithic to
service oriented design;
• Single point of failure;
• Latency.
10. Service Oriented Design
Behind linkedcare’s micro services
!
!
Schedule
Bio &
Vital
SensorsUID
Client
• Future topology;
Pro
• Client controls micro
services;
• “There’s no more” a single
point of failure;
• Latency;
• Exposed micro services.
12. Service Oriented Design
Behind linkedcare’s micro services
!
• Small services with small tasks;
• Well defined boundaries;
• Business logic for who needs it;
13. Service Oriented Design
Behind linkedcare’s micro services
!
• Use well known standards and conventions;
• RESTful APIs:
• Standard HTTP methods;
• One does not simply use POST /v1/events/1 to delete an event;
• Use HTTP Status Codes, and everything will be 200.
14. Service Oriented Design
Behind linkedcare’s micro services
!
• Use only the necessary tools for the job.
• We want MICRO Services;
• Lightweight and flexible systems;
• Sinatra is one of them..
15. Service Oriented Design
Behind linkedcare’s micro services
!
• Isolation;
The goods
• Surviving lost of functionality;
• Scalability.
• …
Ruby doesn’t scale, but services do!
16. Service Oriented Design
Behind linkedcare’s micro services
!
• Distributed system complexity;
The “not so” goods
• Dev Ops overhead;
• Synchronization;
• …
17. !
!
Sinatra rb
"Sinatra is a DSL for quickly creating web
applications in Ruby with minimal effort"
The right tool(s) for the job*
Behind linkedcare’s micro services
18. 1. gem install ‘sinatra’
Sinatra rb
Behind linkedcare’s micro services
3. ruby app.rb
require ‘sinatra’
!
get ‘/’ do
“Hello!”
end
2.
19. Sinatra rb
Behind linkedcare’s micro services
[2014-05-15 21:00:00] INFO WEBrick 1.3.1
[2014-05-15 21:00:00] INFO ruby 1.9.3 (2013-06-27) [x86_64-darwin12.5.0]
== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from WEBrick
[2014-05-15 21:00:00] INFO WEBrick::HTTPServer#start: pid=5343 port=4567
20. Sinatra rb
Behind linkedcare’s micro services
• Sinatra is written over 2000 lines of code, it’s 1%
of Rails;
• Fast, flexible and light… like our micro services
should be;
• Active Record, Rake tasks, Rspec, Bundler…
it’s all available.
The goods
21. Sinatra rb
Behind linkedcare’s micro services
• It’s pure Ruby, all Rails magic its gone;
• Structuring an app can bring some pain;
• There are some gems that only work well with
Rails.
The “not so” goods
For Rails only devs
23. Inside a Sinatra App
Behind linkedcare’s micro services
ROUTING
get '/hello/:name' do
# matches "GET /hello/foo" and "GET /hello/bar"
# params[:name] is 'foo' or 'bar'
"Hello #{params[:name]}!"!
end
post '/say/*/to/*' do
# matches post /say/hello/to/world
params[:splat] # => ["hello", "world"]
end
24. Inside a Sinatra App
Behind linkedcare’s micro services
ROUTING
delete '/event/:id' do
# matches "DELETE /event/1"
end
put '/event/:id' do
# matches "PUT /event/1"
end
25. Inside a Sinatra App
Behind linkedcare’s micro services
HELPERS
require 'sinatra'
!
get '/:color' do
"<b>#{color_world(params[:color])} world!</b>"
end
!
helpers do
def color_world(red)
red == "red" ? "red" : "yellow"
end
end
26. Inside a Sinatra App
Behind linkedcare’s micro services
APP STRUCTURE
Classic Style vs Modular Style
27. Inside a Sinatra App
Behind linkedcare’s micro services
CLASSIC | APP STRUCTURE
• require ‘sinatra’
• Uses the TOP Level DSL included when
required;
!
28. Inside a Sinatra App
Behind linkedcare’s micro services
MODULAR | APP STRUCTURE
• require ‘sinatra/base’
• DSL it’s not included in Object.
• class MyApp < Sinatra::Base
• DSL available inside MyApp object.
!
31. Microservic’ing and Sinatra
@linkedcare
Some tips
LinkedBus => Ruby RabbitMQ Subscriber
https://github.com/tdantas/linkedbus
SmoothOperator => Lightweight and flexible alternative to ActiveResource,
https://github.com/goncalvesjoao/smooth_operator
UseCasing => Responsability Centric Design with use cases
https://github.com/tdantas/usecasing