02 August 2016 11:01AM

Memcache was originally designed as a fast in-memory caching server and has gained wide adoption in this role. Caching a response from an API or webserver is easily implemented using memcache in most systems and can lead to orders of magnitude higher throughput if the caching strategy is correctly designed. Client sharding solutions, where a specific cache server in a cluster is selected based upon the key being stored, have allowed a very scalable solution for caching and are widely available for all modern languages.

However, for some years now, memcache's sphere of use has extended to session management for a number of reasons - having been designed as a caching solution, it is extremely fast, and the built-in timeout expiry lends itself naturally to session management. In the best case, a session can be authenticated and load session state directly from memcache without involving the database at all, with sessions automatically expiring using the built-in cache expiry in a timely manner.

However, from a scalability and high availability point of view, client sharding solutions rarely use any concept of consistent hashing and the loss of a memcache server will always result in the loss of keys - sessions, in this use case - on that server, and likely either many or all keys on other servers depending on sophistication of the server selection algorithm in a scaled (sharding) scenario. A need has arisen, given memcache's adoption as a session store, for a highly available solution in which sessions can be maintained in case of the loss one or more memcache instances.

Memcacheha is a golang client library for memcache that has been designed as drop in replacement for the most commonly implemented memcache client library. In essence, instead of sharding keys across multiple servers, it concurrently replicates keys to all servers during writes, and selects a subset of servers to query during reads. In the case where one of the servers being queried does not contain the key, the item key and value are concurrently re-written to that server. Memcacheha represents an alternative client that attempts to deliver best-effort high availability in the event of the loss or disconnection of one memcache node.

Nodes are configured using a plugin-like discovery system and can be configured statically, or dynamically through AWS (memcacheha works natively with ElastiCache) and are constantly monitored for health, the concept being that the system using the library should be unaware of the failure if the data exists on any of the other nodes. Similarly, sources are queried periodically for new nodes joining the cluster and become synchronised during reads as above. This allows for a much more resilient architecture where memcache is being used for data that is evanescent but nonetheless non-trivial, e.g. user/client sessions and the like.


Memcacheha is currently in late beta and supports the most commonly used operations available in memcache. A golang package implementing memcacheha is available here:


https://github.com/apitalent/memcacheha