Skip to main content

Memcached dependency for nameko services with consistent hashing

Project description

nameko-pymemcache

PyPI version Build Status

Memcached dependency for nameko services with consistent hashing support for multi-node setups. Uses the high-performance pymemcache library with automatic failover and connection pooling.

Key Features:

  • Consistent hashing for reliable multi-node memcached clusters
  • Optimized for Nameko - proper connection management and worker cleanup
  • Django compatible - same interface as django-pymemcache for easy integration
  • Automatic failover when nodes become unavailable
  • Drop-in replacement for bmemcached with better multi-node behavior

Inspiration and structure proudly stolen from nameko-redis :) Thanks guys!

Installation

pip install nameko-pymemcache

Usage

from nameko.rpc import rpc
from nameko_pymemcache import Memcached


class MyService(object):
    name = "my_service"

    memcached = Memcached()

    @rpc
    def hello(self, name):
        self.memcached.set("foo", name)
        return "Hello, {}!".format(name)

    @rpc
    def bye(self):
        name = self.memcached.get("foo")
        return "Bye, {}!".format(name)

To specify memcached uri(s) and optional username/password you will need a config

AMQP_URI: 'amqp://guest:guest@localhost'
MEMCACHED_URIS: ['127.0.0.1:11211', ]
MEMCACHED_USER: 'playerone'
MEMCACHED_PASSWORD: 'ready'

Multi-Node Configuration

For multi-node memcached clusters, simply specify multiple servers:

AMQP_URI: 'amqp://guest:guest@localhost'
MEMCACHED_URIS: 
  - '192.168.1.10:11211'
  - '192.168.1.11:11211'
  - '192.168.1.12:11211'

The client automatically uses consistent hashing to distribute keys across nodes. When a node fails, only the keys on that node are affected (not all keys like with simple round-robin).

Advanced Configuration

You can pass extra options to customize client behavior:

class MyService(object):
    name = "my_service"

    memcached = Memcached(
        connect_timeout=0.1,    # connection timeout in seconds
        timeout=0.2,            # operation timeout in seconds
        retry_attempts=2,       # number of retries on failure
        dead_timeout=10,        # how long to avoid a failed node
    )

    ...

Framework Compatibility

This package provides a consistent interface that works seamlessly with Django's pymemcache backend, making it easy to share cache configurations between Django and Nameko services:

# Same server configuration works in both Django and Nameko
CACHE_SERVERS = ['192.168.1.10:11211', '192.168.1.11:11211', '192.168.1.12:11211']

# Django settings.py
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
        'LOCATION': CACHE_SERVERS,
    }
}

# Nameko config.yaml  
MEMCACHED_URIS: ['192.168.1.10:11211', '192.168.1.11:11211', '192.168.1.12:11211']

Performance Tips

  • Identical server order: Keep the same server order across all clients for consistent key distribution
  • Connection pooling: Available via pymemcache options if needed
  • Custom timeouts: Override defaults by passing pymemcache options to the constructor
  • Failure handling: Failed nodes are automatically removed from the hash ring and retried later

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nameko_pymemcache-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nameko_pymemcache-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file nameko_pymemcache-0.1.0.tar.gz.

File metadata

  • Download URL: nameko_pymemcache-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nameko_pymemcache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a6da4aff9b91d6aa16ab02754c9276f915975a8deb92d9457e636aeb0cc11d42
MD5 05df9a8816533948b176541385cfc7c1
BLAKE2b-256 d173611a11f89949504f093d39de06d00fca49de73399746a2cd831a13c24a6c

See more details on using hashes here.

File details

Details for the file nameko_pymemcache-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nameko_pymemcache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df026e89cbbd7a29397b87a4150459efe1d70bf56a311bc8cc1c22d58bf81219
MD5 30c517f52068d6e22cda5b4d42b3f319
BLAKE2b-256 ed18468fbb3966f057dfa363bead139e59b92aa2078747658d572eecb3bddde8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page