Skip to main content
Swytch Documentation
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Memcached Compatibility

Swytch provides a memcached-compatible server that can serve as a drop-in replacement for memcached in most use cases. This page documents the supported features and known differences.

Quick Start

# Start with defaults (64MB memory, port 11211)
swytch memcached

# Production setup with 4GB memory
swytch memcached -m 4096 -t 8 -c 10000

# Listen on specific address
swytch memcached -l 127.0.0.1 -p 11211

# Unix socket mode
swytch memcached -s /tmp/memcached.sock

Supported Commands

Storage Commands

CommandSyntaxDescription
setset <key> <flags> <exptime> <bytes> [noreply]\r\n<data>\r\nStore a key unconditionally
addadd <key> <flags> <exptime> <bytes> [noreply]\r\n<data>\r\nStore only if key doesn’t exist
replacereplace <key> <flags> <exptime> <bytes> [noreply]\r\n<data>\r\nStore only if key exists
appendappend <key> <flags> <exptime> <bytes> [noreply]\r\n<data>\r\nAppend data to existing value
prependprepend <key> <flags> <exptime> <bytes> [noreply]\r\n<data>\r\nPrepend data to existing value
cascas <key> <flags> <exptime> <bytes> <cas> [noreply]\r\n<data>\r\nCheck-and-set (compare-and-swap)

Retrieval Commands

CommandSyntaxDescription
getget <key> [<key> ...]Retrieve one or more keys
getsgets <key> [<key> ...]Retrieve with CAS token
gatgat <exptime> <key> [<key> ...]Get and touch (update expiration)
gatsgats <exptime> <key> [<key> ...]Get and touch with CAS token

Deletion Commands

CommandSyntaxDescription
deletedelete <key> [noreply]Delete a key
flush_allflush_all [<exptime>] [noreply]Invalidate all items

Arithmetic Commands

CommandSyntaxDescription
incrincr <key> <value> [noreply]Increment numeric value
decrdecr <key> <value> [noreply]Decrement numeric value (floor at 0)

TTL Commands

CommandSyntaxDescription
touchtouch <key> <exptime> [noreply]Update expiration time

Administrative Commands

CommandSyntaxDescription
statsstatsGet server statistics
stats resetstats resetReset statistics counters
versionversionGet server version
quitquitClose connection
shutdownshutdownShutdown server (requires -A flag)

Command-Line Options

Standard Memcached Options

OptionDefaultDescription
-p <port>11211TCP port to listen on (0 = off)
-l <addr>127.0.0.1Interface to listen on
-s <path>-Unix socket path (disables TCP)
-a <perms>0700Unix socket permissions (octal)
-m <MB>64Memory limit in megabytes
-c <num>1024Max simultaneous connections
-t <num>4Sets GOMAXPROCS
-b <num>1024Listen backlog queue limit
-I <size>1mMax item size (e.g., 1k, 1m, 1G)
-C-Disable CAS (saves 8 bytes per item)
-F-Disable flush_all command
-A-Enable shutdown command
-v-Verbose output (-v, -vv, -vvv)
-P <file>-Write PID to file
-k-Lock memory pages (mlockall)
-r-Enable core dumps (setrlimit)
-h-Show help
-V-Show version

Swytch Extensions

OptionDefaultDescription
--dashboard-Enable web dashboard
--dashboard-addr:8080Dashboard HTTP address
--metrics-port-Enable Prometheus metrics on this port
--pprof-Enable pprof HTTP server on :6060

Statistics

The stats command returns standard memcached statistics:

STAT pid <process_id>
STAT uptime <seconds>
STAT time <unix_timestamp>
STAT version <version_string>
STAT curr_connections <count>
STAT total_connections <count>
STAT cmd_get <count>
STAT cmd_set <count>
STAT cmd_flush <count>
STAT cmd_touch <count>
STAT get_hits <count>
STAT get_misses <count>
STAT get_expired <count>
STAT get_flushed <count>
STAT delete_hits <count>
STAT delete_misses <count>
STAT incr_hits <count>
STAT incr_misses <count>
STAT decr_hits <count>
STAT decr_misses <count>
STAT cas_hits <count>
STAT cas_misses <count>
STAT cas_badval <count>
STAT touch_hits <count>
STAT touch_misses <count>
STAT evictions <count>
STAT bytes_read <bytes>
STAT bytes_written <bytes>
STAT limit_maxbytes <bytes>
STAT bytes <current_bytes>
STAT curr_items <count>
STAT total_items <count>
STAT threads <count>
END

Unsupported Features

The following memcached features are not currently supported:

Meta Commands (memcached 1.6+)

The newer meta text protocol commands are not implemented:

CommandNameDescription
mgMetaGetGet with optional metadata flags (TTL, hit status, etc.)
msMetaSetSet with metadata control (stale marking, CAS override)
mdMetaDeleteDelete with invalidation/stale marking options
mnMetaNoopNo-op for pipeline completion signaling
maMetaArithmeticIncrement/decrement with extended options
meMetaDebugDebug/examine item metadata

Extstore (Flash Storage)

Extstore (-o ext_path=...) is not supported. Swytch has its own tiered storage implementation available in Redis mode, but the memcached interface is memory-only.

Protocol

  • Binary protocol - Only ASCII/text protocol is supported
  • SASL authentication - The -S flag is accepted but ignored
  • TLS/SSL - Not supported

Commands

  • stats cachedump - Disabled by default (security)
  • stats slabs - Slabs are not used in this implementation
  • stats items - Not implemented
  • stats sizes - Not implemented
  • stats conns - Not implemented
  • stats settings - Not implemented
  • watch - Log watcher not implemented
  • lru_crawler - LRU crawler commands not implemented (different eviction model)
  • lru - LRU tuning commands not implemented
  • cache_memlimit - Runtime memory limit adjustment not implemented
  • verbosity - Runtime verbosity adjustment not implemented

Server Options

  • Daemon mode (-d) - Use a process manager (systemd, supervisord) instead
  • User switching (-u) - Run container/process as desired user instead
  • Disable evictions (-M) - Memory-based eviction is always enabled
  • Multiple listen addresses - Only first address is used if multiple provided
  • UDP (-U) - Accepts the flag but UDP support is limited
  • Extstore options (-o ext_path, etc.) - Not supported

Differences from Memcached

Eviction Policy

Swytch uses a self-tuning frequency-based eviction algorithm comparable to S3-FIFO instead of memcached’s LRU. This typically results in better hit rates for workloads with frequency skew.

Memory Management

Unlike memcached’s slab allocator, Swytch uses Go’s memory allocator with a background goroutine that enforces memory limits. This simplifies configuration (no slab tuning) but means memory usage patterns differ.

Key Size Limit

Keys are limited to 250 bytes, matching memcached’s limit.

Expiration

  • Expiration times are interpreted the same way as memcached:
    • 0 = never expire
    • 1-2592000 (30 days) = relative seconds from now
    • >2592000 = absolute Unix timestamp
  • Expired items are lazily cleaned up on access

Flush Behavior

flush_all with a delay marks items created before the command as flushed once the delay passes. New items created after the flush command are not affected.

Example Usage

# Connect with netcat
$ nc localhost 11211

# Store a value
set mykey 0 3600 5
hello
STORED

# Retrieve it
get mykey
VALUE mykey 0 5
hello
END

# Increment a counter
set counter 0 0 1
0
STORED

incr counter 5
5

# Check and set with CAS
gets mykey
VALUE mykey 0 5 42
hello
END

cas mykey 0 3600 5 42
world
STORED

# Get statistics
stats
STAT pid 12345
...
END

# Quit
quit

Monitoring

Web Dashboard

Enable the dashboard for real-time monitoring:

swytch memcached --dashboard --dashboard-addr :8080

Access at http://localhost:8080 to see:

  • Hit/miss rates
  • Memory usage
  • Connection counts
  • Per-shard cache statistics

Prometheus Metrics

Enable Prometheus metrics:

swytch memcached --metrics-port 9091

Scrape metrics from http://localhost:9091/metrics.

Operations

Signal Handling

Swytch handles POSIX signals for graceful shutdown:

SignalBehavior
SIGTERMGraceful shutdown. Completes in-flight requests, flushes pending writes, exits
SIGINTSame as SIGTERM (Ctrl+C)
SIGHUPNot handled (no config reload)

For container orchestration (Kubernetes, Docker), send SIGTERM to gracefully stop the server. Swytch will:

  1. Stop accepting new connections
  2. Complete in-flight commands
  3. Exit cleanly

There is no drain period configuration; shutdown completes as fast as pending work allows.

Feedback

If you encounter issues or need features not listed here (such as meta-commands), please report them at the issue tracker.