{"id":1925,"date":"2012-04-26T18:43:06","date_gmt":"2012-04-26T15:43:06","guid":{"rendered":"http:\/\/blog.railsware.com\/?p=1925"},"modified":"2021-08-15T15:09:47","modified_gmt":"2021-08-15T12:09:47","slug":"speed-up-your-activeresources","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/speed-up-your-activeresources\/","title":{"rendered":"Speed up your ActiveResources!"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Preface<\/h2>\n\n\n\n<p>In non trivial projects we often use <a href=\"http:\/\/en.wikipedia.org\/wiki\/Service-oriented_architecture\">SOA<\/a> approach.<br>Thereby one big monolithic application can be segregated into smaller applications or services. The benefit is obvious &#8211; it&#8217;s easy to control and maintain small applications. If you use <a href=\"http:\/\/rubyonrails.org\/\">Rails framework<\/a> you probably already use <a href=\"https:\/\/github.com\/rails\/ActiveResource\">ActiveResource<\/a> for requesting your services because if follows ActiveRecord conventions.<\/p>\n\n\n\n<p>Definitely ActiveResource operations are much slower than ActiveRecord operations because first one use HTTP connection and second one use usually <em>persistent<\/em> connection to database and database is usually always faster than application service.<\/p>\n\n\n\n<p>But modern HTTP\/1.1 servers support <a href=\"http:\/\/en.wikipedia.org\/wiki\/HTTP_persistent_connection\">persistent connection<\/a> and as Ilya Grigorik already mentioned in his awesome <a href=\"http:\/\/www.igvita.com\/2011\/10\/04\/optimizing-http-keep-alive-and-pipelining\/\">article<\/a> we can have serious speedup for resources usage.<\/p>\n\n\n\n<p>The problem is that <a href=\"https:\/\/rails.lighthouseapp.com\/projects\/8994\/tickets\/1513-activeresource-does-not-support-keep-alive-connections\">ActiveResource does not support keep-alive connections<\/a>.<\/p>\n\n\n\n<p>But not for Railsware :).<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">ActiveResource is Persistent<\/h2>\n\n\n\n<p>After playing a bit with sources we released small gem <a href=\"https:\/\/rubygems.org\/gems\/activeresource-persistent\">ActiveResource::Persistent<\/a> that allows ActiveResource to use HTTP keep-alive feature.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<p>Installation is trivial. Just drop line into your Gemfile<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">gem 'activeresource-persistent', :require => 'active_resource\/persistent'\n<\/pre>\n\n\n\n<p>And all your resources will automatically reuse connections to services!<br>You should NOT modify any line of your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Under the hood<\/h2>\n\n\n\n<p>Persistent connections are handled by excellent <a href=\"https:\/\/rubygems.org\/gems\/net-http-persistent\">net-http-persistent<\/a> library. The gem actually is very simple and provides ActiveResource::Persistent::HTTP object wrapper for ActiveResource.<\/p>\n\n\n\n<p>However we covered library with specs that ensure it will work with different ActiveResource versions.<\/p>\n\n\n\n<p>Currently it works with:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>v2.3.x<\/li><li>v3.0.x<\/li><li>v3.1.x<\/li><li>v3.2.x<\/li><\/ul>\n\n\n\n<p>Enjoy! :)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/rubygems.org\/gems\/activeresource-persistent\">ActiveResource::Persistent gem<\/a><\/li><li><a href=\"https:\/\/github.com\/railsware\/activeresource-persistent\">ActiveResource::Persistent repository<\/a><\/li><li><a href=\"http:\/\/www.igvita.com\/2011\/10\/04\/optimizing-http-keep-alive-and-pipelining\/\">Optimizing HTTP: Keep-alive and Pipelining<\/a><\/li><li><a href=\"https:\/\/rails.lighthouseapp.com\/projects\/8994\/tickets\/1513-activeresource-does-not-support-keep-alive-connections\">ActiveResource does not support keep-alive connections<\/a><\/li><li><a href=\"https:\/\/rubygems.org\/gems\/net-http-persistent\">net-http-persistent gem<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Preface In non trivial projects we often use SOA approach.Thereby one big monolithic application can be segregated into smaller applications or services. The benefit is obvious &#8211; it&#8217;s easy to control and maintain small applications. If you use Rails framework you probably already use ActiveResource for requesting your services because if follows ActiveRecord conventions. Definitely&#8230;<\/p>\n","protected":false},"author":19,"featured_media":1932,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Andriy Yanko"],"class_list":["post-1925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/1925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=1925"}],"version-history":[{"count":16,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/1925\/revisions"}],"predecessor-version":[{"id":14066,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/1925\/revisions\/14066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/1932"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=1925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=1925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=1925"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=1925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}