PHP DNS Server & Benchmarking Vs PDNS-MySQL
I have been running a hobby DNS Hosting service for couple of years. DNS servers are powered by Power DNS (aka PDNS) with Mysql backend on commodity hardware. Recently I have been feeling the need for geo/failover/load-balancing DNS service. PDNS has support for geobackend but the installation is not as straight forward. And in addition I have been wanting to learn more about DNS (protocol) so I thought I should write my own DNS server.
PHP is the language I am most familiar with. However I wasn't sure if it is going to be just as fast as PDNS.
My goal is simple:
- Write a non authoritative name server
- Database driven (replication a is powerful tool)
- Geo aware
- Can failover
- Load balance
- Written in PHP
- Modern UI to manage (no bootstrap-theme please)
- Just as fast as PDNS (with mysql backend)
- Run 4 name servers across globe on commodity hardware with no more than $10/month for all
- I'll call this Babaji DNS
PHP React Library
I decided to write my DNS server using phpreact. It is a wonderful event driven library written by Igor Wiedler and Chris Boden. To quote Igor PHP React is a created "by stealing features from other languages (NodeJS)"
Lately I am somewhat obsessed with phpreact. I have written a websocket chat application using it and I think it is just an awesome library that opens the door for TCP/UDP communication. There was already a DNS toolkit in React. However it was missing parsing/binary dumper for various record types to which I have contributed. I have learned a great deal about how DNS works at the binary level during this process.
Wait, but PHP is not fast
Well I wanted to test it myself before investing more into Babaji DNS. So I ran this experiment and result are very promising. Here is the summary of 100k queries:
|PDNS+Mysql||Run time (s): 140.788780
Queries per second: 709.417327
|PHP React Echo||Run time (s): 134.276466
Queries per second: 744.188486
|PHP Raw (without React)||Run time (s): 143.006352
Queries per second: 699.255653
|PHP PDNS+Mysql Alike||Run time (s): 147.829531
Queries per second: 676.448064
This is not a scientific test but based on simple stress testing a PHP + Mysql based DNS is just as fast as PDNS + Mysql. PHP PDNS+Mysql Alike did consume more CPU, but there is room for optimization in reactph-mysql library.
I have also created this video to show CPU & memory utilization during the test.