Repository navigation
Closed
Description
Hi guys,
We noticed an issue with INCR behaving strange (out of order, numbers going from 200->75, 76, 77, 78, 205, etc). We have narrowed it down to what seems like the Redis pipeline interferring with INCR:
Using this test script:
import time
import arrow
import threading
KEY = 'redis_counter_test'
KEY_SET = 'redis_counter_test_set'
RUNNING = True
import redis
cli = redis.StrictRedis(host='redisst-01')
cli.set(KEY, 0)
def run_redis(tid=None):
"""
This runs in a thread
:param tid:
:return:
"""
global RUNNING, cli
try:
while RUNNING:
t = arrow.now()
t = t.timestamp + t.microsecond / 1e6
kv = cli.incr(KEY)
print '%s,%f,%i' % (tid, t, kv)
# Just call SET so we can see it in redis-cli MONITOR
cli.set(KEY_SET, kv)
time.sleep(1)
except KeyboardInterrupt:
RUNNING = False
raise
##
if __name__ == '__main__':
#signal.signal(signal.SIGTERM, signal_term_handler)
threads = []
for i in range(50):
tid = str(i).zfill(3)
name = 'thread_%s' % tid
thread = threading.Thread(target=run_redis, kwargs={'tid': name})
thread.start()
threads.append(thread)
time.sleep(1)
for i in threads:
i.join()
And running the benchmark:
./redis-benchmark -c 1000 -n 1000000
Watching the redis-cli MONITOR, we see results like:
1409943255.190451 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2200"
1409943255.215354 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2201"
1409943255.219862 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "2202"
1409943255.249790 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2203"
1409943255.304738 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2204"
1409943255.330395 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "2205"
1409943255.334665 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2206"
1409943255.385037 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2207"
1409943255.389381 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "2208"
1409943255.393636 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2209"
1409943255.401909 [0 10.3.0.4:57701] "SET" "redis_counter_test_set" "2210"
1409943255.422840 [0 10.3.0.4:57692] "SET" "redis_counter_test_set" "2211"
1409943255.469287 [0 10.3.0.4:57692] "SET" "redis_counter_test_set" "2212"
1409943255.477729 [0 10.3.0.4:57701] "SET" "redis_counter_test_set" "2213"
1409943255.490303 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2214"
1409943255.494568 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "2215"
1409943255.527437 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2216"
1409943255.532000 [0 10.3.0.4:57701] "SET" "redis_counter_test_set" "2217"
1409943255.553395 [0 10.3.0.4:57701] "SET" "redis_counter_test_set" "2218"
1409943255.557993 [0 10.3.0.4:57692] "SET" "redis_counter_test_set" "2219"
1409943255.562195 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2220"
1409943255.566423 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2221"
1409943255.570486 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2222"
1409943255.574864 [0 10.3.0.4:57697] "SET" "redis_counter_test_set" "2224"
1409943255.579098 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "2223"
1409943255.582730 [0 10.3.0.4:57695] "SET" "redis_counter_test_set" "2225"
1409943255.591624 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2226"
1409943255.616749 [0 10.3.0.4:57697] "SET" "redis_counter_test_set" "2227"
1409943255.620367 [0 10.3.0.4:57695] "SET" "redis_counter_test_set" "2228"
1409943255.625130 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "2230"
1409943255.625140 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2229"
1409943255.667475 [0 10.3.0.4:57695] "SET" "redis_counter_test_set" "2231"
1409943255.675330 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "2233"
1409943255.675780 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2232"
1409943255.687710 [0 10.3.0.4:57697] "SET" "redis_counter_test_set" "2234"
1409943255.700798 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2237"
1409943255.700807 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2236"
1409943255.700815 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2235"
1409943255.784454 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2238"
1409943255.859732 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2239"
1409943255.867187 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2240"
1409943255.905723 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2241"
1409943255.957362 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2242"
1409943255.965260 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2243"
1409943255.965654 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2244"
1409943256.020442 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2245"
1409943256.032574 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2246"
1409943256.120717 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2247"
1409943256.154176 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2248"
1409943256.178031 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2249"
1409943256.229907 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2250"
1409943256.263195 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2251"
1409943256.263204 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2252"
1409943256.291534 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2253"
1409943256.348364 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2254"
1409943256.372185 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2255"
1409943256.381564 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2256"
1409943256.428428 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "2257"
1409943256.428438 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2258"
1409943256.437914 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2259"
1409943256.442599 [0 10.3.0.4:57697] "SET" "redis_counter_test_set" "2260"
1409943256.466376 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2261"
1409943256.517092 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2262"
1409943256.522039 [0 10.3.0.4:57697] "SET" "redis_counter_test_set" "2263"
1409943256.537132 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2264"
1409943256.542170 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2265"
1409943256.577143 [0 10.3.0.4:57697] "SET" "redis_counter_test_set" "2266"
1409943256.577155 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "2267"
1409943256.597273 [0 10.3.0.4:57699] "SET" "redis_counter_test_set" "2268"
1409943256.607319 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "2269"
And now restarting the benchmark, with pipelined requests:
./redis-benchmark -c 1000 -n 1000000 -P 10
We see results like this:
1409943322.855377 [0 10.3.0.4:57704] "SET" "redis_counter_test_set" "5410"
1409943322.881399 [0 10.3.0.4:57733] "SET" "redis_counter_test_set" "5412"
1409943322.881411 [0 10.3.0.4:57725] "SET" "redis_counter_test_set" "5413"
1409943322.881418 [0 10.3.0.4:57735] "SET" "redis_counter_test_set" "5414"
1409943322.881426 [0 10.3.0.4:57700] "SET" "redis_counter_test_set" "5418"
1409943322.881434 [0 10.3.0.4:57701] "SET" "redis_counter_test_set" "5419"
1409943322.881443 [0 10.3.0.4:57696] "SET" "redis_counter_test_set" "5420"
1409943322.881451 [0 10.3.0.4:57728] "SET" "redis_counter_test_set" "5416"
1409943322.881459 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5417"
1409943322.881466 [0 10.3.0.4:57734] "SET" "redis_counter_test_set" "5415"
1409943322.930809 [0 10.3.0.4:57724] "SET" "redis_counter_test_set" "5421"
1409943322.930817 [0 10.3.0.4:57732] "SET" "redis_counter_test_set" "5422"
1409943322.930826 [0 10.3.0.4:57727] "SET" "redis_counter_test_set" "5423"
1409943322.980371 [0 10.3.0.4:57731] "SET" "redis_counter_test_set" "5425"
1409943322.980383 [0 10.3.0.4:57736] "SET" "redis_counter_test_set" "5426"
1409943322.980395 [0 10.3.0.4:57707] "SET" "redis_counter_test_set" "5424"
1409943323.003959 [0 10.3.0.4:57726] "SET" "redis_counter_test_set" "5427"
1409943323.003974 [0 10.3.0.4:57702] "SET" "redis_counter_test_set" "5428"
1409943323.003983 [0 10.3.0.4:57695] "SET" "redis_counter_test_set" "5429"
1409943323.029843 [0 10.3.0.4:57733] "SET" "redis_counter_test_set" "5430"
1409943323.029863 [0 10.3.0.4:57728] "SET" "redis_counter_test_set" "5432"
1409943323.029873 [0 10.3.0.4:57701] "SET" "redis_counter_test_set" "5431"
1409943323.029882 [0 10.3.0.4:57725] "SET" "redis_counter_test_set" "5433"
1409943323.053273 [0 10.3.0.4:57734] "SET" "redis_counter_test_set" "5435"
1409943323.053286 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5436"
1409943323.053295 [0 10.3.0.4:57735] "SET" "redis_counter_test_set" "5434"
1409943323.103183 [0 10.3.0.4:57733] "SET" "redis_counter_test_set" "5438"
1409943323.103197 [0 10.3.0.4:57727] "SET" "redis_counter_test_set" "5437"
1409943323.103205 [0 10.3.0.4:57732] "SET" "redis_counter_test_set" "5439"
1409943323.129213 [0 10.3.0.4:57736] "SET" "redis_counter_test_set" "5440"
1409943323.203915 [0 10.3.0.4:57735] "SET" "redis_counter_test_set" "5443"
1409943323.203928 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5442"
1409943323.203939 [0 10.3.0.4:57734] "SET" "redis_counter_test_set" "5441"
1409943323.253153 [0 10.3.0.4:57733] "SET" "redis_counter_test_set" "5444"
1409943323.278872 [0 10.3.0.4:57736] "SET" "redis_counter_test_set" "5445"
1409943323.328256 [0 10.3.0.4:57733] "SET" "redis_counter_test_set" "5446"
1409943323.377378 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5447"
1409943323.426563 [0 10.3.0.4:57727] "SET" "redis_counter_test_set" "5448"
1409943323.450152 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5449"
1409943323.623720 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5450"
1409943323.772541 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5451"
1409943323.871057 [0 10.3.0.4:57734] "SET" "redis_counter_test_set" "5454"
1409943323.871069 [0 10.3.0.4:57736] "SET" "redis_counter_test_set" "5455"
1409943323.871077 [0 10.3.0.4:57727] "SET" "redis_counter_test_set" "5452"
1409943323.871085 [0 10.3.0.4:57733] "SET" "redis_counter_test_set" "5453"
1409943323.943659 [0 10.3.0.4:57735] "SET" "redis_counter_test_set" "5457"
1409943323.943667 [0 10.3.0.4:57733] "SET" "redis_counter_test_set" "5458"
1409943323.943689 [0 10.3.0.4:57708] "SET" "redis_counter_test_set" "5456"
1409943324.020050 [0 10.3.0.4:57725] "SET" "redis_counter_test_set" "5463"
1409943324.020063 [0 10.3.0.4:57734] "SET" "redis_counter_test_set" "5466"
1409943324.020072 [0 10.3.0.4:57726] "SET" "redis_counter_test_set" "5468"
1409943324.020080 [0 10.3.0.4:57701] "SET" "redis_counter_test_set" "5470"
We see similar results when taking the program output and putting it into a CSV file, when pipelining is enabled, they're out of order.
Redis INFO:
# Server
redis_version:2.8.14
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:783f7d2bf590ba7
redis_mode:standalone
os:Linux 3.2.55-vs2.3.2.16-beng x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.6.3
process_id:7597
run_id:d6e888b8af6ff450d38c7498d7a346f7c6273ae3
tcp_port:6379
uptime_in_seconds:2620
uptime_in_days:0
hz:10
lru_clock:656873
config_file:/etc/redis/server.conf
# Clients
connected_clients:1049
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:777976968
used_memory_human:741.94M
used_memory_rss:756142080
used_memory_peak:2274725120
used_memory_peak_human:2.12G
used_memory_lua:33792
mem_fragmentation_ratio:0.97
mem_allocator:jemalloc-3.6.0
# Persistence
loading:0
rdb_changes_since_last_save:2069162
rdb_bgsave_in_progress:0
rdb_last_save_time:1409942988
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:3
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:80422
total_commands_processed:77268458
instantaneous_ops_per_sec:47677
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:20710336
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:17515
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:260.49
used_cpu_user:590.72
used_cpu_sys_children:4.62
used_cpu_user_children:39.30
# Keyspace
db0:keys=729171,expires=0,avg_ttl=0
Python library versions:
$ pip show hiredis redis
---
Name: hiredis
Version: 0.1.4
Location: /opt/python/python2.7/lib/python2.7/site-packages
Requires:
---
Name: redis
Version: 2.10.3
Location: /opt/python/python2.7/lib/python2.7/site-packages
Requires:
We have seen this on Redis 2.8.3+. Not sure if there's something we're doing obviously wrong
Thanks!
Metadata
Metadata
Assignees
Labels
No labels
Activity
mattsta commentedon Sep 5, 2014
Just to clarify: you are launching 50 threads, each thread does:
That just looks like a race condition because you've got multiple things incrementing then setting values. For a better test, try using unique keys per-thread (
key + str(tid)) to see how everything is talking simultaneously.As far as pipelining goes, I don't see it in your code anywhere? And, I'm slightly confused about how redis-benchmark fits in too. redis-benchmark just runs Redis commands, so... I don't quite see how the combined python script + redis-benchmark + MONITOR interacts?
nabeelio commentedon Sep 5, 2014
@mattsta Hi, yes, 50 threads, just to 'coerce' the problem into showing up earlier. This was just an example python script in conjunction with redis-benchmark as way to reproduce and isolate what we're seeing; our application(s) have similar behavior - large amounts data being put via pipeline, and other apps doing INCR commands. It's a distributed app. The whole purpose of using Redis is that these operations should be atomic, no? Using different keys per-thread would defeat the purpose of using INCR, so this test is more representative test
We just were trying to figure out what we were seeing and just isolating this issue outside of our application. If you look at the output between using pipelines and no pipelines, there's a big difference.
Edit: I see what you're saying about the SET, but it should be in order still. If we take the script output, which is CSV, and plot that in time order, we see the same thing that we see in the monitor
mattsta commentedon Sep 5, 2014
Individual commands are atomic, but pipelines aren't an atomic series of commands. For that you need a Lua script or a MULTI transaction.
A pipeline is just a simple way of bundling up multiple replies at once so you don't have "request->reply; request->reply; request->reply," instead you have "request request request->reply reply reply" — but, each request still executes in Redis when it arrives. They don't all get stored and executed at the end of your pipeline (that's what a MULTI would do).
If you have two pipelines running at the same time and they each do: INCR INCR INCR INCR, there's no deterministic ordering of the INCRs. The only thing we can say is at the end, the incremented key will be at the final count.
Notice in your results how all the numbers are there, but they are just out of order because of race conditions between your parallel (INCR and SET) threads.
nabeelio commentedon Sep 5, 2014
@mattsta Yes, correct, but when there are pipeline operations going on independently, they are affecting the INCR commands (which are not pipelined). They're out order because they're getting set out of order.
For example, the first 10 without pipelines running in parallel to separate
INCRcalls:And here the first 10 with pipelined commands running in parallel to separate
INCRcalls:We are seeing a strange issue where multiple
INCRcalls to the same key would yield something like[200, 75, 76, 77, 78, 205]. That's where we've noticed this ordering thing.The main problem is this - the value in the actual counter (
redis_counter_test) ends up being incorrect, so if there are pipelined operations going on in parallel (from another process), the process callingINCRBY redis_counter_test 1would be5420on one call, and then callingINCRagain, instead of giving5421, gives5416, then5417,5415, and then finally getting to a proper position of5421Hopefully that's a little clearer, sorry, I'm not explaining it very well.
mattsta commentedon Sep 5, 2014
Well, you just picked a good run in the example. :) If you go down a little further in that same example, we have 28->30->29:
Also, with pipeline operations, it stores the value at the time of the run. So, if you run a million INCR in an uncontested pipeline, you'll get back 1 2 3 ... 1000000. If you run a million increments in two pipelines at the same time, you'll get random results in the middle (one result may be: 1 3 4 6 12 13 15 22 ..., but the other result would be: 2 5 7 8 9 10 11 14 16 ...). Combine the random intermediate pipeline return values with the race condition INCR->SET operations across threads, and we see out of order return values.
The return values from the pipeline will, kinda by definition, be old values. Pipeline means "let me run many commands, save the results of each command for me, then return the saved results at the end." Redis saved the results during the execution and it's returning them to you in a bundle much later than the original execution.
Update RedisTimeSeries module to v8.9.80 (#15336)