0

I am creating a Ruby on Rails application that will use quite some complex and time consuming alghoritms.

I was thinking of implementing them with a C or a C++ function.

I would reapply appreciate if anyone who has experience with implementing complex alghoritms into web applications could give me some pointers on what to do.

My hypothesis is that Rails by itself is too slow and it is better to use C or C++. If that is the case, how can you implement the alghoritm in C or C++ and call it with Ruby on Rails?

  • 1
    http://guides.rubygems.org/gems-with-extensions/, http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html, https://people.apache.org/~rooneg/talks/ruby-extensions/ruby-extensions.html – Marek Lipka Jul 30 '14 at 14:38
  • 2
    First implement in Ruby. If it's too slow, then look into it. – Neil Kirk Jul 30 '14 at 14:40
  • What do you mean with *time consuming*? Will it take a second, ten seconds of hours to process? You don't want the user of OLTP Rails application to wait for the results of such process online. So you should run such processes as a background batch jobs. So why not to just create a background C/C++ daemon and communicate with is from Rails using some messaging queue system? – Grych Jul 30 '14 at 14:51

1 Answers1

0

ok this is a very vague question but makes for a fun conversation.

Generally the problem you are describing is not really a technical problem, but a business problem. Some of the huge advantages of rails is speed for development, maintenance, etc. C and C++ are great tools as well, but it tends to take longer to build the same thing in C as it does in rails. This means it is more expensive.

This cost is ongoing. With rails you will lose some performance which will require more hardware. This cost is ongoing.

Lets assume the complex algorithm is a long running function that can be defined as the following happening within a User class.

def DoIt() sleep 10000 end

Now lets also assume that you need to have this happen for a lot of items at a time. Say 10000 users every minute.

If we were to do this in a straight linear process we would be looking at a run time of 10000*10000 every minute. That is no bueno.

Luckily these problems are solved all of the time. What we are discussing becomes Vertical vs Horizontal scaling.

Vertical: Lets assume that if you write it in c the function becomes the equivalent of:

def DoIt sleep 5000 end

You have gained 50% and that is great. The problem is that if your user load grows you end up in trouble. We see this in production systems all the time.

So now the question is how can you horizontally scale this.

Using resque (or any of 100s of solutions for this) you can kick this work to background and distribute it. You can then add servers to handle the load that comes into the system.

Now the question is, is it worth the cost of building it in c/c++ or are you better building it in rails and paying for the extra hardware? Or is it better to start with rails, and if needed move to c++/C when you have your prototype finished and validate that it is needed.

vereecjw
  • 139
  • 3
  • That is a really good point! I like the way you look at it. Scaling horizontally sounds like a good plan for the beginning! – user3852820 Jul 30 '14 at 16:06