Networking, SASL, Limits, Monitoring and Managed Caches

Article · 7:00

Why private networking is the entire security model, SASL and TLS where supported, the UDP amplification history, connection limits, monitoring, and managed cache trade-offs.

Memcached has almost no security model, and the correct response is to put it on a private network. There is no authentication by default, no authorisation, no users, no roles and no per-key permissions. Anyone who can reach port 11211 can read every cached value, write any value, and run flush_all. There is no audit trail of who did.

This has a well-documented history. In 2018, Memcached's UDP support was used for one of the largest reflection and amplification attacks recorded: a small spoofed UDP request produced an enormous response sent to the victim, with an amplification factor in the tens of thousands, because tens of thousands of Memcached instances were exposed to the internet with UDP enabled. Memcached now ships with UDP disabled by default and binds to localhost in most packages - and the lesson is the broader one: an exposed Memcached is not only your problem, it is a weapon pointed at someone else.

So the security configuration is short and non-negotiable: bind to a private interface with -l, never 0.0.0.0; a firewall or security group that permits only your application hosts; UDP off (-U 0); and -vv off in production.

SASL provides authentication where your build supports it (--enable-sasl, started with -S). It uses the binary protocol, requires client support, and is genuinely useful in a shared or multi-tenant environment. It is not a substitute for network isolation - it is defence in depth on top of it.

TLS is supported in Memcached 1.6+ when built with it (-Z with certificate options) and in managed offerings. It costs throughput, and for a cache on a private network it is often unnecessary; for a cache crossing any boundary you do not fully control, it is not optional.

Connection limits matter more than they look. -c caps simultaneous connections and the default is 1024. Exceeding it means clients are refused, which is visible as listen_disabled_num climbing - a metric worth alerting on, because the symptom is application errors that look like a network problem. The arithmetic to do: application instances multiplied by pool size per instance against -c. Forty instances with a pool of 50 is 2,000 connections against a default of 1,024, which fails at the moment you scale out.

Monitoring. The Memcached-specific set: hit ratio, evictions, evicted_unfetched, evicted_time (the effective cache lifetime), curr_connections against the limit, listen_disabled_num, bytes against limit_maxbytes, and - per slab class - the item counts, so that a change in size distribution is visible. Plus uptime, which resetting means a restart you may not have noticed, and therefore an unexplained cold cache.

Managed caches - ElastiCache for Memcached, Memorystore, and others - handle provisioning, patching, monitoring and node replacement, and add auto-discovery so the client learns the node list rather than having it configured. The trade-offs to state: cost against self-managed, less control over flags and versions, provider-specific behaviours, and the fact that node replacement is still a cold cache event - a managed cache does not make your cold-start problem go away, it just performs the replacement more smoothly. The cost model is straightforward - node hours plus data transfer - which makes it one of the easier capacity decisions in this programme.

Laboratory exercise 6 - make it defensible.

Configure a hardened instance: bound to a private interface, UDP disabled, with a firewall rule permitting only your application host.

Then demonstrate what the default costs. In an isolated lab network, start an instance bound to 0.0.0.0 with no authentication and, from another machine, run stats, read a cached value, write a value, and run flush_all. Then enable UDP and measure the amplification factor of a single stats request. This is the 2018 attack, reproduced at small scale, and it should be uncomfortable.

If your build supports it, enable SASL and demonstrate authenticated and refused access.

Do the connection arithmetic for your capstone's expected instance count and pool size, set -c accordingly, then deliberately exceed it and capture listen_disabled_num and the client-side symptom.

Configure monitoring with every metric above and a threshold for each, including an alert on uptime resetting.

Then produce the managed-against-self-managed comparison for the capstone: cost at your sizing, what you lose in control, and - specifically - what node replacement does to your hit ratio under each.

Deliverable: the hardened configuration, the unauthenticated-access demonstration with the amplification measurement, the SASL evidence if available, the connection arithmetic with the limit exceeded and its symptom, the alert definitions with thresholds, the managed comparison, and a deployment, sizing and cost checklist.

# ==================================================================
# THERE IS ALMOST NO SECURITY MODEL. Private networking IS the model.
# ==================================================================
# No authentication by default. No authorisation. No users, no roles,
# no per-key permissions. Anyone who can reach 11211 can:
#   * read EVERY cached value        (stats cachedump, or just get)
#   * write ANY value                (set)
#   * run flush_all
# and there is NO AUDIT TRAIL of who did.

#   memcached -m 4096 \
#     -l 10.0.1.5 \        # PRIVATE interface. NEVER 0.0.0.0.
#     -p 11211 \
#     -U 0 \               # UDP OFF. See the history below.
#     -c 2048 \
#     -t 4 \
#     -o modern
#     # and NO -vv in production (it logs every command)

# ==================================================================
# THE 2018 UDP AMPLIFICATION ATTACK. Know this history.
# ==================================================================
# Memcached's UDP support was used for one of the LARGEST REFLECTION
# AND AMPLIFICATION ATTACKS ON RECORD:
#
#   a small SPOOFED UDP request ("stats", ~15 bytes)
#   -> an enormous response, sent TO THE SPOOFED VICTIM
#   -> amplification factor in the TENS OF THOUSANDS
#   -> because tens of thousands of instances were exposed to the
#      internet with UDP enabled
#
# UDP is now DISABLED BY DEFAULT and most packages bind to localhost.
# The broader lesson is the one to take:
#   AN EXPOSED MEMCACHED IS NOT ONLY YOUR PROBLEM - IT IS A WEAPON
#   POINTED AT SOMEONE ELSE.
#
# Reproduce it at small scale in an ISOLATED LAB NETWORK: measure the
# bytes sent against the bytes received for one UDP `stats`. The
# ratio is the lesson.

# Verify your exposure. On every instance you inherit:
#   ss -lntp | grep 11211
#   ss -lnup | grep 11211      # <- UDP. Should be EMPTY.
#   nmap -p 11211 -sU -sS <host>

# ==================================================================
# SASL, where your build supports it. DEFENCE IN DEPTH, not a
# substitute for network isolation.
# ==================================================================
#   built with --enable-sasl, then:
#   memcached -S -l 10.0.1.5 -m 4096
#
#   saslpasswd2 -a memcached -c cacheuser
#
#   Uses the BINARY protocol and requires client support:
#     pymemcache: Client(..., username='cacheuser', password='...')
#     php:        Memcached::OPT_BINARY_PROTOCOL + setSaslAuthData
#
#   Genuinely useful in a shared or multi-tenant environment. Still
#   put it on a private network.

# ==================================================================
# TLS, Memcached 1.6+ built with it
# ==================================================================
#   memcached -Z -o ssl_chain_cert=/etc/ssl/mc.crt,ssl_key=/etc/ssl/mc.key
#   It costs throughput. On a private network often unnecessary;
#   crossing any boundary you do not fully control, not optional.

# ==================================================================
# CONNECTION LIMITS. Do the arithmetic.
# ==================================================================
stats settings
# STAT maxconns 1024          <- the default
stats
# STAT curr_connections 412
# STAT total_connections 48201337
# STAT listen_disabled_num 0  <- > 0 means CLIENTS WERE REFUSED
#
# THE ARITHMETIC NOBODY DOES:
#   app instances x pool size per instance  vs  -c
#   40 instances x 50 connections = 2,000   vs  1,024   -> FAILS
#
# And it fails AT THE MOMENT YOU SCALE OUT, which is the moment you
# least want a new failure mode. The client-side symptom looks like a
# network problem, not a limit - which is why listen_disabled_num
# belongs on the alert list.
#   memcached -c 4096

# ==================================================================
# MONITORING: the Memcached-specific set.
# ==================================================================
#   echo "stats" | nc 10.0.1.5 11211
#   memcached-tool 10.0.1.5:11211 display      # per-slab, readable
#   memcached-tool 10.0.1.5:11211 stats

#   get_hits/(get_hits+get_misses)   falling     cache less useful
#   evictions                        rising      undersized or churn
#   evicted_unfetched / evictions    high        CACHING THE WRONG
#                                                THINGS
#   evicted_time                     < your TTL  YOUR TTLs ARE
#                                                FICTION. The key one.
#   curr_connections / maxconns      > 80%       do the arithmetic
#   listen_disabled_num              > 0         CLIENTS REFUSED
#   bytes / limit_maxbytes           ~100%       always true at steady
#                                                state; not an alarm
#                                                on its own
#   items:N:evicted per slab class   a shift     SIZE DISTRIBUTION
#                                                CHANGED after a deploy
#   slab_reassign_rescues            rising      automove is working
#   uptime                           RESET       A RESTART YOU MAY NOT
#                                                HAVE NOTICED - and
#                                                therefore an
#                                                unexplained cold
#                                                cache. Alert on it.
#   cmd_get / cmd_set ratio          a shift     usage pattern change

# ==================================================================
# MANAGED CACHES: the trade-off, stated.
# ==================================================================
#   ElastiCache for Memcached / Memorystore / others
#
#   YOU GET:  provisioning, patching, monitoring, node replacement,
#             and AUTO-DISCOVERY - the client learns the node list
#             rather than having it configured, which removes a
#             deployment coupling that is genuinely annoying
#
#   YOU LOSE: cost vs self-managed, control over flags and versions,
#             and you inherit provider-specific behaviours
#
#   AND NOTE: NODE REPLACEMENT IS STILL A COLD CACHE EVENT. A managed
#             cache does NOT make your cold-start problem go away -
#             it performs the replacement more smoothly. Module 5's
#             capacity statement is still required.
#
#   COST MODEL - one of the simplest in this programme:
#     node hours x node count
#     + data transfer (usually free within an AZ, charged across)
#     + nothing else
#
#     6 x cache.m7g.large, 730 h    = $___
#     self-managed equivalent        = $___ + YOUR OPERATIONS TIME
#
#   The honest comparison includes the operations time, and for most
#   teams that is what decides it.

# ==================================================================
# AND THE RULE THAT OUTRANKS EVERYTHING ELSE HERE
# ==================================================================
# NEVER STORE ANYTHING IN MEMCACHED THAT YOU CANNOT REBUILD.
#   no sessions as the only copy    (a restart logs everyone out)
#   no queues                       (no persistence, no acknowledgement)
#   no counters that must be exact  (evicted without warning)
#   no "temporary" data that turns   (it will, and then a restart is
#      out to be the only copy       an incident)
#
# Not a style preference. The server will discard any item, at any
# time, to make room, and tell nobody.

Discussion

No questions on this topic yet. Sign in to ask the first one.