Connect to Memcached Server Using only Terminal

Recently, I encountered an intriguing code challenge: build a CLI to connect to Memcached.

My initial thought was to use telnet to interact with the Memcached server. However, after spending some time reading the Memcached protocol documentation I decided to use Netcat instead.

This blog post shares my experience and explains how to use netcat to connect to Memcached, illustrating some basic commands.

Prerequisites

Ensure the following before starting:

  • Memcached
  • Netcat

Protocol Explained

Memcached uses a simple text-based protocol to store, retrieve, and manage data. The basic operations include commands like set, get, delete, and flush_all.

  1. Commands: These are the operations you want to perform, such as get to retrieve data or set to store data.
  2. Keys: Memcached uses keys to identify the data.
  3. Flags: These are optional numerical values stored with the data. They can be used to specify how the data should be interpreted or managed but are often set to 0.
  4. Expiration Time: This specifies the lifespan of the cached data in seconds.
  5. Data Length: When storing data, you must specify the length of the data in bytes.
  6. Data Block: This is the actual data being stored or retrieved. When using the set command, the data is sent after the command and key information, separated by a newline character.

Example for set command:

set <key> <flags> <exptime> <bytes>\r\n
<data block>\r\n

# for example
set foo 0 300 3
bar
  • foo: Key
  • 0: Flags
  • 300: Expiration time in seconds
  • 3: Number of bytes in the data block
  • bar: Data block

The server responds with STORED if the operation was successful.
Similarly, commands like get and delete have their own structures:

  • Get: get <key>\r\n
  • Delete: delete <key>\r\n

Basic Memcached Commands with Netcat

Retriving an Entry

To retrieve data stored in Memcached, use the following command. Replace foo with your desired key

echo 'get foo' | nc localhost 11211

This command sends a request to the Memcached server to get the value associated with the key foo.

Reseting the Cache

To clear all data from the cache, use the flush_all command.

echo 'flush_all' | nc localhost 11211

This command tells the Memcached server to flush all cached data, essentially resetting the cache.

Setting a new Entry

To store data in Memcached, use the set command.

# sets on key foo the value bar for 300 seconds
echo -e 'set foo 0 300 3\r\nbar\r' | nc localhost 11211
  • foo: The key.
  • 0: Flags (not used here, set to 0).
  • 300: Expiration time in seconds.
  • 3: Number of bytes in the value.
  • bar: The value.The \r\n sequence marks the end of the command line, and \r is used to finish the data line.

Removing an Entry

To delete data associated with a key, use the delete command.

echo 'delete bar' | nc localhost 11211

Command Summary

# Set a value
echo -e 'set foo 0 300 3\r\nbar\r' | nc localhost 11211

# Get the value
echo 'get foo' | nc localhost 11211

# Flush all data
echo 'flush_all' | nc localhost 11211

# Attempt to get the value again (should return nothing as cache is flushed)
echo 'get foo' | nc localhost 11211

# Delete the key (this will have no effect if the key doesn't exist)
echo 'delete bar' | nc localhost 11211

Conclusion

Using netcat to interact with Memcached provides a simple, low-level interface for testing and debugging.

This method is especially useful when you need to manually issue commands or quickly check the contents of the cache.

Feel free to try out different Memcached commands and explore the capabilities of this powerful caching system!

References

Build a CLI to connect to Memcached https://codingchallenges.fyi/challenges/challenge-memcached-client

Memcached protocol: https://github.com/memcached/memcached/blob/master/doc/protocol.txt

Love Discord?