Home

Share on Twitter Share on Facebook Share on LinkedIn

Efficient Communication with gRPC in Ruby on Rails -

Modern applications often rely on efficient communication between microservices, and gRPC has emerged as a go-to solution for this purpose. Leveraging HTTP/2 and Protocol Buffers, gRPC enables low-latency, scalable communication. In this blog, we’ll explore how to integrate gRPC into a Ruby on Rails application with a practical example.

What is gRPC?
gRPC (Google Remote Procedure Call) is a high-performance RPC framework. It offers:
 • Binary serialization with Protocol Buffers, making communication faster.
 • HTTP/2 support for multiplexing and low-latency communication.
 • Streaming capabilities for real-time data transfer.
 • Cross-language compatibility, ideal for polyglot architectures (Don't worry we'll explore this next blog).

Setting Up gRPC in a Rails Application

Step 1: Install Required Gems

Add the following gems to your Rails application’s Gemfile:
gem 'grpc'
gem 'google-protobuf'
Run bundle install to install them.

Step 2: Define Your gRPC Service

Create a .proto file to define your gRPC service. For this example, we’ll create a simple Calculator service.

calculator.proto:
syntax = "proto3";

service Calculator {
  rpc Add (Operation) returns (Result) {}
}

message Operation {
  int32 a = 1;
  int32 b = 2;
}

message Result {
  int32 value = 1;
}

This service defines an Add method that takes two numbers and returns their sum.

Step 3: Generate Ruby Code from the .proto File

Use the grpc_tools_ruby_protoc tool to generate Ruby classes:
grpc_tools_ruby_protoc -I . --ruby_out=app/lib --grpc_out=app/lib calculator.proto

This creates two files:
 • calculator_pb.rb: Contains message definitions.
 • calculator_services_pb.rb: Contains the service definition.

Step 4: Implement the gRPC Server

Create a gRPC server by extending the generated Calculator::Service class.

app/lib/calculator_server.rb:
require 'grpc'
require 'calculator_services_pb'

class CalculatorServer < Calculator::Service
  def add(operation, _unused_call)
    sum = operation.a + operation.b
    Calculator::Result.new(value: sum)
  end
end

def start_server
  server = GRPC::RpcServer.new
  server.add_http2_port('0.0.0.0:50051', :this_port_is_insecure)
  server.handle(CalculatorServer)
  puts 'gRPC server is running on localhost:50051...'
  server.run_till_terminated
end

start_server

Run this server script:
ruby app/lib/calculator_server.rb

Step 5: Implement the gRPC Client

To test the server, create a gRPC client that sends requests to the service.

app/lib/calculator_client.rb:
require 'grpc'
require 'calculator_services_pb'

def run_client
  stub = Calculator::Stub.new('localhost:50051', :this_channel_is_insecure)
  operation = Calculator::Operation.new(a: 5, b: 3)
  response = stub.add(operation)
  puts "Result of 5 + 3: #{response.value}"
end

run_client

Run the client script:
ruby app/lib/calculator_client.rb

Expected output:
Result of 5 + 3: 8

Integrating gRPC into Rails Controllers

To integrate gRPC in your Rails application, you can create a Rails controller that interacts with the gRPC client.

app/controllers/api/v1/calculator_controller.rb:
class Api::V1::CalculatorController < ApplicationController
  def add
    stub = Calculator::Stub.new('localhost:50051', :this_channel_is_insecure)
    operation = Calculator::Operation.new(a: params[:a].to_i, b: params[:b].to_i)
    response = stub.add(operation)
    render json: { result: response.value }
  end
end

Update your routes in config/routes.rb:
namespace :api do
  namespace :v1 do
    get 'add', to: 'calculator#add'
  end
end

Test the endpoint:
curl "http://localhost:3000/api/v1/add?a=10&b=20"

Expected response:
{ "result": 30 }

Benefits of Using gRPC in Rails

 • Performance: Faster communication compared to JSON-based REST APIs.
 • Scalability: HTTP/2 and streaming capabilities handle large-scale communication efficiently.
 • Cross-platform: Seamless integration with services written in other languages like Go, Python, or Java.

Conclusion
gRPC brings unmatched efficiency to Rails applications, making it a great choice for microservices or high-performance APIs. By following this example, you can integrate gRPC into your Rails projects and explore its advanced features like streaming and interceptors.

Have questions or want to share your gRPC experience? Drop a comment below! 🚀

Comments



Back to posts


🕊