How does golang handles DNS caching

1,057 views
Skip to first unread message

Kishan Pandey

unread,
Jun 7, 2023, 8:13:49 AM6/7/23
to golang-nuts
My application is using gorm to connect to the database FQDN.  If I am making changes to DNS record for the FQDN , the application needs to be restarted to take the DNS change. I think it is making the DNS query only at the application start time but not afterward.

I would like to know how does golang manages the DNS changes. I looked at gorm issue and found one comment stating that gorm does not manage DNS cache its usage system default. 

Ian Lance Taylor

unread,
Jun 7, 2023, 2:42:17 PM6/7/23
to Kishan Pandey, golang-nuts
On Wed, Jun 7, 2023 at 5:13 AM Kishan Pandey <pandey....@gmail.com> wrote:
>
> My application is using gorm to connect to the database FQDN. If I am making changes to DNS record for the FQDN , the application needs to be restarted to take the DNS change. I think it is making the DNS query only at the application start time but not afterward.
>
> I would like to know how does golang manages the DNS changes. I looked at gorm issue and found one comment stating that gorm does not manage DNS cache its usage system default.

The Go standard library doesn't cache DNS lookups. If you look up a
name twice, it will generate two different DNS requests, or two
different calls to getaddrinfo. That is, Go expects to be contacting
a caching resolver. Of course, a Go program can do a DNS lookup and
cache the results outside of the standard library.

So while I don't know what problem you are encountering, it doesn't
seem likely to be in the Go standard library.

Ian

Slawomir Pryczek

unread,
Jun 9, 2023, 2:09:12 AM6/9/23
to golang-nuts
Probably
1. You're using connection pool in gorm
2. Gorm is using golang's connection pool
3. The connection gets cached/reused while domain's IP changes (as the address is same but ip changes and the change can't be easily detected)

So the solution would probably be to do a lookup in a separated goroutine, every X seconds using net.LookupIP (20 seconds should be good). Then connecting directly to that IP instead of FQDN using gorm.

BTW: If you're using mysql you should also add skip-name-resolve to server config as it'd overload DNS server if you're making a lot of connections and is totally unnecessary.

Kishan Pandey

unread,
Jun 13, 2023, 8:17:11 AM6/13/23
to Slawomir Pryczek, golang-nuts
Thanks, Slawomir,
Yeah, gorm connection pooling was the culprit. After I set up the max connection lifetime it started taking the change in FQDN without application restart. 

We already have a setting for the skip-name-resolve value.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/1FxaM11WZ3M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/24fb2c2b-26cb-40b7-adde-d717db9bfcefn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages