atomic operations redis

2,337 views
Skip to first unread message

Елена Исаева

unread,
Jun 25, 2012, 5:37:34 AM6/25/12
to redi...@googlegroups.com
Hello, I want to ask about the atomic operations in redis. Where to read something that all operations in redis atomic, is this true? Example: I have a new type of list, and from it selected the first 10 lines of command LRANGE overwriting the team LPUSH and LTRIM. So if there will be 2 simultaneous connections of users that normally do перезапишется this list, that is 1 user to overwrite this list and then 2 the user to overwrite. And another question, what if-in-1 hash store about 5 million of the fields? 

Marc Gravell

unread,
Jun 25, 2012, 5:51:12 AM6/25/12
to redi...@googlegroups.com
Each *individual* command is guaranteed to be atomic, but groups of commands are only atomic if you use a transaction. In the case of LPUSH/TRIM to maintain a fixed-size scrolling list, that shouldn't be necessary *if* you don't mind occasionally getting a few extra items, i.e. if you have

conn1: LPUSH LTRIM
conn2: LPUSH LTRIM
conn3: LRANGE

you *could* get that processed as:

conn1: LPUSH
conn2: LPUSH
conn3: LRANGE (returns n+2 items if you say "everything")
conn2: LTRIM  (removes 2 extra items instead of the 1 you might normally expect)
conn1: LTRIM (removes nothing)

of course, you can "fix" this by simply specifying the correct limits on LRANGE.

Does that make sense? have I misunderstood the question?

When you *need* multiple commands to be atomic, use MULTI/EXEC/WATCH/UNWATCH/DISCARD: http://redis.io/topics/transactions/

Marc

On 25 June 2012 10:37, Елена Исаева <obla...@gmail.com> wrote:
Hello, I want to ask about the atomic operations in redis. Where to read something that all operations in redis atomic, is this true? Example: I have a new type of list, and from it selected the first 10 lines of command LRANGE overwriting the team LPUSH and LTRIM. So if there will be 2 simultaneous connections of users that normally do перезапишется this list, that is 1 user to overwrite this list and then 2 the user to overwrite. And another question, what if-in-1 hash store about 5 million of the fields? 

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To view this discussion on the web visit https://groups.google.com/d/msg/redis-db/-/eGmUDXnnX44J.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.



--
Regards,

Marc

Елена Исаева

unread,
Jun 25, 2012, 6:00:29 AM6/25/12
to redi...@googlegroups.com
No, You understood everything correctly, another question now have a list and it is about 300 values, and I need to replace these values other values for this of course will need to cycle, but I'd like to wrap it in a transaction, but as it is to create one? I use the client phpredis, and here is the code in php: 
 $redis->lTrim('posts', 10, 2);//удаляем все записи
//цикл для добавления новых записей
foreach($array as $key => $val){
$redis->lPush('posts', $val);
}

понедельник, 25 июня 2012 г., 13:51:12 UTC+4 пользователь Marc Gravell написал:
To unsubscribe from this group, send email to redis-db+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.



--
Regards,

Marc

Sergei Tulentsev

unread,
Jun 25, 2012, 7:52:43 AM6/25/12
to redi...@googlegroups.com
Not everyone here can read Russian, so watch carefully what you post. It is in your own interests to make your post understandable :)

To view this discussion on the web visit https://groups.google.com/d/msg/redis-db/-/JtpWPvZqC-sJ.

To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.



--
Best regards,
Sergei Tulentsev

Pierre Chapuis

unread,
Jun 25, 2012, 8:21:26 AM6/25/12
to redi...@googlegroups.com
As Marc said, see http://redis.io/topics/transactions/ for the traditional way to do it (MULTI/EXEC).

As an alternative you could use a Lua script (http://redis.io/commands/eval). It is more powerful than MULTI/EXEC because it allows you to read data and use it for another command. Scripts are always executed in an atomic way.

Елена Исаева

unread,
Jun 25, 2012, 11:36:36 AM6/25/12
to redi...@googlegroups.com
And one more question: I Have a hash of the contents hset 'key' 1 'value1'. And I update this value each time, as is the publication of the post, first get the value of this hash and then to him add a new value: hget 'key' 1 and hset 'key' 1 'value1value2'. But what will happen if there will be 2 concurrent connections. What will be the final value of the hash? it will be 'value1value2value3' or 'value1value3' 

понедельник, 25 июня 2012 г., 13:37:34 UTC+4 пользователь Елена Исаева написал:

Didier Spezia

unread,
Jun 25, 2012, 12:50:17 PM6/25/12
to redi...@googlegroups.com

>>  What will be the final value of the hash? it will be 'value1value2value3' or 'value1value3'  

If you get some hash field, and set it to an updated value, you have a race condition:
the result with mulitple clients will be non deterministic. As Marc and others explained,
individual commands are atomic, but not group of commands.

This is exactly where a WATCH/MULTI/EXEC block or a server-side Lua script would help.

Regards,
Didier.

Елена Исаева

unread,
Jun 25, 2012, 1:02:03 PM6/25/12
to redi...@googlegroups.com

Everything is clear, but as far as I know use  WATCH/MULTI/EXEC block or a server-side Lua script  not give the server to execute commands as long as does not complete the command, or am I in the wrong? 
And it can still be a small example of, if not difficult, on account of the receipt of the hash and its change on the basis of the obtained value (as I described in the previous) 
понедельник, 25 июня 2012 г., 20:50:17 UTC+4 пользователь Didier Spezia написал:
Reply all
Reply to author
Forward
0 new messages