Back to Articles

Real-time Data Processing with Rails and Redis

Architecture
15/11/2024
9 min read

Real-time Data Processing with Rails and Redis

Modern web applications often require real-time functionality like live notifications, chat features, or live dashboards. Rails combined with Redis provides a robust solution for implementing these features.

Understanding Real-time Architecture

Real-time functionality in web applications typically involves:

  • Client-server communication via WebSockets
  • Pub/sub messaging for broadcasting
  • In-memory data storage for fast access

Setting Up Redis

Redis serves as both a message broker and a data store for real-time features:

# Gemfile
gem 'redis'
gem 'actioncable'

# config/cable.yml
production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>

Implementing ActionCable

ActionCable is Rails' built-in WebSocket framework:

# app/channels/application_cable/connection.rb
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private

    def find_verified_user
      # Authenticate user using session or token
    end
  end
end

# app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from "chat_#{params[:room]}"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  def send_message(data)
    Message.create!(
      room: params[:room],
      user: current_user,
      content: data['content']
    )

    ActionCable.server.broadcast(
      "chat_#{params[:room]}",
      message: data['content'],
      user: current_user.name
    )
  end
end

Redis Pub/Sub Patterns

Use Redis for more complex real-time architectures:

# Publisher
Redis.current.publish('notifications', {
  user_id: current_user.id,
  type: 'new_message',
  content: message.content
}.to_json)

# Subscriber (in a background job)
redis = Redis.new
redis.subscribe('notifications') do |on|
  on.message do |channel, message|
    data = JSON.parse(message)
    # Process the real-time notification
    ActionCable.server.broadcast(
      "notifications_#{data['user_id']}",
      data
    )
  end
end

Performance Considerations

  • Optimize WebSocket connection management
  • Use Redis clustering for high availability
  • Implement proper connection limits
  • Monitor memory usage for real-time data

Scaling Strategies

  • Horizontal scaling of ActionCable servers
  • Redis clustering for pub/sub
  • Connection load balancing
  • CDN for static assets

Conclusion

Combining Rails and Redis provides a powerful platform for real-time functionality. Proper architecture decisions and performance optimizations ensure your real-time features scale effectively.

Share this article:

Written by

Saiyyed Khhizr Aalam

Rails developer and DevOps engineer building scalable web applications.