Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Correct way of programming a CGI script
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-08 (16:04)
From: Gerd Stolpmann <info@g...>
Subject: Re: [Caml-list] Correct way of programming a CGI script
Am Montag, den 08.10.2007, 17:08 +0200 schrieb Tom:
> Hi! I am in a process of making a website (which might receive
> substantial amounts of traffic), and am considering options for the
> backend. I discarded PHP and other similar server-side scripting
> languages, due to performance problems (I suspect that PHP and similar
> could not scale well, unless I implemented complex caching
> techniques). I plan to use OCaml to generate static .html documents
> from the content from the database. Since the content will probably
> change not as often as it will be accessed, I believe this is the
> better way (as opposed to accessing the database every time a user
> wants to load the page). 
> So, OCaml programs will only be run seldomly to access the database
> and generate HTML files, using the data fetched from the DB. However,
> I am still worried whether this would cause too much performance
> impact. 
> I heard that OCaml is particularly slow (and probably
> memory-inefficient) when it comes to string manipulation. What is the
> preferred way in handling strings (building long strings from short
> parts - something StringBuilder would be used in Java)? Does anybody
> have any experience concerning this kind of applications? 

No, this is nonsense. Of course, you can slow everything down by using
strings in an inappropriate way, like

let rec concat_list l =
  match l with
    [] -> ""
  | s :: l' -> s ^ concat_list l'

Use the Buffer module instead:

let concat_list l =
  let b = Buffer.create 243 in
  let rec concat l =
    [] -> ()
  | s :: l' -> 
     Buffer.add_string b s;
     concat l' in
  concat l;
  Buffer.contents b

> What about the startup time and memory usage of the program? Could
> these affect the stability and efficiency of the web server?
> (Hope someone will be able to decipher my language and care to
> answer :P )

Have a look at ocamlnet ( It has plenty of ways of
building web apps. For example, you can easily run your own HTTP server
in a multi-processing or multi-threading setup. Or you can connect your
web app with Apache by using fastcgi or a few other available protocols.
All this is pretty much scalable.

There is no support for generating HTML, however.

An example for a stand-alone webserver (it is accompanied only by a
config file):

Here is the same for the "connect to Apache" approach:

In either way, it is possible to keep the connection to the db in case
you need it for generating the page.

Hope this helps,

Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany
Phone: +49-6151-153855                  Fax: +49-6151-997714