2

I have an AWS EC2 VM. I want to run a bunch of scripts on a PostgreSQL server hosted on the same machine. When I use the IP address of the server in scripts, it took almost 5 seconds per script. But when I use the loopback address instead of server IP, it hardly took 1 second per script.

what can be the issue ?

I performed the same activity on a different server and scripts were taking almost the same time with both IP and loopback address.

YogeshR
  • 71
  • 5
  • Loop back address will always be faster than IP address assigned to the network adapter. When you access localhost, your /etc/hosts file will tell your server not to look any further and redirects you to your own server. This is normal behavior and no need to worry about it. – Azfar Ullah Khan Apr 04 '19 at 11:50
  • @AzfarUllahKhan : I think u didn't read the last line. – YogeshR Apr 04 '19 at 14:49
  • Did the servers have seted up dns ptr/a right? It seems to me that some of your servers can try to resolve theiself and fails. – Aroly7 Apr 11 '19 at 08:55

1 Answers1

8

By default on Linux traffic from the system itself to the (public) IP-address of the server will not go to the actual NIC nor out over the wire, when that ip-address is configured on the server itself.

By default Linux uses a single network stack and communication to and from all configured ip-addresses will be in-memory in the Linux kernel's network stack, even when the source and destination ip-addresses are associated with different NIC's.

The network speed when communicating with that IP-address should therefore be the same as the loopback interface and is only limited by how fast the system is, not the wire speed of the network uplink (and will usually exceed the bandwidth of that uplink).

When there is a significant difference in those speeds some notable exceptions to that default behaviour come to mind:

  • The public ip-address of the server (or whatever the DNS name you're using resolves to) is not a configured ip-address of that system.
    Check with ip addr.

    • For instance in many cloud deployments the public IP-address associated with an instance is NAT construct configured and maintained in the providers network and that public ip-address is NOT configured in the server itself. (That is also what allows you to use the management layer to remove that public ip-address from one system and assign it to another.) Traffic from the server to that public ip-address will not, as far as your system is aware, have a local destination. It needs to be transmitted before the external NAT mapping will direct it back.
    • Similar the hostname / public ip-address may be associated with a loadbalancer / reverse-proxy and when the applications tries to address itself the request will be routed via the load balancer back to the node (possibly even to a completely different one).
  • The system can have policy routing enabled. That can force the system to send traffic between different IP-addresses on different NIC's out over the wire to the external network. Check with ip rule list.
  • Network namespaces allow the Linux kernel to set up more than one network stack, each with their own IP and routing settings. Traffic between different network namespaces will usually also be routed outside of the system itself and much slower than the loopback interface. Check with ip netns list

I'm not overly familiar with Postgres but typically you can get the most performance with services that support a unix domain socket in addition to TCP/IP connections by using a socket connection instead of either using the localhost or external IP-address as that takes away the overhead of framing the data in IP packets.

So rather than using the external IP-address or the localhost 127.0.0.1 address, enable and connect to the Postgresql socket.

HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • 1
    Without further information i think you're correct about their using either the public IP or a elastic IP associated with the VM. – Mark Wagner Apr 11 '19 at 16:39
  • 1
    Another reason may be a DNS lookup on the "public" IP address, that does not happen for the loopback address that it written into `/etc/hosts`. Since this is Postgres, do not forget that the server will probably request rDNS for the connecting client. – Law29 Apr 15 '19 at 19:53