nscd で名前解決のコストを減らす
hostsに書いたとしても、名前解決をするコストはかなり大きい。
名前解決のコストを単純に減らしたいだけなら nscd 使えばいいです。
nscd が起動していれば /etc/hosts に書いてあるホスト名の解決も 2回目以降は非常に速くなります。DNS サーバへのリクエストも同様にキャッシュされるので(というかこっちがメインの使い方だけど)、非常に短い TTL で運用しているサーバがあるなら別ですが、そうでないなら DNS サーバの負荷も減ります。negative cache もしてくれるので NXDOMAIN を二回以上問い合わせないし。local に DNS cache を持つような感じかな。
# Ubuntu 7.04 Feisty Fawn です。 % uname -srm Linux 2.6.20-16-generic i686 % sudo apt-get install nscd % ps -ef | grep nscd root 28753 1 0 17:23 ? 00:00:00 /usr/sbin/nscd
で、同じようなベンチマークを実行すると、
Benchmark: timing 50000 iterations of hosts_lookup, named_lookup, no_lookup... hosts_lookup: 0 wallclock secs ( 0.08 usr + 0.07 sys = 0.15 CPU) @ 333333.33/s (n=50000) (warning: too few iterations for a reliable count) named_lookup: 0 wallclock secs ( 0.06 usr + 0.09 sys = 0.15 CPU) @ 333333.33/s (n=50000) (warning: too few iterations for a reliable count) no_lookup: 1 wallclock secs ( 0.08 usr + 0.07 sys = 0.15 CPU) @ 333333.33/s (n=50000) (warning: too few iterations for a reliable count)
ほとんど差がなくなる。nscd 使ってると名前解決の障害点が一つ増えることになるので全ての状況でお勧めするわけではないのですが、DNS Server も運用しているのなら検討する価値はあるかなあ。