Skip to main content

Mojolicious and MojoX::Redis

I've been looking at the code for MojoX::Redis for a couple of days now and I'm impressed and depressed at the same time.

First the good news. Like many projects it's open source. The better news is that it looks cool. The code is nicely formatted and if anyone was following the PEP equivalent for perl then you'd say it was adhered to.

On the sad news side of things. While there is some POD doc at the end of the main project file that's it. The code is not documented at all. And the worst of it is that the code is the exact reason why people hate this crap. This person clearly knows the ins and outs or perl and he demonstrated that aptitude well. But if you asked me to reverse engineer it... it's going to take a while and a few cases of wine or beer.

The best feature is that it implements non-blocking in a way that complements Mojolicious, however, the first side effect is that the main thread continues to run while the first request is processing. When really the benefit of this sort of functionality is to let peer events run not the current main thread. Since it was not documented in any meaningful way this had to be experienced first hand... and after reviewing the test code I'm not sure that my conclusions from my code are correct.

Anyway, here is an explanation as I see it in pseudo code.
1) do some redis function like INCR expecting a response
2) do a get on the same key
3) compare the results and they will always fail because
the results from #1 have not completed by the time #2 completes.

Some code that demonstrates this
my $retval1 = undef;
$redis->execute("incr" => [$mykey] => sub{my ($redis,$res)=@_; $retval1=$res;});
my $retval2 = undef;
$redis->execute("get" => [$mykey] => sub{my ($redis,$res)=@_; $retval2=$res;});
die "they do not match" if $retval1 != $retval2;

The side effect here is that it simply does not work. The only way to make this work is something like this:
my $retval1 = undef;
my $retval2 = undef;
$redis->execute("incr" => [$mykey]
=> sub{
my ($redis,$res)=@_;
$redis->execute("get" => [$mykey]
=> sub{my ($redis,$res)=@_; $retval2=$res;});
die "they do not match" if $retval1 != $retval2;

The effect in the above code is that since the sub() that is called upon completion of the incr() is called when the incr() is completed. The same for the subsequent call to the get(). The last die() will still have the same effect of getting control before the redis calls have completed execution. So fo for this to be effective the die() needs to be inside the sub() of the get(). Phew!

I looked at the test cases in MojoX::Redis and there were some interesting examples. There was an implementation of the redis pipeline in the form of a multi() transaction. This could be interesting since one could do an incr() and a get() in the same pipeline, however, if you needed the result in order to perform future calls then you'd have the same timing problems with the response not being ready or available in local memory for future calls.

An async lib of the redis tools seems novel but it makes certain use-cases very difficult and verbose. For example I was playing with the sinatra example of RestMQ. Sinatra being ruby has many of the same warts, and certainly the ruby version of the lib was not evented like MojoX::Redis so I do not expect that it's going to get much work done. (I really like their demo version because it is so little code and it so accurately depicts the mission that it's hard not to like the elegance. Even though it's ruby.) But the reality is that it is still constrained.

In summary, while Mojolicious is nice and simple to use(I still like it). The simple use-cases are simple but as soon as you advance to the next step this will get tricky. If you track after the "get it to work correctly" and then think about performance you could end up rewriting the project to make it performant. So be mindful.
post '/q/:queue' => sub {
my $self = shift;
my $result = undef;
my $queue = $self->param('queue');
my $value = $self->param('value');
if (! defined $queue) {
$self->app->log->debug('queue was not in the URL ('.$queue.')');
} else {
my $uuid = undef;
my $lkey = undef;
my $q1 = $queue . $QUEUE_SUFFIX;
my $q2 = $queue . $UUID_SUFFIX;
$redis->execute("incr" => [$q2] => sub{
my ($redis, $res) = @_;
$uuid = $res->[0];
$self->app->log->debug('the uuid is ('.($uuid||'undefined').')');
$lkey = $queue . ':' . $uuid;
$redis->execute("sadd" => [$QUEUESET, $q1]);
$redis->execute("set" => [$lkey, $value]);
$redis->execute("lpush" => [$q1, $lkey]);
$self->app->log->debug('the uuid q is ('.($q2||'undefined').')');
$self->render(text => '{ok, ' . $lkey . '}');

When this code executes... the following output is on the console:
rbucker@mvgw:~/hg/metaventures/gwtwo$ ./alt/  daemon
[Sun Oct 30 00:59:50 2011] [info] Server listening (http://*:3000)
Server available at
[Sun Oct 30 00:59:51 2011] [debug] Your secret passphrase needs to be changed!!!
[Sun Oct 30 00:59:51 2011] [debug] POST /q/myqueue/ (Wget/1.12 (linux-gnu)).
[Sun Oct 30 00:59:51 2011] [debug] Dispatching callback.
Use of uninitialized value $uuid in concatenation (.) or string at ./alt/ line 47.
[Sun Oct 30 00:59:51 2011] [debug] the uuid q is (myqueue:UUID)
[Sun Oct 30 00:59:51 2011] [debug] 200 OK (0.003678s, 271.887/s).
[Sun Oct 30 00:59:51 2011] [debug] the uuid is (19)

My observation is that the output from the sub() is in the log after the rest of the output. Also the error complaining about line 47 is because $uuid is currently undefined when the line executes. Therefore the callback is not merging the execution and therefore any sensible use requires that the code be nested. And that sucks.


Popular posts from this blog

Entry level cost for CoreOS+Tectonic

CoreOS and Tectonic start their pricing at 10 servers. Managed CoreOS starts at $1000 per month for those first 10 servers and Tectonic is $5000 for the same 10 servers. Annualized that is $85K or at least one employee depending on your market. As a single employee company I'd rather hire the employee. Specially since I only have 3 servers.

The pricing is biased toward the largest servers with the largest capacities; my dual core 32GB i5 IntelNuc can never be mistaken for a 96-CPU dual or quad core DELL

If CoreOS does not figure out a different barrier of entry they are going to follow the Borland path to obscurity.

UPDATE 2017-10-30: With gratitude the CoreOS team has provided updated information on their pricing, however, I stand by my conclusion that the effective cost is lower when you deploy monster machines. The cost per node of my 1 CPU Intel NUC is the same as a 96 CPU server when you get beyond 10 nodes. I'll also reiterate that while my pricing notes are not currently…

eGalax touch on default Ubuntu 14.04.2 LTS

I have not had success with the touch drivers as yet.  The touch works and evtest also seems to report events, however, I have noticed that the button click is not working and no matter what I do xinput refuses to configure the buttons correctly.  When I downgraded to ubuntu 10.04 LTS everything sort of worked... there must have been something in the kermel as 10.04 was in the 2.6 kernel and 4.04 is in the 3.x branch.

One thing ... all of the documentation pointed to the wrong website or one in Taiwanese. I was finally able to locate the drivers again: (it would have been nice if they provided the install instructions in text rather than PDF)
Please open the document "EETI_eGTouch_Programming_Guide" under the Guide directory, and follow the Guidline to install driver.
download the appropriate versionunzip the fileread the programming manual And from that I'm distilling to the following: execute the answer all of the questio…

Prometheus vs Bosun

In conclusion... while Bosun(B) is still not the ideal monitoring system neither is Prometheus(P).


I am running Bosun in a Docker container hosted on CoreOS. Fleet service/unit files keep it running. However in once case I have experienced at least one severe crash as a result of a disk full condition. That it is implemented as part golang, java and python is an annoyance. The MIT license is about the only good thing.

I am trying to integrate Prometheus into my pipeline but losing steam fast. The Prometheus design seems to desire that you integrate your own cache inside your application and then allow the server to scrape the data, however, if the interval between scrapes is shorter than the longest transient session of your application then you need a gateway. A place to shuttle your data that will be a little more persistent.

(1) storing the data in my application might get me started more quickly
(2) getting the server to pull the data might be more secure
(3) using a push g…