Learn Memcached - Top and Common Commands to Get Started
Key Memcached Commands for New Users
Contents
About Memcached
Set Up
Application
Access
User Management
Basic Operations
Data Operations
Maintenance
Advanced
Security
Monitoring and Performance
About Memcached
Overview
Memcached is an open-source, high-performance, distributed memory caching system
Designed to speed up dynamic web applications by alleviating database load
Stores data in RAM for quick access
Commonly used for caching database query results, session data, and other transient information
Key Features
In-Memory Storage
Provides fast read and write operations by storing data in memory
Distributed Architecture
Supports horizontal scaling by distributing data across multiple nodes
Simple Design
Easy to install and use, with a straightforward set of commands for storing and retrieving data
Highly Efficient
Minimal resource usage with a focus on performance and scalability
Language Support
Compatible with various programming languages including Python, PHP, Java, C#, and more
Eviction Policy
Uses the Least Recently Used (LRU) eviction policy to manage memory efficiently
Comparison to Alternatives
MySQL
A relational database better suited for structured data and complex queries
Memcached is used for caching to improve MySQL performance
SQLite
Lightweight and serverless, ideal for embedded systems and small applications
Memcached provides faster, in-memory storage for transient data
PostgreSQL
An advanced RDBMS with strong ACID compliance and support for complex queries
Memcached is often used alongside PostgreSQL for caching to boost performance
Redis
Memcached is simpler and optimized for caching
Redis offers more features and flexibility
Set-Up
Check Installation and Version
Check if Memcached is installed and its version:
$ memcached -h | head -n 1
Output:
memcached 1.6.14
Installation
$ sudo apt update
$ sudo apt install memcached
Uninstallation
$ sudo apt remove memcached
$ sudo apt autoremove
Application
Start-Up
$ sudo service memcached start
Check Status
$ sudo service memcached status
Check Port
$ sudo netstat -tuln | grep 11211
Power-Off
$ sudo service memcached stop
Access
Single Login into Database
To access the Memcached command line interface, use telnet:
$ telnet localhost 11211
Persist Login into Database
Memcached does not require persistent login management as it is a simple, in-memory key-value store
User Management
Memcached does not support creating users and managing privileges
It is designed to be a simple, in-memory key-value store
Basic Operations
List Databases
Memcached is a single in-memory store
There are no multiple stores - just one store
Therefore, it does not have the concept of listing multiple databases or stores
Create Database
Memcached does not support creating databases
Data is stored in memory
Clear Database Data
To delete all data in Memcached:
$ echo "flush_all" | nc localhost 11211
Delete Database
Memcached does not support deleting databases, since there are no databases
Data Operations
Create Table
Memcached is a key-value store and does not support tables
However, you can simulate tables using keys and values
Syntax Explained
$ set <key> <flags> <expiry-time> <bytes>
This command sets a key “dept:1” with the value of “Finance”
This key expires is 3600 seconds (1 hour)
To retrieve this key later on, you use the “dept:1” key
The prefix of dept serves, conceptually, like a database table
The value of 1 corresponds to the unique primary key for this database record
The example below showcases a simple table/record, where we simply store a list of departments
Example:
Use keys to store department information:
Use keys to store department information:
echo "set dept:1 0 3600 Finance" | nc localhost 11211
echo "set dept:2 0 3600 Human Resources" | nc localhost 11211
echo "set dept:3 0 3600 Marketing" | nc localhost 11211
Example:
Store employee records:
In this second example, we take this key-concept further by creating a complex key system - something completely invented by us that allows us to define a key as (Table Name):(Unique Record Key):(Column Name)
When we want to retrieve an entire object, we look for all column names that have the same table name and unique record name.
As long as we are consistent in this approach, our data is soundly stored and efficiently retrieved
echo "set emp:1:name 0 3600 John Doe" | nc localhost 11211
echo "set emp:1:email 0 3600 john.doe@example.com" | nc localhost 11211
echo "set emp:1:dept_id 0 3600 1" | nc localhost 11211
echo "set emp:2:name 0 3600 Jane Smith" | nc localhost 11211
echo "set emp:2:email 0 3600 jane.smith@example.com" | nc localhost 11211
echo "set emp:2:dept_id 0 3600 2" | nc localhost 11211
echo "set emp:3:name 0 3600 Michael Johnson" | nc localhost 11211
echo "set emp:3:email 0 3600 michael.johnson@example.com" | nc localhost 11211
echo "set emp:3:dept_id 0 3600 3" | nc localhost 11211
Insert Data
$ echo "set key 0 3600 value" | nc localhost 11211
Example:
Add a new employee:
echo "set emp:4:name 0 3600 Emily Davis" | nc localhost 11211
echo "set emp:4:email 0 3600 emily.davis@example.com" | nc localhost 11211
echo "set emp:4:dept_id 0 3600 1" | nc localhost 11211
Update Data
$ echo "set key 0 3600 new_value" | nc localhost 11211
Example:
Update an employee's email:
echo "set emp:1:email 0 3600 john.doe.new@example.com" | nc localhost 11211
Delete Data
$ echo "delete key" | nc localhost 11211
Example:
Delete an employee:
echo "delete emp:3:name" | nc localhost 11211
echo "delete emp:3:email" | nc localhost 11211
echo "delete emp:3:dept_id" | nc localhost 11211
Query Data
$ echo "get key" | nc localhost 11211
Example:
Retrieve employee details:
echo "get emp:1:name" | nc localhost 11211
echo "get emp:1:email" | nc localhost 11211
echo "get emp:1:dept_id" | nc localhost 11211
Maintenance
Backup Database
Memcached does not support native backup operations as it is an in-memory store
Given that it is in-memory, records expire after a specified amount of time
Memcached, therefore, is only intended as a short-term data storage solution, commonly as a cache
To back up data, you need to implement custom logic in your application
Restore Database
To restore data, you need to write custom logic in your application to repopulate Memcached with previous data
Optimize Database
Memcached manages memory efficiently on its own
You can get statistics using the stats command:
$ echo "stats" | nc localhost 11211
Check Logs
$ tail -f /var/log/memcached.log