How to write a memcached server in less than 50 lines of code

Step 1: Figuring out how to build a web server

In every programming language, this usually ends up with a copy-paste of something. Let’s do the same with Python! Apparently, Python 3 now has a cool new feature called socketserver which we will use. After a quick Google search we find the following code:

import socketserverclass MyTCPHandler(socketserver.BaseRequestHandler):
The request handler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
def handle(self):
# self.request is the TCP socket connected to the client = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
# just send back the same data, but upper-cased
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C

Step 2: Reading about the protocol

The memcached is an extremely simple one. It is well documented in its wiki ( but looking over the internet we can find a straightforward example, right here:

set tutorialspoint 0 900 9
get tutorialspoint
VALUE tutorialspoint 0 9
  • flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved.
  • exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration.
  • bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached.
  • noreply (optional) − It is a parameter that informs the server not to send any reply.
  • value − It is the data that needs to be stored. The data needs to be passed on the new line after executing the command with the above options.

Step 3: Set up the environment

First of all, before we actually start to code, let’s get a working memcached server running, and a test.

brew install memcached
from pymemcache.client import base
client = base.Client(('localhost', 11211))
client.set('some_key', 'some value')

Step 4: Let’s code!

Here it is, the web server.

  1. Using a main loop in conjunction with a peek command — texual web servers also expect that you read all lines which are in the buffer for each connection being made, which is why a while True loop is necessary. However, we don’t want to keep the connection hanging if there’s nothing to read as part of a new sequence, so we can use peek to determine if there’s anything to read right now.


The purpose of this exercise is to show you the basics of web servers, so if you’ll think about writing one, you won’t be afraid to do so. The reality is, however, that in the modern world there is almost no reason to write a new web server, especially using a programming language like Python, mostly because there are great implementations out there for just about anything you need, HTTP is great for REST APIs, gRPC is great for RPC, and there’s many more queue systems, databases, cache servers, and just about anything you can think of.




An entrepreneur, and a web expert.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Let’s talk DSL with Kotlin

How I Stopped Learning to Code and Started Coding

The SQL I Love ❤. Efficient pagination of a table with 100M records

Using Terraform to enable automatic tuning mode in an individual Azure SQL Database

Release Notes — December 12th

Crucial Things for Successful Mobile Application Development

Yabai — MacOS Tile Window Manager

Awesome Speech Toolkit You Need To See

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ron Reiter

Ron Reiter

An entrepreneur, and a web expert.

More from Medium


LeetCode Database Solutions [Easy] 001 | Eugene Tsai

Lessons learned from building a high-volume public-facing website

Power Apps — With() Function/Streamline Your Formulas