ActiveMicroService: A New Rails Component

april-fools-day-2019

Note: This blog post is our April Fools Prank. Bazinga! We hope we got you, if not watch out next year :)

Today, finally I can reveal the results of many months of hard work and collaboration between Cloud 66 and many Rails developers to roll out a new core component for Rails: ActiveMicroService, to be available in Rails 6. We will be dropping the first publicly available beta code for ActiveMicroService on the 1st of April.

ActiveMicroService makes extremely easy to break your Rails applications into microservices, based on best practices and a year-long experiment on the most efficient ways to break up a monolithic Rails application into hundreds of small microservices.

Here is an example of how ActiveMicroService works:

class PaymentService < ActiveMicroService::Base
    endpoint path: '/payments', availability: :internal
    authentication :jwt
    replicas 3
    
    def take_payment
        amount = params[:amount]
        user = jwt.tokens[:user]
        payment = Stripe.take_payment(user, amount)
        render payment
    end
end

As you can see, ActiveMicroService is very much like a Rails Controller. ActiveMicroService controllers, however, are aware of where they are hosted: on Kubernetes, for example, they configure Istio routing tables automatically to route traffic, configure any required authentication protocols like JWT and can be exposed internally to the cluster instead of the public internet.

Here is how you can start using ActiveMicroService on any Rails 6 application:

rake active_microservice:enable

This will generate config/active_microservice.rb:

ActiveMicroService.config do |config|
    config.orchestrator = KubernetesBinder
    config.rbac = true
    config.router = IstioRouter  # you can use IngressRouter as well
    config.namespace = 'awesome-apps'
end

KubernetesBinder provides the bridge between Rails and Kubernetes but can be replaced with DockerComposeBinder, OpenShiftBinder, MesosBinder or your own.

You can open this file and make the needed changes. Once done, run this:

rake active_microservice:configure

This will generate a single yaml that contains everything your cluster needs to run the application, including Kuberentes Service Accounts, Secrets, Deployments, etc. You can run it like this:

rake active_microservice:configure | kubectl apply -f -

From now on, you can deploy the application with a single command like this:

rake active_microservice:deploy | kubectl apply -f -

We believe this is the best Rails deployment experience you can ever have while benefiting from new infrastructure tools like Kubernetes without learning all their details.

Running your Rails applications on Kubernetes is not about microservices. Using containers and Kubernetes reduces infrastructure costs by a large factor, makes your applications much more resilient and gives you a great deal of flexibility in choosing application components and hosting providers out there.

We hope you enjoy this as much as we enjoyed making Rails deployments easier!

active-micro-service-a-new-rails-component-happy-april-fools-day

Try Cloud 66 for Free, No credit card required