Putting a cache in front of a database is simple on one server. On several servers, three problems appear together. If every node caches on its own, the same value is loaded and stored many times. If a popular key is missing, hundreds of requests may ask the database for it at the same moment. And when you add or remove a node, you do not want every key to change place.
groupcache is a well-known Go design that answers all three, and I wanted to understand it by writing my own version. distcache is a groupcache-style distributed read-through cache in Go, using only the standard library. Every key has exactly one owner node, and other nodes ask the owner over HTTP instead of hitting your database.
What it does
The project is four small packages:
consistenthash : a hash ring with virtual nodes that picks a key's owner.
singleflight : collapses concurrent loads of the same key into one.
lru : a size-bounded least-recently-used cache.
distcache : ties them together.…