TruffleRuby on OCI Container Instances

January 18, 2023 | 5 minute read
Ewan Slater
Director of Technical Product Marketing, Java & GraalVM
Text Size 100%:

Elegance at Speed

Ruby is one of my favourite programming languages.  I really appreciate how expressive it is, how productive (and how much fun) it makes development.

Unfortunately Ruby has a reputation for being slow compared to other languages such as JavaScript.

While the performance of the default Ruby interpreter has improved, it still hasn't had the engineering investment that goes into V8.

I want to introduce you to an alternative Ruby engine, and a really simple way to deploy Ruby applications in the cloud.

TruffleRuby

TruffleRuby is a high - performance implementation of Ruby, built on top of GraalVM.

GraalVM is a high - performance, polyglot virtual machine.

In benchmark tests, TruffleRuby often delivers superior performance to other Ruby implementations.

You can install TruffleRuby using commonly used Ruby managers such as RVMchruby && ruby-install, and rbenv.

Once you've installed TruffleRuby, you can use it just like the standard Ruby engine.  Typically I set TruffleRuby as my default runtime.

TruffleRuby is an open-source project.  GraalVM is developed via the open - source project.  GraalVM is available in two flavours:

  • Community Edition (GraalVM CE) - the open - source edition
  • Enterprise Edition - includes premium enhancements not included in the open - source edition

Enterprise Edition requires a subscription for production use.  This is included at no additional cost for workloads running on Oracle Cloud infrastructure (OCI).

OCI Container Instances

Since I like Ruby, it's probably not a surprise that I like to keep things simple.

OCI Container Instances is a simple, serverless, cloud service for running applications in containers.

Essentially you just create an instance, specifying the image you want to use, and the service runs it for you.

So I thought I'd quickly show you how you can use Container Instances to run a simple Sinatra web application in a container, with TruffleRuby as the Ruby runtime.

Hands - On

Pre - requisites

  • OCI account (paid or trial with credits)
  • VCN with public subnet created (see here for a tutorial if required)

App

The app is about the simplest Sinatra app you can create:


require 'sinatra'

get '/' do
  'Hello world!'
end

Dockerfile

I'm going to use publicly available GraalVM CE images containing TruffleRuby to build from.

These don't require any license.

I install some dependencies for building native extensions and then the sinatra and thin gems.

Then copy in the app from above:


FROM ghcr.io/graalvm/truffleruby:latest as build
RUN dnf install -y xz patch
RUN gem install sinatra thin

FROM ghcr.io/graalvm/truffleruby:slim as runner
COPY --from=build /opt/truffleruby-22.3.0/lib/gems /opt/truffleruby-22.3.0/lib/gems
COPY myapp.rb .
ENV APP_ENV=production
EXPOSE 4567
CMD ["ruby","myapp.rb"]

Then build the image and push it to your repository of choice.

In my case, I have chosen to push it to a private repository in OCI.  Since the plan is to deploy to OCI, this makes sense, but you could push it to any repository you have access to.

In my case the image is:
oracledeveloper/burton7/truffle:ce

Container Instance

Click on "Container Instances" and then "Create Container Instance":

Just accept the defaults for the moment and click "Next" (make sure your subnet is public):

Select your image and enter your credentials if you're using a private repository:

Then click "Next".

The final screen should look something like this:

Click create.  You should get a screen showing an orange "CI" icon and the message "CREATING":

After a few minutes, the icon should change to green, and the public IP address of the instance will be displayed:

If you then open another tab with the URL http://<public IP>:4567 you should get the traditional "Hello world!" message:

Next steps

You can follow the template to spin up an app that does something more meaningful!

Summary

TruffleRuby is the high - performance implementation of Ruby built on the GraalVM.

In benchmark tests it is often the fastest Ruby implementation.

OCI Container Instances is a serverless compute service for running containers simply.

Together they enable you to easily run high performance Ruby applications in containers.

Ewan Slater

Director of Technical Product Marketing, Java & GraalVM

Ewan Slater is the Technical Director of Java & GraalVM product marketing at Oracle with over twenty years experience in the technology industry.

Ewan started programming in Java in 1997 and joined Oracle with the acquisition of Thor Technology in 2005.

His current focus is on helping Oracle’s customers and partners understand the technical benefits of Java and GraalVM.

He is involved with a number of open source projects, is a recovering conference organiser and a member of the Ipswich & Suffolk Technology Network (ISTN).

Outside of work, he can usually be found outdoors in the Suffolk countryside, or indoors in the kitchen.


Previous Post

JDK 11.0.18, 17.0.6, 8u361, 19.0.2 and 7u371 Have Been Released!

Raymond Gallardo | 1 min read

Next Post


Announcing Java Card 3.2 Release

Nicolas Ponsini | 1 min read