Memcached 101: Basic Operations and Commands

Memcached 101: Basic Operations and Commands
Memcached 101: Basic Operations and Commands

Introduction

Memcached is a free and open-source, high-performance, distributed memory caching system. It’s primarily used to speed up dynamic web applications by offloading database load. It stores data as key-value pairs in the server's main memory (RAM), allowing for extremely fast lookups.

This article serves as an introduction to the core concepts and fundamental commands you need to start using Memcached.

 

Prerequisites

To follow this tutorial, you will need:
Ubuntu 22.04, 20.04, 18.04 or older server with a sudo non-root user. 
Installed Memcached service.

 

Basic Operations and Commands

Memcached works on a simple command-line interface (CLI) and uses a set of straightforward commands to interact with the stored data. You can connect to a Memcached instance using a telnet client on the server's IP address and the default port, which is usually 11211.

 

The Full documentation can be found in Protocol Document: Protocol.txt

Retrieval command: get
echo "get <key>" | nc localhost 11211

get <key>*\r\n

The get command is the main read operation. It efficiently retrieves the value associated with one or more keys.

Store command: set
echo -e "set <key> <flags> <exptime> <bytes>\n<the data to store>" | nc localhost 11211

set <key> <flags> <exptime> <bytes>\r\n
<the data to store>\r\n

The set command is the most common and powerful storage command. It unconditionally stores a piece of data. If the key already exists, set will simply overwrite the old value.

Store command: append
echo -e "append <key> <flags> <exptime> <bytes>\n<the data to store>" | nc localhost 11211

Add this data after the last byte in an existing item. This does not allow you to extend past the item limit. Useful for managing lists.

Maintenance & Configuration

These commands are essential for diagnostics, monitoring, and administrative tasks.

Inspecting Running Configuration: Output current settings
echo "stats settings" | nc localhost 11211

Basic commands to get Memcached service current settings.

Inspecting Running Configuration: Output Usage Statistics
echo "stats" | nc localhost 11211

Basic commands to output Memcached service usage statistics.

Output an information about cached items
echo "stats items" | nc localhost 11211

Returns some information, broken down by slab, about items stored in memcached.

Clear / Invalidate all existing cache items
echo "flush_all" | nc localhost 11211

Invalidate all existing cache items. Optionally takes a parameter, which means to invalidate all items after N seconds have passed.

This command does not pause the server, as it returns immediately. It does not free up or flush memory at all, it just causes all items to expire.

Output all keys from Memached service
memcdump --servers=localhost | less

Command memcdump (sometimes memdump) from libmemcached-tools package, can be used to dump all keys from Memached service

Summary

Memcached is an in-memory key-value store that excels at providing fast data access, making it indispensable for modern high-traffic applications.

Its core functionality revolves around a few simple commands: set for storing, get for retrieval, and delete for removal. Conditional commands like add and replace offer finer control over data persistence. Understanding these basic operations is the first step toward leveraging Memcached to drastically improve your application's performance and scalability.