skip to content

In a MongoDB connection string, what does the mongodb+srv:// scheme add over a plain mongodb:// seed list?

level: juniorimportance: should knowfreq 38%

answer

  1. one name instead of many hosts
  2. two DNS record types
  3. TLS without asking
  4. only three options from DNS
  5. replica set members come from monitoring

basics

~20 s

A mongodb+srv:// string names one DNS hostname instead of listing hosts: the driver reads SRV records for the seed list and a TXT record for a few default options, and it turns TLS on by default.

solid answer

~40 s

With `mongodb+srv://` you give one hostname and no port. The driver queries the SRV records at `_mongodb._tcp.<hostname>` to build its seed list, ports included, and a TXT record on the hostname that may supply only `authSource`, `replicaSet` and `loadBalanced`. TLS is enabled implicitly; you can turn it off with `tls=false` in the string or the client options, never in TXT. Returned hosts must normally share the hostname's parent domain, or the driver raises an error. After that first lookup, a replica set's membership comes from server monitoring, not DNS: the driver rescans SRV only for a sharded topology, at least 60 seconds apart. The gain is that hosts can move without anyone editing connection strings. The price is that DNS becomes a startup dependency.

code

plaintext · 6 lines
plaintext
Record                            TTL   Class    Priority Weight Port  Target
_mongodb._tcp.server.example.com. 86400 IN SRV   0        5      27317 mongodb1.example.com.
_mongodb._tcp.server.example.com. 86400 IN SRV   0        5      27017 mongodb2.example.com.

Record              TTL   Class    Text
server.example.com. 86400 IN TXT   "replicaSet=mySet&authSource=authDB"

go deeper

for a junior

Recall the three additions: hosts from SRV records, a few default options from a TXT record, and TLS switched on. Say that the string names exactly one host and no port.

for a middle

Explain the lookup order and validation: the _mongodb._tcp prefix, the parent-domain check, ports taken from the records, and which three options TXT may set. Mention that string options override TXT.

for a senior

Show you know where SRV stops mattering. Replica set membership comes from monitoring afterwards, so DNS edits do not move running clients, while DNS availability does affect every process that starts during an outage.

for a principal

Weigh operational freedom against a new dependency: hosts can change without redeploying connection strings across services, but startup now relies on DNS, and any exception to the domain check weakens a safeguard.

## Two ways to name a deployment A MongoDB **connection string** tells a driver where to start and how to behave. The standard form lists hosts: ```plaintext mongodb://db0.example.com:27017,db1.example.com:27017/?replicaSet=rs0 ``` Those hosts are a **seed list**: a starting set from which the driver discovers the rest of the deployment. The list does not have to be complete, because the deployment itself reports its members. The other form, the **DNS seed list** format, replaces the list with one name, and the driver turns that name into a seed list before it contacts any server. Atlas connection strings normally use this format, but it is a driver feature that works with any deployment whose DNS you control. ```plaintext mongodb+srv://server.example.com/ ``` ## What the driver does with the hostname 1. **Validates the string.** A port, or more than one host name, is a parse error, and no DNS query is made. 2. **Queries SRV records** at `_mongodb._tcp.<hostname>`. The `mongodb` part comes from `srvServiceName`, and the protocol is always `tcp`. 3. **Verifies every returned host.** By default it must share the parent domain of the name you gave; when that name has fewer than three parts, it must also have one more domain level. A host that fails raises an error, and the driver does not connect to it. 4. **Queries the TXT record** on the hostname for default options. 5. **Enables TLS** unless told otherwise, then hands the seed list to normal server discovery. An SRV record carries a port and a target host, and both are used. It also carries priority and weight, which the specification tells drivers to ignore. So SRV cannot steer traffic towards a preferred member: that is the job of server selection, later on. ## What a TXT record may carry A TXT record supplies defaults as a short query string, for example `replicaSet=mySet&authSource=authDB`. The rules are strict: - Only `authSource`, `replicaSet` and `loadBalanced` are allowed; any other option is an error, so `maxPoolSize` cannot live in DNS. - `ssl` may not be set there, and neither can TLS be switched off there. - More than one TXT record for the same name is an error. - Options in the connection string or in client code override what the TXT record says. ## The two schemes side by side | Question | `mongodb://` | `mongodb+srv://` | |---|---|---| | Hosts in the string | one or more | exactly one DNS name | | Port in the string | allowed | parse error | | TLS | only if you enable it | on unless you set `tls=false` | | Default options from DNS | no | `authSource`, `replicaSet`, `loadBalanced` | | SRV lookup at startup | no | required | ## After the first lookup SRV provides the **seed list**, and nothing more for a replica set. Once connected, the driver's monitors call `hello` on each member and learn the current member list from the replies. The specification says a driver must not rescan SRV when the topology is a replica set or a single server. A member added next week is therefore found through monitoring, and changing DNS only helps clients that start afterwards. A sharded cluster differs, because each `mongos` does not report the other routers. For a Sharded topology the driver rescans SRV for as long as it runs, using the lowest TTL among the records but never less than 60 seconds, and it adds or removes routers to match. This behaviour cannot be turned off. `srvMaxHosts` limits how many of the returned hosts a client uses, and setting it to a positive value together with `replicaSet` or `loadBalanced=true` is an error. ## What it costs - **DNS becomes a startup dependency.** No records, or a DNS error, means an error instead of a client that finds servers. A driver that resolves asynchronously may construct the client and fail later. - **CNAME is not supported** on the hostname, because it could break the domain check. - **The domain check is a security measure.** `srvAllowedHostsSuffix` and `srvHostValidator` relax it, and the specification requires drivers to document that as dangerous. - **SRV-only options are errors elsewhere.** `srvServiceName` or `srvMaxHosts` on a `mongodb://` string fails validation.

  • Why does a driver insist that SRV results share the hostname's parent domain?
    It is a security check: without it, a tampered or mistaken DNS answer could point a client at hosts in an unrelated domain, which could then receive its credentials. The specification treats `srvAllowedHostsSuffix` and `srvHostValidator`, which relax the check, as dangerous options that drivers must document as such.
  • You add a fourth member to a replica set and update only the SRV records. What do running clients do?
    They learn about the new member from `hello` replies once the replica set configuration includes it, not from DNS, because SRV is not rescanned for a replica set. If you update DNS but never reconfigure the set, running clients ignore the change and the new clients simply get one more seed.

  • mongodb+srv keeps replica set membership in DNS, so running clients poll it for new members.
  • You can add a port after the hostname in a mongodb+srv connection string.
  • The TXT record can hold any connection option, such as maxPoolSize or retryReads.
  • SRV priority and weight let you steer the driver towards a preferred member.
  • The +srv form only shortens the host list and leaves the TLS setting unchanged.