Get last inserted member

1,480 views
Skip to first unread message

gokan.ek...@gmail.com

unread,
Dec 7, 2014, 2:47:45 PM12/7/14
to redi...@googlegroups.com
Hello,

I think Redis works like a map<string, string> and map<string, set<string>> since 2.4.0 with members.
I think there is no "sort" concept in Redis, unless I use a ZADD but if I use ZADD I have to precise a sort value myself (because there is no auto-increment)... That's the reason why I prefer to use SADD.

My questions are :
  1. Can you tell me if I am wrong with my comprehension about sort in Redis ?
  2. Is it possible to get the last member inserted into a key ? Or should I use another key which represent the last inserted element ?


Thanks

Matt Palmer

unread,
Dec 7, 2014, 5:51:47 PM12/7/14
to redi...@googlegroups.com
On Sun, Dec 07, 2014 at 11:47:45AM -0800, gokan.ek...@gmail.com wrote:
> Hello,
>
> I think Redis works like a *map<string, string>* and *map<string,
> set<string>>* since 2.4.0 with members.
> I think there is no "sort" concept in Redis, unless I use a ZADD but if I
> use ZADD I have to precise a sort value myself (because there is no
> auto-increment)... That's the reason why I prefer to use SADD.
>
> My questions are :
>
> 1. Can you tell me if I am wrong with my comprehension about sort in
> Redis ?

Your comprehension is correct. Redis sets have no conception of sorting, or
the order of insertion.

> 2. Is it possible to get the last member inserted into a key ? Or should
> I use another key which represent the last inserted element ?

No. Yes. If you wish to ensure that there are no race conditions with
regards to setting that other key, you should use a Lua script to add the
item to the set and then set the other key (a Lua script runs atomically, so
the two operations are guaranteed to run without anything else getting in
the way), or use a MULTI/EXEC block to apply both operations together (as
described in http://redis.io/topics/transactions).

There's an open question you may not have considered, though -- what happens
if the item you're adding to the set already exists? Do you want to keep a
record of the last item that was *added* to the set (that is, it wasn't
already in the set before you did the SADD) or do you want the value of the
last SADD call, regardless of whether that item was already in the set?
They're rather different things, and the code to achieve them will be quite
different.

- Matt

gokan.ek...@gmail.com

unread,
Dec 7, 2014, 6:47:21 PM12/7/14
to redi...@googlegroups.com, mpa...@hezmatt.org
Thank you for your help Matt.

I cannot use transactions, because I would like to use Redis in cluster mod.
Redis Cluster (Redis 3.x which is in RC1 for the moment) allows transactions, but I have to work with stable version (Redis 2.8.x).
I know it's risky, and I cannot wait for stable release... maybe should I use another nosql database...

Well, all the members I will add are unique (each member is a JSON string with a timestamp inside), and if I try to add an existing member, the documentation says :
Specified members that are already a member of this set are ignored.

Thanks,

Josiah Carlson

unread,
Dec 8, 2014, 1:13:56 AM12/8/14
to redi...@googlegroups.com, mpa...@hezmatt.org
You can use transactions in Redis cluster, you just need to ensure that both keys are in the same slot. This is trivial:

MULTI
SADD {key}:set ...
SET {key}:last ...
EXEC

As long as the parts between the {} are the same, and you leave the {} in there, Redis cluster will use that part as the shard key. That will let you use transactions.

 - Josiah



--
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/d/optout.

gokan.ek...@gmail.com

unread,
Dec 8, 2014, 4:54:47 AM12/8/14
to redi...@googlegroups.com, mpa...@hezmatt.org
Thanks for your reply Josiah,


But I haven't precised I am using Jedis for doing my client part. Jedis documentation says that if I want to use Redis in cluster mod :
  • I have to use ShardedJedis for Redis' stable version (2.8.x), but I cannot use transactions.
  • I can use JedisCluster with transactions for Redis 3.x, but Redis 3.x is not stable yet !

Sources :

Thanks

Josiah Carlson

unread,
Dec 8, 2014, 1:37:38 PM12/8/14
to redi...@googlegroups.com, mpa...@hezmatt.org
That the Jedis sharded Redis client doesn't support transactions is unfortunately broken (other clients don't have this issue), as given tagged keys (that's the {key} part I was talking about), you can verify correctness prior to execution.

No matter, this is further trivially solved with a Lua script, which is available in Redis 2.6 and later. Your keys would be: '{key}:set' and '{key}:last', and your argv value would just be the value you are adding/setting. The below is the content of the Lua script.

redis.call('SADD', KEYS[1], ARGV[1])
redis.call('SET', KEYS[2], ARGV[1])


 - Josiah

Reply all
Reply to author
Forward
0 new messages