OpenBSD Handbook

    Theme
    • Part I. Install & Configure
      • Introduction to OpenBSD
      • Installing OpenBSD
      • The X Window System
      • Networking on OpenBSD
      • System Configuration
      • OpenBSD Basics
      • Managing Software: Packages and Ports
    • Part II. Daily Operations
      • OpenBSD Desktop Environments
      • Multimedia
      • Printing
      • Linux Compatibility
      • Windows Compatibility
      • Games
    • Part III. System Administration
      • Virtualization
      • Storage and File Systems
      • Updating and Upgrading
      • Localization
      • The OpenBSD Boot Process
      • Security
    • Part IV. Networking & Daemons
      • Services
        • Database
          • MariaDB
          • PostgreSQL
          • Redis
          • memcached
        • Directory
          • YP (NIS)
          • LDAP
        • File
          • NFS
          • Samba
        • FTP Services
          • ftpd
          • ProFTPD
          • vsftpd
          • Pure-FTPd
          • TFTP
        • Mail
          • OpenSMTPD Mail Server
          • Dovecot IMAP and POP3 Server
          • Postfix
          • Exim
          • Rspamd
        • Name
          • Unbound DNS Resolver
          • NSD Authoritative DNS Server
          • BIND DNS Server
        • Networking
          • OpenBGPD
          • rad
          • DHCP on OpenBSD
          • slaacd
        • Web
          • Apache HTTP Server
          • nginx Web Server
          • OpenBSD httpd Web Server
          • relayd Reverse Proxy
        • Logging
          • syslogd
        • Monitoring
          • SNMP
        • Remote Access
          • Audit OpenSSH
          • OpenSSH Server
        • File Synchronization
          • rsync
        • Messaging
          • RabbitMQ Message Broker
        • Time
          • NTP Time Synchronization
        • Load Balancing
          • Build Redundant Load-Balanced Gateways
          • Load Balance Services with NAT or DSR
          • Build an HA HTTPS Load Balancer
      • PF
        • pfctl cheat sheet
        • PF Anchors
        • PF Filter Rules
        • PF Forwarding
        • PF Lists and Macros
        • PF Load Balancing
        • PF Logging
        • PF NAT
        • PF Options
        • PF Policies
        • PF Shortcuts
        • PF Tables
      • Advanced Networking
        • High Availability and State Replication
        • Multi-WAN and Policy-Based Routing
        • VPN and Cryptographic Tunneling
        • Classic and Lightweight Tunnels
        • IPv6 at Scale
        • QoS and Traffic Shaping
        • MPLS and Label Distribution
        • Network Services at Scale
        • Virtualization and Host Networking
        • Large-Scale L2 and L3 Design
        • Telemetry, Logging, and Flow Export
        • Hardening and Operational Safety
        • Reference Architectures
        • Troubleshooting Playbooks
      • Serial Communication
    • Part V. Miscellaneous
      • Virtualization Cheat Sheet
      • OpenBSD Cheatsheet
      • How-to Guides
        • Installation and First Boot
          • Complete the First 30 Minutes After Installation
          • Check Hardware Compatibility Before Installing
          • Bootstrap Wi-Fi Firmware Without Ethernet
          • Run OpenBSD as a Virtual Machine Guest
          • Install OpenBSD on a VPS or Cloud Server
          • Install OpenBSD with Full-Disk Encryption
          • Dual-Boot OpenBSD on a UEFI System
          • Automate OpenBSD Installation with Autoinstall and PXE
        • Desktops and Laptops
          • Set Up OpenBSD as a Laptop or Workstation
          • Configure Laptop Power Management
          • Troubleshoot Web Browsers on OpenBSD
          • Use Plan 9 Tools on OpenBSD
          • Build a Minimal Desktop with suckless Tools
          • Install Z shell (zsh)
        • Networking and Remote Access
          • Create an Administrator Account with doas and SSH Keys
          • Configure Secure Remote Access with OpenSSH
          • Use FIDO Security Keys with OpenSSH
          • Configure Wi-Fi Roaming and Wired Failover
          • Troubleshoot PF, NAT, and Routing
          • Route Multiple VLANs with PF
          • Configure a Road-Warrior WireGuard VPN
          • Route IPv6 Networks without NAT
          • Test OpenBGPD with Two Providers
          • Unlock an Internal Service with an SSH Key
          • Build a Simple Router and Firewall
        • Servers and Services
          • Set Up WordPress on OpenBSD
          • Build a Mail Server with OpenSMTPD, Dovecot, and Rspamd
        • Security, Maintenance and Recovery
          • Establish a Conservative Security Baseline
          • Recover Access and Reset the Root Password
          • Collect OpenBSD Diagnostic Evidence
          • Troubleshoot Package Installation and Update Failures
          • Schedule Recurring Maintenance
          • Monitor an OpenBSD System
          • Back Up and Restore an OpenBSD System
          • Configure softraid RAID1
          • Recover an OpenBSD System That Does Not Boot
        • Building, Automation and Virtual Machines
          • Manage OpenBSD with Ansible
          • Build Third-Party Software from Ports
          • Build OpenBSD from Source
          • Plan a Docker Workload Migration to OpenBSD
      • OpenBSD for Linux Users
      • OpenBSD for FreeBSD Users
      • OpenBSD for macOS Users
    • OpenBSD FAQ
    • OpenBSD Architectures
      • OpenBSD Package Search
        • Synopsis
        • Installation
        • Configuration
        • Testing Basic Usage
        • Using with Clients
        • Security Considerations
        • Service Management
        • Logging

        memcached

        Synopsis #

        memcached is a high-performance in-memory key-value store used primarily for caching data to reduce database load and latency in dynamic web applications. It provides a simple TCP protocol for setting and retrieving short-lived values and is often used by frameworks like Django, Rails, and PHP.

        memcached is available on OpenBSD via packages and integrates with rcctl(8) for service management.

        Unlike Redis, memcached is not persistent and supports only very simple key-value operations.

        Installation #

        Install memcached using the package system:

        doas pkg_add memcached
        

        This installs:

        • /usr/local/bin/memcached — the daemon
        • /etc/rc.d/memcached — rcctl control script

        Configuration #

        memcached is typically configured via command-line options. Enable the packaged service before setting persistent rcctl flags:

        doas rcctl enable memcached
        doas rcctl set memcached flags "-u _memcached -m 64 -l 127.0.0.1 -p 11211"
        

        Common options:

        • -m 64 — allocate 64 MB of RAM
        • -l 127.0.0.1 — bind only to localhost
        • -p 11211 — default TCP port

        To apply changes:

        doas rcctl enable memcached
        doas rcctl start memcached
        

        Testing Basic Usage #

        Use the telnet(1) client to test memcached:

        $ telnet localhost 11211
        Trying 127.0.0.1...
        Connected to localhost.
        set testkey 0 300 5
        hello
        STORED
        get testkey
        VALUE testkey 0 5
        hello
        END
        
        • set <key> <flags> <ttl> <bytes>
        • get <key>

        TTL values up to 30 days are relative seconds; larger values are Unix timestamps. Zero disables expiry. Items can still be evicted when memory is needed.

        To quit:

        quit
        

        Using with Clients #

        memcached is supported by many programming languages. Examples:

        • PHP: memcached or memcache extension
        • Python: python3-memcached or pymemcache
        • Ruby: dalli gem

        Example Python usage:

        import memcache
        mc = memcache.Client(['127.0.0.1:11211'])
        mc.set('greeting', 'hello', time=300)
        print(mc.get('greeting'))
        

        Security Considerations #

        The release package builds TLS support, and a SASL flavor is available. Neither is enabled by the simple configuration below; restrict the listener and firewall to trusted clients. Consult the installed README before enabling authentication or TLS.

        By default, it should be bound to 127.0.0.1. If remote access is necessary:

        1. Adjust flags:
        doas rcctl set memcached flags "-u _memcached -m 128 -l 192.0.2.1 -p 11211"
        
        1. Restrict access using pf(4):
        pass in quick on $int_if proto tcp from 192.0.2.0/24 to port 11211
        block in on $int_if proto tcp from any to port 11211
        
        1. Reload pf and restart memcached:
        doas pfctl -f /etc/pf.conf
        doas rcctl restart memcached
        

        Never expose memcached to the public Internet.

        Service Management #

        To start and enable at boot:

        doas rcctl enable memcached
        doas rcctl start memcached
        

        To stop or restart:

        doas rcctl stop memcached
        doas rcctl restart memcached
        

        Check status:

        doas rcctl check memcached
        

        Logging #

        The daemon writes verbose diagnostics to standard error. Do not assume that the packaged service sends these to syslog; inspect the service launch and use a controlled foreground diagnostic session if needed.

        To increase verbosity, add -v or -vv to flags:

        doas rcctl set memcached flags "-u _memcached -m 64 -l 127.0.0.1 -p 11211 -v"
        doas rcctl restart memcached
        
        Report a bug
        • Synopsis
        • Installation
        • Configuration
        • Testing Basic Usage
        • Using with Clients
        • Security Considerations
        • Service Management
        • Logging