How exactly does a TLD name server resolve a query and identify the authoritative name server with the required information?
A TLD nameserver does nothing differently than any other authoritative nameserver at any other node in the DNS tree, that is per design and a property of the DNS protocol as a decentralized database.
You are probably forgetting the "registration plane". DNS deals with resolution aka the "resolution plane". It explains how to get the data, not how the data was stored in the first place (except with things like DNS updates, but that is for a local use not a global one).
The registration plane is that there are registries and registrars typically. A registry manage a TLD. It ensures the nameservers are working and it gets order from registrars. Registrars have end clients that choose domains and register them. Registrars send commands to registries typically with a protocol called EPP.
So in summary a registry maintain a database (typically relational) with all the data, including things not published in the DNS but available through other protocols like whois or RDAP (ex: contacts). This database is used to configure TLD authoritative nameservers with all the delegations, aka the NS
records.
Does a TLD name server for ".com" store the corresponding name server for every single website with the ".com" TLD?
Yes, per above discussion.
Is that not a massive database?
For some TLDs like .com
yes, but:
- only a few hundred millions of records, which is something databases can completely handle, they are other cases with far more data
- TLD like that is an exception, most TLDs are far smaller; typical ccTLDs are a few millions domains for example
Or is there some kind of algorithm that decides which domain names are assigned to which authoritative name servers and thus running the domain name through the algorithm you can identify the right authoritative name server?
No there is not. When you register a domain name (at any level in the DNS tree in fact), you are free to choose whatever nameserver you want to have handling it (except some very specific edge cases, like .tel
in the past where the registry was forcing specific nameservers).
Sorry this seems like a really simple question but I can't find anything online,
https://en.wikipedia.org/wiki/Domain_name_registry is short but can be a good introduction.
As soon as you understand there is resolution on one side (the DNS) and registration on the other (the whole registry/registrar things), it should help you understand things better.