Performance: API versioning
API versioning affects server response time and client load speed by managing how API changes are delivered and cached.
module Api
module V1
class ItemsController < ApplicationController
def index
render json: { data: 'v1 data' }
end
end
end
module V2
class ItemsController < ApplicationController
def index
render json: { data: 'v2 optimized data' }
end
end
end
endclass ApiController < ApplicationController def index # No versioning, all clients get the same response render json: { data: 'all data' } end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No API versioning | N/A | N/A | Large payload slows client rendering | [X] Bad |
| Versioned API endpoints | N/A | N/A | Smaller payloads improve client rendering | [OK] Good |
namespace method to group routes under a version folder like v1.namespace :v1 do ... end is valid Rails syntax for versioning routes.namespace :v1 do get 'status', to: 'health#show' end namespace :v2 do get 'status', to: 'health#show' end
GET /v2/status call?v2 routes call controllers inside V2:: module.GET /v2/status calls V2::HealthController#show, not the v1 or main controller.namespace :v1 do resources :products end namespace :v1 do resources :orders end
namespace :v1 blocks into one improves readability and avoids confusion.name only, but v2 adds email. How should you organize your controllers to handle this cleanly?v1/users_controller.rb and v2/users_controller.rb to handle different responses.Pass the test and this pattern moves unknown