cptlobster.dev

DNS as a Database: A Theoretical Concept

  • software
  • cursed
  • dns
  • database

I thought up an idea for a very easy-to-use distributed database that could be used worldwide with near instantaneous response times for reading data. While this might be slower for writing data (it will update Eventually™), it could feasibly be used to store some form of data that you likely won’t change too often. It utilizes a service that is integral to the internet today: the domain-name service (DNS).

The Intended Use of DNS

When you connect to a site (like google.com), your computer doesn’t directly try to find google.com. Instead, it uses a DNS to look up the IP address that belongs to google.com. DNS is run off of servers around the globe that all pull from a central source of truth (the 13 root servers run by ICANN), and providers (such as Cloudflare) will push records to those servers which will propagate elsewhere.

DNS Records

Your average run-of-the-mill DNS record for a website is known as an A record; it just stores an IPv4 address. Nowadays, there are also AAAA records which correspond to IPv6 (but isn’t used quite as often). However, other records exist, for example a CNAME record (which just redirects to another domain name), an MX (Mail Exchange) record (used for email), but the one we are interested in is the TXT record. This just stores plaintext data, and is generally used to verify ownership of domains (for example, GitHub Pages will use a TXT record to verify ownership of a domain).

This TXT record is the one we want to abuse.

The Database Structure

We will call our database example.com. We can create a key/value store of sorts, where the key name is just a subdomain of example.com (for example: 1.example.com, 2.example.com, etc.), and the value is stored in the TXT record for that subdomain. How the data is stored is left as an exercise to the reader (you could probably use JSON, or Avro, or any other serialization technique.)

Implementation

Reading

The simplest way to read a database would be a simple nslookup query:

nslookup -q=TXT [key].example.com

will return something like:

Server:		1.1.1.1
Address:	1.1.1.1#53

Non-authoritative answer:
[key.example.com]	text = "value"

Authoritative answers can be found from:

Use a little grep and sed magic:

nslookup -q=TXT [key].example.com | grep "text" | sed -r 's/^\W text \"(.*)\"$/\1/'

will return only the value.

I looked into implementing this using JavaScript for a web application, but there is no way to execute a DNS query manually, or get a TXT record. Even if you compile nslookup for wasm, it won’t be capable of making DNS queries. You could use DNS-over-HTTPS, but that isn’t quite as widespread as normal DNS.

Writing

There isn’t a very standard way to write to DNS as a database, as each provider has their own API. You’ll need to look this up for whomever you use.

Pros and Cons

Pros:

  • Basically free content delivery around the globe.
  • Near instantaneous reads for your client applications.
  • No need to think about caching!

Cons:

  • Slow to update records.
  • Limit of 65,535 bytes per DNS message.
  • Implementation will be a challenge in web applications.
  • Your DNS provider will probably send trained assassins to your house with the amount of times that you are updating records.

You probably wouldn’t want to use this for any important database (hell, you wouldn’t want to use this at all probably). However, it is entirely possible that you could use this for storing some kind of data (for example, what the IP address of your server is for a specific domain name…).

POS (10, 60)
ELEV 0.55 KM