[erlang-questions] "Erlang plus BDB: Disrupting the Conventional Web Wisdom"

Yariv Sadan <>
Sat Oct 13 07:59:21 CEST 2007


On 10/12/07, YC <> wrote:
>
> On 10/12/07, Yariv Sadan <> wrote:
> > > We can use both Postgres and MySQL from Erlang. With ErlyDB, using
> > > those DBMS's is just as Erlangy as Mnesia for most CRUD and SELECT
> > > operations -- you don't have to write SQL by hand. Many big sites run
> > > on MySQL (YouTube, Flickr, Facebook...), so it definitely scales.
>
> Hi Yariv -
>
> A few questions for ya :)
>
> Is the postgresql driver on par with the mysql driver at this time?  Reading
> from your blog it seems Postgresql is not yet there.

AFAIK, the Postgres driver works. I haven't tested it myself, though.
Other developers have contributed that code -- I just did the MySQL
driver. There's a minor bug in the driver in the current version of
ErlyWeb that will be fixed in the next release.

>
> Would ErlyDB be the DBI/DBD equivalent of the Erlang world?  The nice thing
> about open source is that everyone can do what they want, but having a de
> facto package for something like this is probably essential.

ErlyDB is like ActiveRecord for Rails. It automatically generates
database abstraction code for you from the database metadata. So, if
you create a simple module, e.g. cat.erl:

-module(cat).

and then call erlydb:code_gen() on it, ErlyDB would generate a bunch
of functions that make it easy to work with persistent cats. For
example, you could write the following code

Cat = cat:new_with([{name, "Felix"}]),
Cat1 = cat:save(Cat),          %% executes 'INSERT INTO cat(name)
VALUES ("Felix"); '
"Felix" = cat:name(Cat1),
Cat2 = cat:name(Cat1, "Darwin")
cat:save(Cat2),       %% executes 'UPDATE cat SET name="Darwin" WHERE id=1'
[_] = cat:find({name,'=',"Darwin"}),      % executes 'SELECT FROM cat
WHERE name="Darwin"
cat:delete(Cat2)      % executes 'DELETE FROM cat WHERE id=1

You can also do more advanced things, like one-to-many and
many-to-many relations. (In the Vimagi codebase, you won't find a
single line of SQL :) )

I can't say if it's a de-facto package -- that depends on how many
people want to adopt it.

>
> > > In Vimagi, I use MySQL for persistent data and Mnesia for live session
> > > data. It works great, even on a RAM-constrained VPS.
>
> Is the live session data a cached data or actually the session table? I
> think Mnesia would be great as a cache server (e.g. memcached), but haven't
> thought through it, and if you have done so, would love to hear your take.

I think one of the best things about building webapps with Erlang is
Mnesia. You can keep your session state in Mnesia without having to
put it in your database (slow), on disk (also slow) or in memcached
(requires more moving parts).  I currently use Mnesia just for session
state -- I don't do caching in Vimagi yet. I'm sure that implementing
caching with Mnesia would be just as easy -- I just haven't tried it.
I don't know enough about memcached to say which would work best under
very high loads, but Ericsson's experience with Mnesia indicates that
Mnesia can support more than AJAXy blog engines :)

Mnesia is shared memory done right. It's easy to work with, it can
store any Erlang data, it's transactional, and due to Erlang's hot
code swapping you don't lose your in-RAM data when you do code
upgrades. Without hot code swapping, keeping seesion state purely in
RAM would be a bad choice because you'd lose it when having to restart
the server to do code upgrades. (To do code upgrades in Vimagi, I ssh
into the box and execute "svn up" :) ).

But don't use Mnesia for high-volume persistent storage -- use MySQL,
Postgres or BDB (or create your own driver for Oracle or whatnot).

Cheers,
Yariv



More information about the erlang-questions mailing list