AMS, API, RAILS
and a developer
LOVE
STORY
API
make the modern
web what it is
API
João Moura
co-founder at Gioco
@joaomdmoura
gioco.pro
AMS, API, RAILS
AMS, API, RAILS
and a developer
AMS, API, RAILS
and a developer
LOVE
STORY
largest city in the Americas
largest city in the Americas
largest city in the Americas
the most populous city of the
southern hemisphere
No Background Job
Bad Relationships
No Background Job
Bad Architecture
Bad Relationships
No Background Job
Bad Architecture
Bad Relationships
Indexes Missing
No Background Job
Bad Architecture
No Cache at all
Bad Relationships
Indexes Missing
No Background Job
Open Source
😘
Open Source
APIapplication programming interface
APIapplication programming interface
in·ter·face
/ˈin(t)ərˌfās/
APIapplication programming interface
in·ter·face
/ˈin(t)ərˌfās/
a point where two systems meet and interact.
ONE OF THE
GREATEST
assets
ONE OF THE
GREATEST
liabilities
Good API
Good API
Performance
Scalability
Reusability
Evolvability
Documentation
Easy to learn
Easy to use
Hard to misuse
An API should do one
thing, and do it well.“ Joshua Blonch, Google tech talk, Jan 2007
why
Rails
is the best tool!
why
Rails
is the best tool!
It’s not
why
Rails
is the best tool!
It’s not
But it’s a great one
Performance
Scalability
Reusability
Evolvability
Documentation
Easy to learn
Easy to use
Hard to misuse
Performance
Scalability
Reusability
Evolvability
Documentation
Easy to learn
Easy to use
Hard to misuse
}
Conventions
Performance
Scalability
Reusability
Evolvability
Documentation
Easy to learn
Easy to use
Hard to misuse
}
RailsConventions
}
}RailsConventions
}
Rails
is robust
rails-api
rails-api
Subset of a normal Rails application, created for
applications that don't require all functionality of
a complet...
Active Model Serializer
AMS
Bringing Convention over
Configuration to your JSON
AMS
class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :comments_count
def comments_count
object.comment...
class PostSerializer < ActiveModel::Serializer
class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :comments_count
class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :comments_count
def comments_count
object.comment...
{
id: 1,
title: "ZOMG an amazing post",
body: "Indeed, my friend",
comments_count: 5
}
AMS 0.8.x
AMS 0.9.x
AMS 0.10
1. Adapters Pattern
1. Adapters Pattern
Adapters describe how attributes and
relationships should be serialized.
{
"links": {"self": "http://example.com/p
"next": "http://example.com/po
"last": "http://example.com/po
},
"data": [{"type...
2. JSONAPI
{
"links": {"self": "http://example.com/p
"next": "http://example.com/po
"last": "http://example.com/po
},
"dat...
2. JSONAPI
A standard for building
APIs in JSON.
{
"links": {"self": "http://example.com/p
"next": "http://example.com/po
...
2. JSONAPI
A standard for building
APIs in JSON.
{
"links": {"self": "http://example.com/p
"next": "http://example.com/po
...
JSONAPI RC3
JSONAPI 1.0
JSONAPI 1.0
on May 21, 2015
3. Cache
3. Cache
New implementation, optimised and following
Rails conventions
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
end
class PostSerializer < ActiveModel::Serializer
cache
attributes :title, :body
end
class PostSerializer < ActiveModel::Serializer
cache key: 'post'
attributes :title, :body
end
class PostSerializer < ActiveModel::Serializer
cache key: 'post', expires_in: 3.hours
attributes :title, :body
end
class PostsController < ApplicationController
def index
@posts = Post.all
render json: @posts
end
end
class PostsController < ApplicationController
def index
@posts = Post.all
render json: @posts
end
end
class PostsController < ApplicationController
def index
@posts = Post.all
render json: @posts
end
def show
@post = Post.fi...
4. Fragment Cache
4. Fragment Cache
Rails conventions to cache specific attributes
class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :comments_count
def title
"Post - #{object.title}...
def title
"Post - #{object.title}"
end
def comments_count
object.comments.size
end
def title
"Post - #{object.title}"
end
def comments_count
object.comments.size
end
cacheable
def title
"Post - #{object.title}"
end
def comments_count
object.comments.size
end
cacheable
non-cacheable
class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :comments_count
def title
"Post - #{object.title}...
class PostSerializer < ActiveModel::Serializer
cache only: [:title]
attributes :title, :body, :comments_count
def title
"P...
class PostSerializer < ActiveModel::Serializer
cache except: [:comments_count]
attributes :title, :body, :comments_count
d...
class PostSerializer < ActiveModel::Serializer
cache key: 'post', expires_in: 3.hours, only: [:title]
attributes :title, :...
In god we trust…
all others bring data
0.9.x
42.680000user
system
total
real
17.630000
60.310000
71.201321
0.9.x 0.10.0
real 71.201321 63.926189
0.9.x 0.10.0
real 71.201321 57.851192
WIP
WIP
fetch multi
benchmark
and more…
one more
thing
AMS
0.10.0
another
thing
Rails 5
maybe?
special tks!
special tks!
João Moura
co-founder at Gioco
@joaomdmoura
gioco.pro
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
AMS, API, RAILS and a developer, a Love Story
Próximos SlideShares
Carregando em…5
×

AMS, API, RAILS and a developer, a Love Story

539 visualizações

Publicada em

A lot of people have being using Rails to develop both their internal or external API, but building a high quality API can be hard, and performance is a key point to achieve it.

I'll share my stories with APIs, and tell you how Active Model Serializer, component of Rails-API, helped me. AMS have being used across thousands of applications bringing convention over configuration to JSON generation.

This talk will give you a sneak peek of a new version of AMS that we have being working on, it's new cache conventions, and how it's being considered to be shipped by default in new Rails 5.

Publicada em: Tecnologia
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

AMS, API, RAILS and a developer, a Love Story

  1. 1. AMS, API, RAILS and a developer LOVE STORY
  2. 2. API
  3. 3. make the modern web what it is API
  4. 4. João Moura co-founder at Gioco @joaomdmoura gioco.pro
  5. 5. AMS, API, RAILS
  6. 6. AMS, API, RAILS and a developer
  7. 7. AMS, API, RAILS and a developer LOVE STORY
  8. 8. largest city in the Americas
  9. 9. largest city in the Americas
  10. 10. largest city in the Americas the most populous city of the southern hemisphere
  11. 11. No Background Job
  12. 12. Bad Relationships No Background Job
  13. 13. Bad Architecture Bad Relationships No Background Job
  14. 14. Bad Architecture Bad Relationships Indexes Missing No Background Job
  15. 15. Bad Architecture No Cache at all Bad Relationships Indexes Missing No Background Job
  16. 16. Open Source
  17. 17. 😘 Open Source
  18. 18. APIapplication programming interface
  19. 19. APIapplication programming interface in·ter·face /ˈin(t)ərˌfās/
  20. 20. APIapplication programming interface in·ter·face /ˈin(t)ərˌfās/ a point where two systems meet and interact.
  21. 21. ONE OF THE GREATEST assets
  22. 22. ONE OF THE GREATEST liabilities
  23. 23. Good API
  24. 24. Good API Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misuse
  25. 25. An API should do one thing, and do it well.“ Joshua Blonch, Google tech talk, Jan 2007
  26. 26. why Rails is the best tool!
  27. 27. why Rails is the best tool! It’s not
  28. 28. why Rails is the best tool! It’s not But it’s a great one
  29. 29. Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misuse
  30. 30. Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misuse }
  31. 31. Conventions Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misuse }
  32. 32. RailsConventions }
  33. 33. }RailsConventions }
  34. 34. Rails is robust
  35. 35. rails-api
  36. 36. rails-api Subset of a normal Rails application, created for applications that don't require all functionality of a complete Rails application
  37. 37. Active Model Serializer
  38. 38. AMS
  39. 39. Bringing Convention over Configuration to your JSON AMS
  40. 40. class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def comments_count object.comments.size end end
  41. 41. class PostSerializer < ActiveModel::Serializer
  42. 42. class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count
  43. 43. class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def comments_count object.comments.size end end
  44. 44. { id: 1, title: "ZOMG an amazing post", body: "Indeed, my friend", comments_count: 5 }
  45. 45. AMS 0.8.x
  46. 46. AMS 0.9.x
  47. 47. AMS 0.10
  48. 48. 1. Adapters Pattern
  49. 49. 1. Adapters Pattern Adapters describe how attributes and relationships should be serialized.
  50. 50. { "links": {"self": "http://example.com/p "next": "http://example.com/po "last": "http://example.com/po }, "data": [{"type": "posts","id": "1","title": "JSON API paints my bi "links": {"self": "http://example.com/p "author": {"self": "http://example.com/ "related": "http://example.c "linkage": { "type": "people }, "comments": {"self": "http://example.com/p "related": "http://example.co "linkage": [{ "type": "comments", "id": { "type": "comments", "id": ] } } }], "included": [{"type": "people","id": "9","first-name": "Dan","last-name": "Gebhardt", "twitter": "dgeb","links": {"self": "http://example.com/people/ } }, { "type": "comments","id": "5","body": "First!","links": {"self": "http://example.com/comments/ } }, { "type": "comments","id": "12","body": "I like XML better", "links": {"self": "http://example.com/comments/12 } }] }
  51. 51. 2. JSONAPI { "links": {"self": "http://example.com/p "next": "http://example.com/po "last": "http://example.com/po }, "data": [{"type": "posts","id": "1","title": "JSON API paints my bi "links": {"self": "http://example.com/p "author": {"self": "http://example.com/ "related": "http://example.c "linkage": { "type": "people }, "comments": {"self": "http://example.com/p "related": "http://example.co "linkage": [{ "type": "comments", "id": { "type": "comments", "id": ] } } }], "included": [{"type": "people","id": "9","first-name": "Dan","last-name": "Gebhardt", "twitter": "dgeb","links": {"self": "http://example.com/people/ } }, { "type": "comments","id": "5","body": "First!","links": {"self": "http://example.com/comments/ } }, { "type": "comments","id": "12","body": "I like XML better", "links": {"self": "http://example.com/comments/12 } }] }
  52. 52. 2. JSONAPI A standard for building APIs in JSON. { "links": {"self": "http://example.com/p "next": "http://example.com/po "last": "http://example.com/po }, "data": [{"type": "posts","id": "1","title": "JSON API paints my bi "links": {"self": "http://example.com/p "author": {"self": "http://example.com/ "related": "http://example.c "linkage": { "type": "people }, "comments": {"self": "http://example.com/p "related": "http://example.co "linkage": [{ "type": "comments", "id": { "type": "comments", "id": ] } } }], "included": [{"type": "people","id": "9","first-name": "Dan","last-name": "Gebhardt", "twitter": "dgeb","links": {"self": "http://example.com/people/ } }, { "type": "comments","id": "5","body": "First!","links": {"self": "http://example.com/comments/ } }, { "type": "comments","id": "12","body": "I like XML better", "links": {"self": "http://example.com/comments/12 } }] }
  53. 53. 2. JSONAPI A standard for building APIs in JSON. { "links": {"self": "http://example.com/p "next": "http://example.com/po "last": "http://example.com/po }, "data": [{"type": "posts","id": "1","title": "JSON API paints my bi "links": {"self": "http://example.com/p "author": {"self": "http://example.com/ "related": "http://example.c "linkage": { "type": "people }, "comments": {"self": "http://example.com/p "related": "http://example.co "linkage": [{ "type": "comments", "id": { "type": "comments", "id": ] } } }], "included": [{"type": "people","id": "9","first-name": "Dan","last-name": "Gebhardt", "twitter": "dgeb","links": {"self": "http://example.com/people/ } }, { "type": "comments","id": "5","body": "First!","links": {"self": "http://example.com/comments/ } }, { "type": "comments","id": "12","body": "I like XML better", "links": {"self": "http://example.com/comments/12 } }] }
  54. 54. JSONAPI RC3
  55. 55. JSONAPI 1.0
  56. 56. JSONAPI 1.0 on May 21, 2015
  57. 57. 3. Cache
  58. 58. 3. Cache New implementation, optimised and following Rails conventions
  59. 59. class PostSerializer < ActiveModel::Serializer attributes :title, :body end
  60. 60. class PostSerializer < ActiveModel::Serializer cache attributes :title, :body end
  61. 61. class PostSerializer < ActiveModel::Serializer cache key: 'post' attributes :title, :body end
  62. 62. class PostSerializer < ActiveModel::Serializer cache key: 'post', expires_in: 3.hours attributes :title, :body end
  63. 63. class PostsController < ApplicationController def index @posts = Post.all render json: @posts end end
  64. 64. class PostsController < ApplicationController def index @posts = Post.all render json: @posts end end
  65. 65. class PostsController < ApplicationController def index @posts = Post.all render json: @posts end def show @post = Post.find(params[:id]) render json: @post end end
  66. 66. 4. Fragment Cache
  67. 67. 4. Fragment Cache Rails conventions to cache specific attributes
  68. 68. class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end
  69. 69. def title "Post - #{object.title}" end def comments_count object.comments.size end
  70. 70. def title "Post - #{object.title}" end def comments_count object.comments.size end cacheable
  71. 71. def title "Post - #{object.title}" end def comments_count object.comments.size end cacheable non-cacheable
  72. 72. class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end
  73. 73. class PostSerializer < ActiveModel::Serializer cache only: [:title] attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end
  74. 74. class PostSerializer < ActiveModel::Serializer cache except: [:comments_count] attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end
  75. 75. class PostSerializer < ActiveModel::Serializer cache key: 'post', expires_in: 3.hours, only: [:title] attributes :title, :body end
  76. 76. In god we trust… all others bring data
  77. 77. 0.9.x 42.680000user system total real 17.630000 60.310000 71.201321
  78. 78. 0.9.x 0.10.0 real 71.201321 63.926189
  79. 79. 0.9.x 0.10.0 real 71.201321 57.851192
  80. 80. WIP
  81. 81. WIP fetch multi benchmark and more…
  82. 82. one more thing
  83. 83. AMS 0.10.0
  84. 84. another thing
  85. 85. Rails 5 maybe?
  86. 86. special tks!
  87. 87. special tks!
  88. 88. João Moura co-founder at Gioco @joaomdmoura gioco.pro

×