Distributed Ruby: Mark Bates Sheds New Light
If you're reading this, you may already be sold on the idea that Ruby is a fun and productive language. Certainly, I am. As someone with ten years of Ruby experience (and two editions of The Ruby Way) behind me, you must forgive me if I'm not objective.
Besides the tendency toward more agile languages, the industry is also seeing distributed programming techniques grow in popularity. Distributed programming in Ruby has been possible for years. But certain areas were not well-understood by many people, since the documentation has not been wholly adequate (unless you speak Japanese).
Mark Bates recently helped fill that gap. His new book, Distributed Programming with Ruby, addresses libraries, tools, and techniques that facilitate distributed coding in Ruby.
Hal Fulton: What was your motivation in writing this book, other than the minimum-wage pay?
Mark Bates: A colleague approached me at the Ruby Conference last year (in November 2008), after I spoke about building distributed applications. He suggested I should write a book on the subject, because no comparable book existed. It seemed like a good idea, especially since the documentation around DRb/Rinda is almost nonexistent. I thought it would be a real benefit to the community to explain what those libraries are, how they work, and what you can and can't do with them. From that impetus, the book just grew.
Hal Fulton: Do you write distributed applications often?
Mark Bates: In a way, I think we all write distributed apps more than we think we do; we just usually don't think about it.
The company I was working for a year ago was heading down a real distributed app path, which is how I got so deeply into this area. Over the past year I've been working at a company where we have a few applications that aren't traditionally "distributed" applications, but there is still a high amount of queuing and communication that needs to happen between the apps. So in some way, I work on distributed apps every day.
Hal Fulton: What problem domains have you specifically coded with these techniques?
Mark Bates: Throughout the book, I try to relate the technologies and techniques to problems that I or others have encountered. I present the problems (and the solutions to those problems) in a way that makes sense to the reader, so that they can find value and relevance to their own projects. Personally, I've had to deal with communication between different apps, remote method invocation, offloading heavy tasks, messaging – really, the whole range of issues that accompany any decent-sized application.
A lot of those experiences are spelled out in the book to help keep people from falling into the same traps that caught me along the way.
Hal Fulton: Is it your perception that there are significant gaps in distributed Ruby as it exists now? Tools, libraries, or features that are missing?
Mark Bates: I don't think there is any one perfect solution out there for Ruby, or for any other language for that matter. This is a hard question to answer; what is the real definition of "distributed Ruby" anyhow? In the book, as I talk about different technologies, and the solutions they present, I try to point out what's missing and how things could be improved.
I think the DRb/Rinda packages offer some great tools. But they can be slow at times, and they could be made easier to use.
Hal Fulton: You said, "I think we all write distributed apps more than we think we do, we just usually don't know it." That's intriguing to me. Can you give an example or two of applications whose distributed nature might be less than obvious?
Mark Bates: Any app that has ever offloaded some heavy lifting to a background process has dabbled in distributed programming. If you use a service like Amazon's new RDS, Hadoop, or any other remote service you are writing distributed applications.
I think people get it in their heads that distributed programming means just one or two different things, some sort of remote method invocation or having a bunch of computers all chewing on some complex data set or problem. But really it's a lot broader a field than that. I tried to demonstrate that in the book by giving examples of some of the different types of distributed programming there are.
Hal Fulton: Many people associate distributed programming with map-reduce type problems of the SETI@Home variety. Do you find existing Ruby tools adequate for this type of task (barring, of course, the fact that Ruby tends to be slower than C)?
Mark Bates: There's no denying that if you're talking about raw number crunching speed, a C app will certainly be a lot faster than Ruby. With that said, I don't think most people are writing those kinds of programs. For most of us, it's offloading a heavy task, sending messages, or communicating with a service somewhere that is doing some heavy lifting or is "isolating" code for us. In those circumstances, Ruby is definitely fast enough.
There are a few map-reduce libraries available for Ruby; I talk about them in the book, but they haven't really caught on. I don't know if that is because there aren't enough people in the community writing applications that require map-reduce, or that the libraries are too slow.
During the course of writing this book, I looked at a few of those libraries, and unfortunately I was not overly impressed by them. I'm hoping that if this book goes to a second edition, there will be a really great Ruby map-reduce library out there for me to include. Developers, start your engines!
Hal Fulton: Some applications are "inherently" distributed by their nature (such as a multi-person chat client). Do you see these as a special subset of distributed apps, or are they just "more of the same"?
Mark Bates: I wouldn't say they are a special subset of distributed apps. I think they are a flavor of distributed applications, of which there are many flavors.
Hal Fulton: More generally: Are there distinct classes of apps that are more or less challenging?
Mark Bates: Dealing with background jobs can be fairly straightforward, and certainly libraries like Delayed::Job help make it easier. The same goes for message queuing. Even so, it's easy to get yourself stuck in a tricky situation with those.
On the more complex side of things, I think any sort of remote method invocation can easily lead you down a rabbit hole. When you make RMI calls you are tying your application to the internal design of another application. With a REST API you know that you will deal with a very clear API that shouldn't be changing too much, and that will publish basic data types. With RMI, it's possible that the developer of the other app could easily change a method definition, or change the return data type, or even make a simple change within the method itself which can easily break your application.
And security becomes an even bigger concern as you can now execute code remotely on another server. Scary stuff.
Hal Fulton: Many existing tools and libraries offer similar functionality in different ways, and therefore overlap here and there. Have you discovered particular tools that conflict with (or better, complement) each other?
Mark Bates: I believe that you should try to keep your environment as clean as possible. If you don't like the message queue you are currently using, don't bring in a second one and use them both. Instead, refactor your code to use the new one. Otherwise it's confusing to code, difficult to deploy, and definitely error-prone. That's the biggest conflict I think you'd find with most of these libraries.
As for complementary libraries, I really enjoy using RabbitMQ/AMQP and Delayed::Job together. I find that these complement each other incredibly well. For example, I had several business groups each asking for various "slices" of data from requests to the app. One group wanted the data presented one way, another wanted it another way, and so on. So I built a simple Rack app that took the request info (headers, params, cookies, etc.) and sent it to RabbitMQ. Then we wrote some processes that would subscribe to the queue and generate Delayed::Jobs for each type of reporting we needed. We got the speed of RabbitMQ and the power of subscribing to queues. And, in addition, we got the security of the Delayed::Job database-backed storage and error handling. It's worked out really well for us, and it's certainly a pattern I'll use again.
Hal Fulton: While you wrote this book, did particular libraries emerge as your favorite pieces of technology?
Mark Bates: If I had to pick one, it would have to be Delayed::Job. It's incredibly easy to set up and use. You can easily prioritize tasks and monitor them, and great error handling is built in. I use it all the time. It's an excellent way to deal with those heavy background tasks.
I did have to extend it for my own purposes. I wanted things like Hoptoad Notification and an easy way to re-enqueue a task, so I built the delayed_job_extras gem, but apart from those things, it's pretty awesome right out of the box.
Hal Fulton: In writing this book, what insights or important truths did you learn?
Mark Bates: I learned that DRb/Rinda needs better documentation in the core!
While it's not necessarily something I learned while writing this book, my advice would be: Don't get caught up with the "new, hot, sexy" library. It's easy to read a book like this one and decide you have to implement RabbitMQ because it's so cool, even if you don't have an actual business need for it. Before you go and spend crazy amounts of time implementing any technology into your project, first ask yourself: Is this the right technology for the problem at hand, or am I you just trying to add it to my resume?
In either case, when you introduce a new tool, I recommend playing with it a little bit on a small scale. Open up an empty editor and try playing with the technology on its own for a few hours. Dig into the source code. Read the documentation. Try out a few of the features. You'll quickly learn whether the technology is right for you and the project at hand. It's quicker and easier than trying to shoehorn it into some big existing project.