Dzone Rc250 Memcached

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

BROUGHT TO YOU IN PARTNERSHIP WITH

250
GETTING STARTED WITH CONTENTS

Memcached
Introduction
What is Memcached?
Installing Memcached
Connecting To Your Memcached Server

BY JAMES SUGRUE
Common Commands...and more!

INTRODUCTION memcached p <TCP port> -U <UDP port> -u <username> -d


This memcached Refcard provides you with basic configuration
information for servers and client-side commands to use memcached Tip: Use memcached h for up-to-date documentation on the memcached
as a caching solution for your applications. version you have installed on your server

WHAT IS MEMCACHED? SERVER CONFIGURATION


memcached is defined as a high performance, distributed memory The configuration file for memcached is available at /etc/
sysconfig/memcached with defaults set as follows:
object caching system, generic in nature, but originally intended for
use in speeding up dynamic web applications by alleviating database PORT="11211"
load by memcached.org. USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
memcached works by caching data in RAM so that the number of OPTIONS=""
times an application has to read an external data source is reduced.
The in-memory key-value store can be used for data that would be
The PORT value specified in the above configuration file is the same
returned from database or API calls. On top of all of this, memcached
for TCP and UDP ports.
allows the cache load to be distributed across servers. However, as
the data is stored in memory by default, when the server is restarted, MAXCONN represents the maximum number of concurrent
memcached will re-initialize with an empty dataset. connections for memcached. Changing it to a higher value is
generally okay. Use the stats command to look for the listen_
disabled_num attribute. The value for this should be zero. If the
number is higher, it indicates that connections have been dropped,
and you should then consider increasing the MAXCONN value.

Running multiple instances of memcached on the same server is simply


a matter of changing the port that memcached is listening on.

CONNECTING TO YOUR MEMCACHED SERVER


The most straightforward way to connect to memcached and check
that it is running okay is to use telnet:
Diagram 1: Illustration of how a web server interacts with memcached and
a database
telnet <host> <port>

LIMITATIONS
There are four key limitations in memcached to keep in mind:

1. The key used is a string with a maximum length of 250 bytes


2. The value has a 1 megabyte size limit, which can be increased
with the I parameter
Amazon ElastiCache
3. No persistence Fully Managed Memcached
4. it is not highly available
by AWS
INSTALLING MEMCACHED
To install memcached on your Linux-based system, use either of the
following commands, depending on the OS that it is being installed to:

apt-get install memcached (Debian / Ubuntu)


yum install memcached (Red Hat / Fedora)

Once installed you can run memcached using the following command,
Try it today for Free
where d causes memcached to run as a daemon process:

DZONE.COM | DZONE, INC. VISIT DZONE.COM/REFCARDZ FOR MORE!


Fully managed
Memcached by AWS
Amazon ElastiCache is a fully-managed service for Redis and
Memcached that makes it easy to deploy, operate, and scale an
in-memory data store or cache in the cloud with microsecond
response times.

Fully managed Memcached


Hardened by Amazon
AWS integration and support

Try it today for free: aws.amazon.com/elasticache

Try it today for Free


3 GETTING STARTED WITH MEMCACHED

Commands can be run from this telnet session, as outlined in later


ATTRIBUTE DESCRIPTION
sections of this Refcard.
Name of the unique key that will be used to access
key
Note that your client-side code must be written to take advantage the data. Limited to 250 bytes.
of memcached; it is not a transparent implementation, and as such, A 32-bit space stored alongside the main value. Many
caching is not done automatically on the server-side. Libraries are flag sub libraries make use of this field, so in most cases it
available for the most popular programming languages so you can use should be avoided, by setting 0 as the flag value.
memcached in your application, as can be seen in the following table.
Expiration time in seconds of the data stored in
The commands outlined in the following sections will be exposed as cache. Use 0 to never expire. Using more than 30
functions in these libraries. exptime
days will be interpreted as a UNIX timestamp for an
exact date to expire.
LANGUAGE LIBRARY LINK The length in bytes that needs to be allocated for
bytes
Ruby Dalli github.com/petergoldstein/dalli this value.

Go gomemcache github.com/bradfitz/gomemcache Optional parameter that ensures no reply is sent


noreply
from server following command execution.
C/C++ libMemcached libmemcached.org/libMemcached.html
Python Pymemcache github.com/pinterest/pymemcache Value to be stored, which needs to be provided in a
value
new line following the set command with options.
Java Spymemcached github.com/couchbase/spymemcached
Node.js Memcached github.com/3rd-Eden/memcached A unique token number which can be retrieved
unique_cas_token
using the gets command.
php- github.com/php-memcached-dev/php-
PHP
memcached memcached
Table 2- Common command attributes used in storage and retrieval

Table 1- A selection of memcached client libraries


STORAGE COMMANDS
CLIENT CONFIGURATION The following commands allow you to add and replace values in your
memcached store:
The typical use case for applications using memcached client libraries
is as follows:
SET
Set is the main command that you will use when adding data to your
1. Web application requests keys using a client library. The library
data store, which simply assigns a value to a given key. This command
will calculate key hash values (consistent hashing algorithm
may overwrite existing data. The new items are at the top of the LRU.
is the most common example), and thus determine which
memcached server to send the request to
set key flags exptime bytes [noreply]
value
2. Client library sends parallel requests to all servers identified in
the previous step
ADD
3. Responses are sent to the client library from the server Stores the value provided only if it does not already exist. New items
are at the top of the LRU. If an item exists, and the add command fails,
Some client libraries will allow you to apply weight to preferred
the item is placed to the front of the LRU anyway.
servers, either by applying a weight value, or by adding that server
multiple times to your client configuration. An auto-discovery feature add key flags exptime bytes [noreply]
of a memcached client allows you to discover the nodes within the value

memcached cluster so their IPs do not need to be hardcoded within


the application. This is a very useful feature to enable dynamically REPLACE
supported topology changes. Stores the value provided only if it already exists.

replace key flags exptime bytes [noreply]


USING MEMCACHED WITH MYSQL value

Paired with MySQL, or another database, memcached can provide an


efficient way to access data. The typical usage patterns are: APPEND
Add this value after the last byte in an existing item. If the specified
Use MySQL for persistent data and memcached for transient key does not exist, the command will fail.
data only.
append key flags exptime bytes [noreply]
Clients read data from memcached, but if there is a cache miss, value

the client retrieves the data from the MySQL database and then
PREPEND
stores the returned value in the cache, for efficient access in
Prepend this value before the first byte in an existing item. If the speci-
subsequent executions.
fied key does not exist, the command will fail.
COMMON COMMANDS
prepend key flags exptime bytes [noreply]
The following attributes are used across a number of the value
following commands:

DZONE.COM | DZONE, INC. BROUGHT TO YOU IN PARTNERSHIP WITH


4 GETTING STARTED WITH MEMCACHED

CAS STATS ITEMS


Stored data but only if no one else has updated the data since you Returns information about items stored in memcached, broken down
read it last. Abbreviation for Check And Set (or Compare And by slab id. Returns values for items, age, and cache eviction.
Swap). This command is useful to resolving race conditions on
updating cache data. If the unique_cas_token for the item has stats items

changed since you last retrieved it (using gets) it will not be stored.
STATS SLABS
cas key flags exptime bytes unique_cas_token [noreply]
value Returns information about items stored in memcached, broken down
by slab id. Values returned in this command are more focused around
RETRIEVAL COMMANDS performance such as memory usage, allocated memory, and number
The following commands allow you to retrieve values from your of free chunks.
memcached store.
stats items
GET
Command for retrieving data for a given key, or set of keys. If a key STATS SIZES
does not exist, no value is returned. Illustrates how items would be distributed if broken into 32 byte
buckets instead of your current number of slabs, helping you to
get key [key2 .. keyn]
determine how efficient your slab sizing is.

GETS
stats sizes
Get command to be used with CAS, which returns the unique_cas_to-
ken with the item. ICommand for retrieving data for a given key, or set MEMCACHED TOOLS
of keys. If a key does not exist, no value is returned. Although some level of debugging is possible using the commands
listed above with the telnet client, there are a number of other tools
gets key [key2 .. keyn]
available to help monitor and troubleshoot your distributed mem-
cached setup. The following is a summary of the most popular tools:
DELETE
Removes an item from the cache if it exists. MEMCACHE-TOP
Link: https://code.google.com/archive/p/memcache-top/
delete key [noreply]

Command line tool that displays real-time stats from memcached,


INCR/DECR displaying the results in a view similar to top. This tool can only monitor
These commands will either increment or decrement the numeric a single memcached instance at a time. Results include I/O throughput
value of an existing key. These commands will only succeed where and evictions per second along with gets and sets per second.
the key is a numeric value.
PHPMEMCACHEDADMIN
incr key increment_value
Link: https://blog.elijaa.org/phpmemcachedadmin-download/
decr key decrement_value

FLUSH_ALL A standalone web-based application that displays memcached stats,


Invalidates all cache items in your memcached server. This command including details on server slabs, memory wasted, and item key values.
takes an optional time parameter, which allows you to instruct mem- You can execute commands on a memcached server from this interface.
cached to clear this data after a number of seconds. Allows the monitoring of multiple memcached instances.

Flush_all [time] MEMCACHED-MANAGER


Link: https://github.com/memcached-manager/memcached-manager

STATISTICS COMMANDS A single-page Sinatra memcached manager/admin that allows you to


The following commands allow you to inspect settings on the mem- read stats, view, edit, and create keys from memcached. Can easily be
cached server. Stats commands give some insights on how perfor- plugged into a Rails/Rack app and can work with multiple instances
mance of your setup could be tuned, as well as monitoring the overall of memcached.
usage of the cache.
REPLICATION
STATS Replication is not supported out-of-the-box in memcached, as it is
The basic stats command which returns values for server uptime, designed to be a transient cache store. However, replication can be
memcached version, number of connections, number of items, and achieved through some third-party solutions.
other traffic-related statistics.
REPCACHED
stats Link: https://github.com/ignacykasperowicz/repcached

DZONE.COM | DZONE, INC. BROUGHT TO YOU IN PARTNERSHIP WITH


5 GETTING STARTED WITH MEMCACHED

3. Retrieve values in bulk when possible rather than doing a


Repcached allows you to have a redundant memcached installation number of get calls in series.
running on another server. This is done by applying a patch that adds
4. Compressing large values will speed up your application as
a new x parameter, which accepts the IP address of the other server.
there is less data being transmitted over the wire, and less
Note that each server will need to have a link to the other.
memory being used when storing the value in memcached.
Most clients support enabling or disabling compression using
YRMCDS
either an item size threshold or even on a per-item basis.
Link: http://cybozu.github.io/yrmcds
5. When initializing memcached, you can prepopulate the cache
Developed mainly for session storage, yrmcds (Ymmts Replicatin
with keys, meaning your application will have less cache misses
MemcacheD for Sessions) is completely compatible with mem-
in its initial execution after a redeploy on the server.
cached, and as such, can be used as a drop-in replacement. To enable
replication, a virtual IP address needs to be specified along with the SECURITY
configuration of clustering software such as keepalived. The ports on which your memcached instances are running should not
be exposed to the internet as a rule. Since version1.4.3, SASL (Simple
TWEMPROXY Authentication and Security Layer) is included. Note that SASL restricts
Link: https://github.com/twitter/twemproxy access to the daemon but does not hide communications.

A lightweight proxy for memcached as well as Redis, built by Tip: Enable SASL on memcached by using the S flag on startup. Following
Twitter to reduce the number of open connections to cache servers. this, all commands require that authentication be successful before they are
Requests to a pool of memcached instances go through this proxy. issued on a connection.
Using two twemproxy instances, traffic can be directed to both a
pool of slaves, as well as a pool of master instances. To increase your memcached security beyond SASL, the following
should be considered:
PERFORMANCE BEST PRACTICES
Never run memcached as root. If someone did gain access to
Following these best practices will help you get the most
memcached and there was a security vulnerability, then they
from memcached:
would be able to compromise your machine and network.
1. From stats, take note of the evictions count, which shows
Use a firewall to limit which connections are open to the
the number of non-expired items that were removed from the
outside world. If you are running memcached on a single
cache to make space for new items. If this number is high, it
server, then you can bind the instance to localhost using the
indicates that the memory allocated for items storage is too low.
l parameter, restricting cache access to just that machine.
2. The default number of threads is 4, and for most cases should
not be changed. Using a single thread will be too slow, and
more than 8 threads can lead to high lock contention.

A B O U T T H E AU T H O R
JAMES SUGRUE is Chief Architect at Over-C, building mobile applications and services for managing compliance, using
NFC and Bluetooth sensors for proof of presence. James is an expert in Java and Swift, building everything from desktop
applications to high performance servers and mobile applications in Android and iOS.

James has been a Zone Leader at DZone since 2008, and has written many Refcardz, as well as a book on Backbone.js.

BROUGHT TO YOU IN PARTNERSHIP WITH

DZone communities deliver over 6 million pages each month to more than
3.3 million software developers, architects and decision makers. DZone offers
something for everyone, including news, tutorials, cheat sheets, research guides,
DZONE, INC. REFCARDZ FEEDBACK
feature articles, source code and more. "DZone is a developer's dream," says PC 150 PRESTON EXECUTIVE DR. WELCOME
[email protected]
Magazine. CARY, NC 27513
SPONSORSHIP
Copyright 2017 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval
888.678.0399 OPPORTUNITIES
system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior
919.678.0300 [email protected]
written permission of the publisher.

You might also like