hfingler
hfingler

Reputation: 2004

First GET from local redis is always faster than the following to the same object

I've been messing around with redis as a cache for a program and I'm getting this strange behaviour that I can't wrap my head around. I'm doing a get on the same 1MB object a few times and the first get is always faster.

Redis-server is run without any config $redis-server.

The connection is created once: rc = redis.Redis(host='127.0.01', port=6379, db=0)

The get is done somewhat like this:

from timeit import default_timer as timer
for i in range(7):
   s = timer()
   buf = rc.get(bk)
   redis_keys[bk] = buf
   t = timer()
   ...
   sleep(1)

Now if I print the elapsed times, I get something like this:

elapsed: 3.965349002100993
elapsed: 5.852620000950992
elapsed: 6.301352994341869
elapsed: 4.343975997471716
elapsed: 5.502833999344148

If I write the contents of the get to a file it seems to make it more radical:

elapsed: 4.384059997391887
elapsed: 8.715648000361398
elapsed: 8.873455997672863
elapsed: 8.82012600050075
elapsed: 8.847879005770665

What could be the cause of this? Maybe some redis config I'm missing?

Thanks.

Upvotes: 1

Views: 81

Answers (1)

hfingler
hfingler

Reputation: 2004

Thought I had THP disabled, but I didn't restart redis after. Solution was to run the following and restart redis-server.

echo never | sudo tee -a /sys/kernel/mm/transparent_hugepage/enabled
echo never | sudo tee -a /sys/kernel/mm/transparent_hugepage/defrag

from this question: https://unix.stackexchange.com/questions/99154/disable-transparent-hugepages

Upvotes: 1

Related Questions