Thursday, December 4, 2014

Using Puma, Rails 4 and JRuby on Heroku

The state of JRuby deployment has improved quite a bit since I wrote my book. But information on setting up a new environment still seems hard to come by. I've encountered a number of folks who have tried to set up a deployment environment using guides that are directed at MRI (C Ruby) users. Let's fix that, shall we...

In this post, I'll walk you through the basic steps for setting up Rails 4 to run with Puma under JRuby. Then you'll learn how to deploy that app on Heroku.

Creating a New App


First, let's create a brand new Rails 4 application. Make sure you have JRuby installed and are using it, like this:

Now install the Rails 4 Gem and create a new Rails application:

Because this application was created under JRuby, a nice set of defaults have been configured for you. Gems like "therubyrhino" and "activerecord-jdbc" are already in your application's Gemfile.

But the new application does not have a production ready web server. That's were Puma comes in.

Using and Configuring Puma


Move into the root directory for your new application and open the Gemfile. You'll probably see a commented out line for the "unicorn" gem. Delete it. JRuby and Unicorn are a bad combination (if they even work together at all).

Now add the following line to the Gemfile to make Puma a dependency:

Run bundle install at the command line to install the Gem.  Then configure Puma by creating a config/puma.rb file and putting the follow code in it:

This will set the port, environment, and thread pool size based on environment variables. The defaults are intended for development.

Because Puma is a multi-threaded server, and JRuby has real threads, you'll want to configure your database connection pool size appropriately. To do this, create a config/initializers/database_connection.rb file, and put the following code in it:

Setting your maximum pool size to the number of threads is a good default.

Now run the application with this command:

Your application is running, and you can see it by browsing to http://localhost:3000. You'll probably get an error if you haven't connected the application to a database. You can do that locally, or you can use a free one from Heroku.

Deploying to Heroku


Heroku is a Platform-as-a-Service that supports both Ruby and Java applications. Naturally, it also supports JRuby. To start using Heroku, follow this guide for installing the Heroku toolbelt and creating an account. This will provide you with a heroku command that you can use to create and deploy apps for free!

To start, make sure your project is under version control with Git by running these commands:

Now you need to tell Heroku how to run your app. Do this by placing the command you ran earlier in a Procfile in the root directory of your project. Create the Procfile and put this code in it:

You'll also need to configure the Ruby runtime in your Gemfile (in this case its JRuby) by adding this line:

Add the changes to Git by running these commands:

Next, create a Heroku app for the project and make your first deployment by running these command:

Heroku has detected that your application uses JRuby, it installed the JDK, ran bundler and launched your web process. If you were to open the app by running heroku open you'll probably get an error, because the default landing page is disabled in this environment. But you can create standard Rails templates by running rails generate controller welcome as you would with MRI.

There is a great deal more that Heroku can do for you. You can provision a database, a queuing system, a scheduler, monitoring tools, or even telephony services. And of course, you can begin to leverage the power of the JVM for your Ruby app.

You can download the complete source code for the application presented in this article from the heroku-jruby-rails-4 repo on Github.

50 comments:

  1. Thanks for the post. I think you forgot to include the code snippet after this line: "You'll also need to configure the Ruby runtime in your Gemfile (in this case its JRuby) by adding this line "

    ReplyDelete
  2. A crucial piece missing from your recipe is configuring the size of the database connection pool to match the number of threads. This article describes that: https://devcenter.heroku.com/articles/concurrency-and-database-connections

    ReplyDelete
    Replies
    1. Good call, that is important. I'll add a note. Thanks again.

      Delete
  3. Great article, thank you. What about JRuby's performance in comparison to MRI(2.1.5) ?
    I tried JRuby in production for a server with about 100 req/seq.(moved from MRI) I didn't notice any speed up.
    Is there any point to move to it at all?

    ReplyDelete
    Replies
    1. I think it really depends on your application, and what it is doing during each request cycle. At a lower load, such as you tried, there may not be a big difference. But I suspect most apps will see improvements as they start to scale up. The JVM has better memory characteristics and better GC. And because Puma can use threads instead of separate processes to achieve true concurrency, the threads can actually share memory instead of the processes replicating your app (and running multiple garbage collectors).

      Delete
  4. I'd like to know how can we perform a graceful restart while deploying? Since it's clearly written in the official readme file (https://github.com/puma/puma#platform-constraints), I'm having second thoughts on choosing Puma server for Jruby.

    ReplyDelete
  5. Thanks for the post,Really you given a valuable information.worth to read this type of articles .
    Thank you.
    oracle R12 training

    ReplyDelete
  6. Thanks you!! very useful for me to study and very simple and easy to undersatnd...
    WebDschool
    UI UX Design Courses in Chennai

    ReplyDelete
  7. Thanks you!! very useful for me to study and very simple and easy to undersatnd...
    digital marketing courses in delhi

    ReplyDelete
  8. This is an incredibly detailed guide on deploying Rails 4 with Puma and JRuby on Heroku—super helpful for developers exploring JRuby! 🚀 The insights on configuring Puma for JRuby’s real threading capabilities and tweaking the database connection pool are spot on. I particularly liked the step-by-step breakdown for Heroku deployment and configuring the Procfile.

    For those diving into modern tech stacks like this, it might also be a great time to explore data Science courses in Delhi . Learning data science can complement web development skills by enabling developers to integrate analytics, machine learning, and data-driven decision-making into their applications.

    ReplyDelete
  9. This is a fantastic guide for deploying Rails 4 with Puma on JRuby to Heroku! The step-by-step breakdown makes it easier for developers to navigate the setup process, especially those transitioning from MRI-based guides. The emphasis on configuring Puma correctly and managing database connections for JRuby’s threading capabilities is particularly useful.

    On a different note, if you're looking to explore opportunities beyond development, consider checking out Medical Coding Courses in Delhi. These courses offer valuable skills in the growing healthcare industry.

    Great article—looking forward to more insights on JRuby deployments! 🚀

    ReplyDelete
  10. Great guide on setting up Rails 4 with Puma and JRuby on Heroku! 🚀 The step-by-step breakdown makes it easy to follow, especially for those transitioning from MRI to JRuby. The inclusion of database connection pooling optimizations is a nice touch for handling concurrency effectively.

    For anyone interested in Medical Coding Courses in Delhi, there are excellent opportunities to build a career in healthcare data management while mastering coding skills in a high-demand field.

    Thanks for sharing this—Heroku deployment just got a lot smoother! 💡🔥

    ReplyDelete
  11. This guide is super helpful! Deploying Rails 4 apps with Puma on Heroku can be tricky, but your step-by-step breakdown makes it much easier to follow.

    Medical Coding Courses in Delhi

    ReplyDelete
  12. Thanks for sharing! Deploying Rails 4 with Puma under JRuby on Heroku simplifies hosting Ruby applications. a practical guide for modern app deployment!
    Medical Coding Courses in Chennai

    ReplyDelete
  13. Thanks for sharing! This guide provides a practical. It simplifies the process for developers transitioning from MRI-based setups, offering clear steps for creating a new app, configuring Puma, and managing deployment.

    Medical Coding Courses in Chennai

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This is exactly the kind of guide I wish existed when I first tried deploying JRuby apps to Heroku—super clear and thorough.Medical Coding Courses in Delhi

    ReplyDelete
  16. "I landed my first freelance project within a week of completing the IIM SKILLS Content Writing course. The training was top-notch!"


    https://iimskills.com/data-science-courses-in-india/

    ReplyDelete
  17. Deploy Rails 4 with Puma on JRuby for efficient multithreading, leveraging real threads and optimized database pooling. Deploy seamlessly to Heroku for hosting.


    Data Science Courses in India

    ReplyDelete
  18. I was able to upskill while working full-time. Super flexible!

    ReplyDelete
  19. Thanks for sharing! Deploying Rails 4 with Puma under JRuby on Heroku simplifies hosting Ruby applications, leveraging real threads and optimized database pooling for efficient performance. (https://deployingjruby.blogspot.com/2014/12/using-puma-with-rails-4-on-heroku.html).

    ReplyDelete
  20. The course helped me understand data science fundamentals and advanced techniques. The live projects were a highlight.

    ReplyDelete
  21. I loved how the course gradually moved from basic concepts to more advanced topics. It gave me a strong foundation.

    ReplyDelete
  22. Tools like Tableau were taught in detail.
    Data Science Courses in India

    ReplyDelete
  23. This article was both insightful and easy to follow—exactly what a good blog should be. You explain things in a way that makes learning enjoyable. If you're aiming for a stable healthcare career, do check out the Medical Coding Course offered by IIM SKILLS in Varanasi.
    Medical Coding Courses in Varanasi

    ReplyDelete

  24. This was a well-written and informative post that kept me engaged throughout. You have a talent for simplifying complex subjects. For those interested in healthcare careers, the Medical Coding Course by IIM SKILLS in Varanasi offers excellent training and job readiness.
    Medical Coding Courses in Varanasi

    ReplyDelete
  25. "The feedback sessions at IIM SKILLS were instrumental in my learning journey."

    ReplyDelete
  26. A mobile app for LMS would be a great future upgrade.

    ReplyDelete
  27. This article was very helpful and delivered complex information in a simple way. Such posts make learning new concepts more approachable.
    For those seeking career stability, Medical Coding is worth exploring. Consider enrolling in Medical Coding Courses in Delhi for expert-led training.
    Medical Coding Courses in Delhi

    ReplyDelete
  28. Thanks for this detailed walkthrough on deploying Rails 4 with Puma and JRuby on Heroku! The step-by-step instructions for configuring Puma and managing the database pool really clarified things for me. Great resource for anyone looking to leverage JRuby’s threading with Rails on a scalable platform.

    Medical Coding Courses in Kochi

    ReplyDelete
  29. This is a solid and informative post—thanks for walking through the setup of Puma with Rails 4 and JRuby on Heroku. It’s always refreshing to see content that addresses real-world deployment challenges with JRuby, especially considering it’s a less common stack compared to the typical MRI-based Rails apps.

    Puma is an excellent choice for JRuby, given its ability to handle concurrent threads efficiently, which aligns well with JRuby’s true multithreading capabilities. When paired with the scalability and ease of deployment Heroku provides, this combination becomes quite powerful for applications expecting a significant number of concurrent users.

    I appreciate how you broke down the configuration steps clearly—especially the details about setting the proper thread counts and using the Procfile. Many developers overlook the importance of tuning thread and worker counts based on the expected load, and your explanation highlights why it matters.

    Also, pointing out potential issues, like slower cold starts or memory tuning with JRuby, adds real value for those considering this stack. It’s not just about making it work—it’s about making it perform well.
    Medical Coding Courses in Coimbatore

    ReplyDelete
  30. Using Puma with Rails 4 and JRuby on Heroku is a smart configuration that offers high concurrency and performance benefits. Puma, being a multithreaded web server, works exceptionally well with JRuby because JRuby allows true multithreading—unlike MRI Ruby, which is constrained by the Global Interpreter Lock (GIL). Deploying this setup on Heroku further enhances its scalability due to Heroku's support for concurrent web servers and JRuby runtime.

    One challenge developers may encounter is memory usage, as JRuby tends to be more memory-intensive. Optimizing thread counts in the puma.rb config file and properly tuning JVM settings are essential for maintaining a healthy performance-to-cost ratio on Heroku dynos. Additionally, precompiling assets and ensuring all gems are JRuby-compatible are crucial steps for smooth deployment.

    Overall, this stack is ideal for applications that need to handle high traffic or parallel background processing. It combines modern server architecture with the power of the JVM, making it a reliable choice for production.

    Medical Coding Courses in Coimbatore<

    ReplyDelete
  31. This post is an excellent guide for deploying Rails 4 with Puma on Heroku. Your step-by-step instructions are clear and easy to follow, making the deployment process much less intimidating. I especially appreciate the troubleshooting tips and practical advice you’ve included. It’s a great resource for developers looking to optimize performance and stability on Heroku. Thanks for sharing your experience and making deployment smoother for others!
    Medical Coding Courses in Mumbai





    ReplyDelete
  32. Thanks for this detailed walkthrough on deploying Rails 4 with Puma and Juby on Heroku! The step-by-step instructions for configuring Puma and managing the database pool really clarified things for me. Great resource for anyone looking to leverage Juby's threading with Rails on a scalable platform.
    Medical Coding Courses in Mumbai

    ReplyDelete
  33. I really appreciated how you walked through each step—from adding the gem to configuring the Profile. The explanation of threading vs worker processes was especially helpful for understanding performance implications. This guide made deployment feel much more approachable.
    Medical Coding Courses in Mumbai

    ReplyDelete
  34. This guide on using Puma with Rails 4 on Heroku provides clear, step-by-step instructions—perfect for developers aiming to optimize app deployment and performance! Medical Coding Courses in Norway

    ReplyDelete
  35. Thanks for this clear and detailed walkthrough! Setting up JRuby with Puma on Heroku can be tricky, especially with so many MRI-focused guides out there. Your step-by-step approach really helps demystify the process. Medical Coding Courses in Mumbai

    ReplyDelete
  36. Fantastic guide—thank you for putting this together! It’s often tough to find JRuby-specific deployment steps that are this clear and up-to-date, especially when most tutorials default to MRI. Medical Coding Courses in Mumbai

    ReplyDelete
  37. Great deployment guide! Smooth integration with Puma and Heroku.
    Medical Coding Courses in Mumbai

    ReplyDelete
  38. Exactly what I needed—great deployment notes for Heroku.
    Medical Coding Courses in Mumbai


    ReplyDelete
  39. Fantastic Guide, cleared all the doubts of setting up Rails 4 to run with Puma under JRuby.
    Medical Coding Courses in Mumbai

    ReplyDelete