This is a well studied problem. Dan Bernstein famously used constant databases with guaranteed performance in tinydns en qmail:
CDB and deratives use a external file for the k-v content, but there is no need to do this if the number a entries is very small.
Some papers that describe the algorithms en the math: