[ index | links | mail ]

Scaling single-script CGI applications

Problems in scaling single-script CGI applications

I guess every Perl programmer has written a medium CGI application at least once. I did it several times, each time discovering something new to make my application more scalable.

Now I think that a single script with run-modes provides the best framework to develop CGI.
My code usually looks like this:

my %dispatch_table = (
 list => \&list,
 search_form => \&search_form,
 help => \&help,

 # And so on...
);

my $mode = $query->param( 'mode' );

print $query->header, $query->start_html();
&{$dispatch_table{$mode}};
print $query->end_html;

Then I write a procedure for every run-mode I need. Using Template module I need also a template file for every type of page that my CGI application will generate (that often corresponds to a single run mode) so that I could add functionality to my application just adding a new term in my %dispatch_table and writing a proper new procedure. I fell very comfortable with this developing process. But I'm sure I didn't say anything new for most part of you.

Unfortunately designing a CGI application often drives to more complicated situations, where a run-mode implies another run-mode and so on, asking the developer to pass parameter through the application, just because a run-mode has to pass them to the run-mode that will actually need them. I'll explain this situation with an example.

Let's suppose we are developing a web-based forum. So there will be a list run-mode that displays every message in the forum, allowing the user to reply to a particular message. Also, there will be a form run-mode that will provide... a form to let the user insert the reply. In this form there will be a submit button that will invoke insert_message run-mode. This mode contains DBI stuff to actually insert the new message in the forum, reporting success or error to the user.

Here a schematization:

list mode -> form mode -> insert_message mode
We are passing info about the message we're replying to. form mode does not need this infos, but we need to pass them to insert_message mode. Eventually we pass informations to insert-message mode, that will use them, e.g., to store the message in the same thread of its parent.

What does it means? form run-mode fetches proper parameter, will pass it to the template that will probably add a hidden field to pass this parameter to insert_message mode. I think this approach is too complicated and makes the application difficult to scale. The linear process of adding new procedures and entries in the %dispatch_table breaks when the functionality provided by the application is formed by several dipendent screens.

What can be done to cope with such situations? I thought to a couple of solutions, but I'm not sure of their effectiveness.

  • Treating complex functionalities in complex procedures, using another dispatch table. In the example I was talking about, we could unify form and insert_message run-modes in a single reply run-mode, that will have its own dispatch table with form and insert_message modes. I don't like this approach too much because it takes off the possibility to use a procedure in multiple situations (e.g.: we could use form and insert_message modes to insert in the forum messages that are not replies.
  • CPAN :) There are CGI::Application (that, as far as I know, does not introduce new functionality to the tecniques I've described above), CGI::Screen and CGI::FormMagick. I haven't tried the last two.

Last modified: 09:37:37 25-Dec-2001 / Maintained by larsen