Redis & Perl

1,061 views
Skip to first unread message

Martin R

unread,
Oct 1, 2013, 3:16:59 PM10/1/13
to redi...@googlegroups.com
Hello, I use redis with perl module Redis::Hash  more here -> http://search.cpan.org/dist/Redis/lib/Redis/Hash.pm


My question is how using this utility , delete, update etc,  values . This is not mentioned in module doc.

The problem is, when I add value to key i.e my $value = $my_hash{key} it works but problem occur when I try to delete this value i.e delete $my_hash{key} unfortunately still returns assigned value ;/


What is wrong or how to deal with this ?


Regards.

Damien Krotkine

unread,
Oct 1, 2013, 5:22:33 PM10/1/13
to redi...@googlegroups.com
Hi,

Why don't you just use Redis, the Perl module ? It is of very good quality and performance. 

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/groups/opt_out.

Mark Martinec

unread,
Oct 1, 2013, 8:25:40 PM10/1/13
to redi...@googlegroups.com
Martin,

> Hello, I use redis with perl module Redis::Hash more here ->
> http://search.cpan.org/dist/Redis/lib/Redis/Hash.pm

> My question is how using this utility , delete, update etc, values . This
> is not mentioned in module doc.

See its source, it's 66 lines altogether (excluding the POD documentation).
Implements FETCH, STORE, EXISTS, DELETE, CLEAR and hash traversal
(each/keys/values).

> The problem is, when I add value to key i.e my $value = $my_hash{key} it
> works but problem occur when I try to delete this value i.e delete
> $my_hash{key} unfortunately still returns assigned value ;/
> What is wrong or how to deal with this ?

The perl's delete() is supposed to return the old value before deletion.
The Redis::Hash doesn't obey this, but returns a status of a redis delete
operation. Other than that, it does its job, it seems:

use Redis::Hash;
my %h; tie %h, 'Redis::Hash';
$h{"foo"} = "hello";
printf("1: %s\n", defined $_ ? $_ : 'UNDEF') for $h{"foo"};
my($ddd) = delete $h{"foo"};
printf("2: %s\n", defined $_ ? $_ : 'UNDEF') for $h{"foo"};
printf("3: %s\n", $ddd);

1: hello
2: UNDEF
3: 1

Btw, don't use hash traversal as implemented by Redis::Hash
unless your database is really small: it uses the redis KEYS call
and collects all matching keys in memory.

Damien Krotkine wrote:
> Why don't you just use Redis, the Perl module ?
> https://metacpan.org/module/Redis

The Redis::Hash is just a thin wrapper around the Perl module Redis,
by the same author.

> It is of very good quality and performance.

Its performance on pipelined commands is lamentable: it does one packet
roundtrip for each command in a pipeline, instead of sending them all
in one go and receiving an entire reply. There are two bug reports
complaining about this, but it seems the module is unmaintained since
beginning of this year. We ended up rewriting the client-side of the redis
protocol in Perl for SpamAssassin and for amavisd-new projects (TinyRedis),
giving up hope on the CPAN module Redis which we used initially.
It also doesn't support IPv6 (available since redis 2.8.0).

Mark

Pedro Melo

unread,
Oct 2, 2013, 2:22:18 AM10/2/13
to redi...@googlegroups.com
Hi,

On Tue, Oct 1, 2013 at 8:16 PM, Martin R <mondo...@gmail.com> wrote:
Hello, I use redis with perl module Redis::Hash  more here -> http://search.cpan.org/dist/Redis/lib/Redis/Hash.pm


My question is how using this utility , delete, update etc,  values . This is not mentioned in module doc.

The problem is, when I add value to key i.e my $value = $my_hash{key} it works but problem occur when I try to delete this value i.e delete $my_hash{key} unfortunately still returns assigned value ;/

It returns the assigned value because that's the default behaviour of delete.

But if you test exists $my_hash{key} after the delete, it should return false.

The Redis::Hash is a simple Tie layer, all operations are supported but I would recommend that you use Redis.pm directly.

I've inherited that code when I took over the Redis.pm module, I don't recommend you use it, but it should be bug free, the test suite is up-to-date to cover it.

Bye,
--
Pedro Melo
@pedromelo
http://www.simplicidade.org/
xmpp:me...@simplicidade.org
mailto:me...@simplicidade.org

Pedro Melo

unread,
Oct 2, 2013, 2:26:06 AM10/2/13
to redi...@googlegroups.com
Hi,

On Wed, Oct 2, 2013 at 1:25 AM, Mark Martinec <Mark.M...@ijs.si> wrote:
> It is of very good quality and performance.

Its performance on pipelined commands is lamentable: it does one packet
roundtrip for each command in a pipeline, instead of sending them all
in one go and receiving an entire reply. There are two bug reports
complaining about this, but it seems the module is unmaintained since
beginning of this year. We ended up rewriting the client-side of the redis
protocol in Perl for SpamAssassin and for amavisd-new projects (TinyRedis),
giving up hope on the CPAN module Redis which we used initially.
It also doesn't support IPv6 (available since redis 2.8.0).

I've started a new job at the beggining of the year and my time to deal with the pull request dwindled to almost nothing.

If you want to take over maintenership or even help out with the pending pull requests, make sure they are sane, and push out a new release, you're more than welcome. I would rather have Redis.pm thrive under a management that has the time to work on it than to have it linger.

Bye,

Martin R

unread,
Oct 2, 2013, 3:46:48 AM10/2/13
to redi...@googlegroups.com
Thanks, indeed that was my mistake and delete works as it should.

 

    Its performance on pipelined commands is lamentable: it does one packet
    roundtrip for each command in a pipeline, instead of sending them all
    in one go and receiving an entire reply.


So, no doubt performance is key factor, if not Redis / Redis::Hash then what to work with redis from perl script ?

I just add, read and delate redis keys from hash in perl script. Basicly I need Key -> Value system, Redis::Hash looks good but how it will be working with about 10k keys + how expensive is set new redis connection with redis-> new, tie hash, etc in perl ?

Also, how looks performance issue between redis hash and another types which one is recommended as the fastest ?


Mark Martinec

unread,
Oct 2, 2013, 4:53:25 AM10/2/13
to redi...@googlegroups.com
Martin R wrote:

> Thanks, indeed that was my mistake and delete works as it should.

> Its performance on pipelined commands is lamentable: it does one packet
> roundtrip for each command in a pipeline, instead of sending them all
> in one go and receiving an entire reply.
> So, no doubt performance is key factor, if not Redis / Redis::Hash then
> what to work with redis from perl script ?
> I just add, read and delate redis keys from hash in perl script. Basicly I
> need Key -> Value system, Redis::Hash looks good but how it will be working
> with about 10k keys

If your application can make use of batched redis calls (pipelining),
that is the fastest way to access a redis DB, comparable in speed
to Lua scripting.

If the application needs to fetch hash values right after each query
(cannot use command batching), the Redis::Hash is just fine (one
procedure call overhead compared to using Redis.pm directy);
or use the Redis module, or the alternative module Redis::hiredis.

With Redis::Hash just avoid hash traversals (perl functions each/keys/values).
When only using fetch, set, exists and delete that approach is just fine.

> + how expensive is set new redis connection with
> redis-> new, tie hash, etc in perl ?

Each connect takes a couple/dozen of milliseconds, so for any serious
use you should keep connections persistent.

> Also, how looks performance issue between redis hash and another types
> which one is recommended as the fastest ?

If you are asking about Redis::Hash vs. Redis vs. Redis::hiredis, then
Redis::hiredis seems to be the fastest (but in our experiments its
command batching support (append_command) was not binary clean, so
couldn't use it; I may be wrong on this, could be a documentation issue).

If you are asking about redis data types, use what best suits your
application - and check redis documentation, which offers good
indications on what is fast or small and what is more expensive.

Mark

Martin R

unread,
Oct 2, 2013, 5:28:02 AM10/2/13
to redi...@googlegroups.com, mark.m...@ijs.si


Very helpful answer, thanks you Mark and all users involved in subject ;)
Regards.

Jeremy Zawodny

unread,
Oct 2, 2013, 9:41:00 AM10/2/13
to redi...@googlegroups.com
Oh, interesting.  We were talking about this at work recently.  We use Redis.pm heavily and could probably step up to take over maintenance...

Jeremy 

Damien Krotkine

unread,
Oct 2, 2013, 10:17:42 AM10/2/13
to redi...@googlegroups.com
 
Hi Jeremy,
 
I also proposed myself to be co-maint of Redis.pm, I emailed Mark and Pedro privately. I'm sure we can work something together. There is enough work for everybody :)
 

Pedro Melo

unread,
Oct 2, 2013, 10:29:22 AM10/2/13
to redi...@googlegroups.com
Hi,

Still at work in the middle of something, just a quick note that both your help would be most welcome and I can take care of permissions and a "State of the union"-style email when I get home later today.

Martin R

unread,
Oct 2, 2013, 1:29:05 PM10/2/13
to redi...@googlegroups.com
I am going to test both Redis and Redis::Hash on quite busy system which constantly add, update and delete redis hash keys.
If my tests may help I can share it.


Jeremy Zawodny

unread,
Oct 2, 2013, 3:15:46 PM10/2/13
to redi...@googlegroups.com
On Wed, Oct 2, 2013 at 7:29 AM, Pedro Melo <me...@simplicidade.org> wrote:
Hi,

On Wed, Oct 2, 2013 at 3:17 PM, Damien Krotkine <dkro...@gmail.com> wrote:
On Wed, Oct 2, 2013, at 06:41 AM, Jeremy Zawodny wrote:
On Tue, Oct 1, 2013 at 11:26 PM, Pedro Melo <me...@simplicidade.org> wrote:
Hi,
 
On Wed, Oct 2, 2013 at 1:25 AM, Mark Martinec <Mark.M...@ijs.si> wrote:
 
> It is of very good quality and performance.
 
Its performance on pipelined commands is lamentable: it does one packet
roundtrip for each command in a pipeline, instead of sending them all
in one go and receiving an entire reply. There are two bug reports
complaining about this, but it seems the module is unmaintained since
beginning of this year. We ended up rewriting the client-side of the redis
protocol in Perl for SpamAssassin and for amavisd-new projects (TinyRedis),
giving up hope on the CPAN module Redis which we used initially.
It also doesn't support IPv6 (available since redis 2.8.0).
 
I've started a new job at the beggining of the year and my time to deal with the pull request dwindled to almost nothing.
 
If you want to take over maintenership or even help out with the pending pull requests, make sure they are sane, and push out a new release, you're more than welcome. I would rather have Redis.pm thrive under a management that has the time to work on it than to have it linger.
 
Oh, interesting.  We were talking about this at work recently.  We use Redis.pm heavily and could probably step up to take over maintenance...
 
Hi Jeremy,
 
I also proposed myself to be co-maint of Redis.pm, I emailed Mark and Pedro privately. I'm sure we can work something together. There is enough work for everybody :)

Still at work in the middle of something, just a quick note that both your help would be most welcome and I can take care of permissions and a "State of the union"-style email when I get home later today.

Excellent.  Glad to help!

Jeremy 

Martin R

unread,
Oct 5, 2013, 1:42:43 PM10/5/13
to redi...@googlegroups.com, Jer...@zawodny.com, jer...@zawodny.com
I encounter strange situation with perl and redis:

I set new keys and values to redis with Redis::Hash (code below)

           tie %bandwith_hash, 'Redis::Hash', 'bandwith_hash', @Redis_new_parameters;

                 $bandwith_hash{3} = 1000;


everything work well (I can read and write to hash bandwith_hash) except one thing, when I try to get values back using redis-cli I get 'nil' or 'empty list' why ?

In redis-cli I type :

        HGET "bandwith_hash" "3"

 
What is wrong ?

Mark Martinec

unread,
Oct 5, 2013, 8:29:49 PM10/5/13
to redi...@googlegroups.com
Redis data representation as used by Redis::Hash is plain key/value pairs,
not redis hashes. So to get your value back, you need the GET command,
not HGET :

$ redis-cli GET bandwith_hash:3
"1000"


Mark

Martin R

unread,
Oct 6, 2013, 4:27:52 AM10/6/13
to redi...@googlegroups.com, mark.m...@ijs.si

Redis data representation as used by Redis::Hash is plain key/value pairs,
not redis hashes. So to get your value back, you need the GET command,
not HGET :

$ redis-cli GET bandwith_hash:3
"1000"


  Mark

Ahh right of course, thanks Mark ;)

One more question regarding t o performance issue, if I tie perl hash with redis::hash which contain aprox. 100k keys and values may it cause high memory usage and weaken performance ?

Using module Redis instead of Redis::Hash seems to need run my $r = Redis->new; every time when with redis::hash I can skip this since I have redis-server running any time.

Mark Martinec

unread,
Oct 6, 2013, 7:06:39 PM10/6/13
to redi...@googlegroups.com
Martin,

> *One more question regarding to performance issue, if I tie perl hash with
> redis::hash which contain aprox. 100k keys and values may it cause high
> memory usage and weaken performance ?*

No, the size of a redis database has no impact on memory usage
of a client process using Redis::Hash to 'tie' a database to
a perl variable.

> Using module Redis instead of Redis::Hash seems to need run
> my $r = Redis->new; every time

... every time you start a new client process, indeed.
It establishes a connection to socket listened by a redis server process.

> when with redis::hash I can skip this since I have redis-server
> running any time.

There is no difference between using Redis::Hash or calling Redis.pm
directly. Internally the Redis::Hash just calls Redis->new when you
do a tie(). And without a tie() call (or a Redis->new call) you can't
access the redis database.

Mark

Martin R

unread,
Oct 8, 2013, 4:36:21 AM10/8/13
to redi...@googlegroups.com, mark.m...@ijs.si
Thanks Mark, very helpful answers.

Regards, Martin.
Reply all
Reply to author
Forward
0 new messages