The app logs "Connection timed out" when connecting to the database. How do you troubleshoot?
IntermediateTroubleshootingDatabase connections in apps
Answer
Short answer
A timeout means packets are being dropped somewhere between the app and the database — almost always a network or firewall issue: security group / NSG rules, a Kubernetes NetworkPolicy, wrong subnet routing, a private endpoint/DNS mismatch, or the database not being reachable from that network at all.
Step by step (from inside the pod or app server)
nslookup db.prod.internal # 1. resolves to the IP you expect? (private vs public!)
nc -zv db.prod.internal 5432 # 2. times out? → network path blocked
Then check, in order:
- Database firewall — RDS security group / Azure DB firewall / NSG allows 5432 from the app's security group or subnet.
- App-side egress — outbound rules, NAT, egress firewall.
- Kubernetes NetworkPolicy — is egress from the namespace to the DB allowed?
- Routing — app and DB in different VPCs/VNets? Peering or Transit Gateway routes and DNS for private endpoints.
- DNS — does the name resolve to a public IP while public access is disabled?
Timeout vs refused
| Error | Meaning |
|---|---|
timed out |
Packets dropped → firewall/routing |
connection refused |
Reached the host, nothing listening on that port → DB down / wrong port |
password authentication failed |
Network fine → credentials |
What to say in the interview
"Timeout points at the network layer. From inside the pod I check DNS resolution and nc to the port, then work through the DB's security group, NetworkPolicies, routing or peering and private DNS. A refused connection would instead mean the port isn't listening."
More on Database connections in apps
How does an application connect to a database, and how should connection details be passed?BeginnerAfter deployment, the app can't connect to the database. How do you troubleshoot it?IntermediateWhat is a connection string? Explain its parts.BeginnerWhat are the default ports for common databases?BeginnerWhy should database credentials never be stored in code or Git?BeginnerHow do Kubernetes Secrets work, and are they secure?Intermediate