Ruby on Rails Backend Development for Mobile Applications
Rails is chosen for mobile backend when you need MVP development speed, startup pace, or when product already lives on Rails and scales via horizontal instance addition. Ecosystem is mature: Devise, Pundit, Sidekiq, Active Storage — all production-ready tools with years of battle testing.
What Often Breaks in Rails API for Mobile
ActiveRecord callbacks chain unexpectedly. after_create :send_push_notification in User model — each User.create! in tests, rake tasks or data migration tries FCM send. Mobile client has nothing to do with it, but catching regression in production is unpleasant. For side effects use Service Objects or event processing via ActiveSupport::Notifications.
N+1 through serialization. ActiveModelSerializers or fast_jsonapi (jsonapi-serializer) with has_many relations — if not passing include:, each nested object loads separately. Diagnose via Bullet gem in development, fix via includes(:association) before serialization.
Stack for Mobile API
Rails 7.x in API-only mode (rails new myapp --api), PostgreSQL, Redis + Sidekiq for background tasks. Authentication — Devise + devise-jwt (JWT via JWTSessions) or Rodauth for more flexible token control.
Push notifications — rpush gem: supports APNs (HTTP/2) and FCM, manages connection pool, batch send, logs delivery. Sidekiq job for bulk sends with bulk_push doesn't load HTTP handler.
File storage — Active Storage with S3 adapter. For mobile client — presigned URL via blob.service_url_for_direct_upload so client uploads directly to S3, bypassing Rails server.
Case: lifestyle app for iOS/Android, 40,000 MAU. Rails 6 API, PostgreSQL, Sidekiq. Endpoint /api/v1/feed — feed with posts, likes, comments. Response time reached 1.2 seconds. Problem: serializer loaded user, likes_count, comments_count separately per post. Solution: switch to jsonapi-serializer with explicit includes(:user) and counter_cache: true for likes and comments at DB level. Result: 80 ms on typical 20-post fetch.
Code Organization
Rails API-only project with service objects:
app/
├── controllers/api/v1/ — thin controllers, HTTP logic only
├── services/ — business logic (CreateOrderService, ProcessPaymentService)
├── serializers/ — jsonapi-serializer
├── jobs/ — Sidekiq jobs
└── policies/ — Pundit policies for authorization
API versioning via namespace (/api/v1, //v2) — mandatory from day one. Mobile client updates slowly, old versions live 6–12 months parallel to new.
Performance and Caching
Russian Doll Caching — Rails fragment caching via cache(model) works in API mode too via etag. For aggregates (counters, ratings) — Rails.cache with Redis, invalidation via after_commit callback.
Rack::Attack for rate limiting: limit by IP and by token, so mobile client with stuck retry loop doesn't kill DB.
Timeline: MVP API with authentication, basic CRUD, pushes — 2–4 weeks. Production backend with payments, realtime via ActionCable and full DevOps — 8–12 weeks.







