Memcached is all the rage these days with the high scalability guys. Legends have it that as soon as you slap a couple of memcached in between your web app and your database server, all of your scalability issues go away. Even if that's glossing over of details, memcached certainly offers advantages over accessing the discs all the time, and we want it in PLT.
Let's try to build a memcached client.
Steps to Build a Client (or a Server)
The steps to build either a client or a server is pretty straight forward (independent of the complexity of the protocol):
- study the protocol
- write the "output functions" that sends the info to the other party
- write the "input functions" that parses the input from the other party
- combine the input & output functions, and manage the states of the connection (if applicable)
Allright - let's get started.
Study the Protocol
The key to build a network interface is to understand the protocols. It is best if the protocol is open and published, so you have something written that you can study (hopefully in a language you understand). The next best option is to reverse engineer from an open source implementation, and the worst is to reverse engineer from sniffing the network interactions between the clients and the server. I am glad such memcached's protocol is already published so we do not have to go down the other routes.
Please refer to the memcached protocols throughout this process as the official reference. We'll sprinkle information from the official doc in the posts as necessary.
First a quick overview of the memcached protocol.
- request/response protocol - there are no unexpected responses that the client has to handle
- multiple requests and responses within a single session
- *mostly* line-based protocol - the commands are single lines that are terminated by CRLF; the data is also terminated by CRLF, but it is actually determined by length parameter
- can store, retrieve, and delete the content (as well as flush)
- there are also statistics that can be retrieved, but the protocol appears in flux for the stats (so it'll be out of scope for now)
- the protocol itself is *stateless* as each of the requests and responses are independent from each other (each request is atomic)
Storage API
Memcached has multiple storage commands for different situations: