My codebase is in python. And this is how I access redis.
import os
import redis
from dotenv import load_dotenv
load_dotenv()
host = os.getenv("REDIS_HOST")
port = int(os.getenv("REDIS_PORT"))
host, port = host, 6379
class RedisCache(redis.Redis):
def __init__(self):
self.deserializer = lambda m: json.loads(m.decode("utf-8"))
self.serializer = lambda m: json.dumps(m)
super().__init__(
host=host, port=master_port, db=0
)
def get(self, key):
return self.deserializer(super().get(key))
In the host if i give localhost: this is the profiler. The socket.socket function in redis is taking 1second and the entire api is taking 1.03seconds
if i change the host from localhost to 127.0.0.1 the entire api itself is taking 30ms
in redis-dist.conf file bind is defined for 127.0.0.1 localhost in hosts file is defined for both 127.0.0.1 and ::1(ipv6). even after removing ipv6 it is same.
Comment From: sundb
@AjayEdupuganti jay can you use socket.gethostbyname('localhost') and `socket.gethostbyname('127.0.0.1') to see how long they take?
Comment From: BHznJNs
@AjayEdupuganti jay can you use
socket.gethostbyname('localhost')and `socket.gethostbyname('127.0.0.1') to see how long they take?
I made a test, the socket.gethostbyname('localhost') has a cost of about 8ms comparing to the socket.gethostbyname('127.0.0.1')
Comment From: sundb
@BHznJNs you can get the query time via dig localhost.
Comment From: BHznJNs
@sundb I made a test again with WSL Debian 11, the socket.gethostbyname('localhost') has a 3ms overhead and with the dig command, the query time to localhost and 127.0.0.1 are both 0ms
Comment From: sundb
I see the cost of 3 seconds is on windows? maybe you should copy the python script to debian to verify it.
Comment From: BHznJNs
3 seconds is maybe a bit long, in my case, in windows, it's about 8ms; in debian, it's 3ms; and I also tested it with a online compiler, it's around 8ms. Here is my testing code:
import time
import socket
start_time1 = time.time()
res1 = socket.gethostbyname('localhost')
end_time1 = time.time()
start_time2 = time.time()
res2 = socket.gethostbyname('127.0.0.1')
end_time2= time.time()
print(end_time1 - start_time1, end_time2 - start_time2)
Comment From: sundb
did you clean the dns cache after localhost in hosts file is defined for both 127.0.0.1 and ::1(ipv6). even after removing ipv6 it is same.?
Comment From: BHznJNs
ur right, after i cleared the dns cache, the dig query time for localhost became 9ms
Comment From: sundb
i think you can try the python script, if you don't clean the cache, socket will still try to connect to ipv6 first.
Comment From: BHznJNs
i tested several times, the result is nearly same to the above. Maybe it's because i didn't clear the dns cache correctly. I also tried some domains, i guess the 3 second of dns query time is imposible since most of the domains i tested has a query time of lower than 100ms, i dont think the access to localhost will has the more overhead than network domains.
Comment From: sundb
@BHznJNs yes, it's impossible to cost 3 seconds for DNS, I mean, retry the first script: https://github.com/redis/redis/issues/13471#issue-2460694948 because redis is bound to 127.0.0.1, the socket may make an ipv6 connection first, and only use ipv4 when it fails.