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
  • Automatic failover when nodes become unavailable
  • Drop-in replacement for bmemcached with better multi-node behavior
  • High performance - uses pymemcache's efficient C implementation

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, specify multiple servers using either format:

YAML list format:

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

Bracketed list format:

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
    )

    ...

Available Operations

All standard memcached operations are supported:

# Basic operations
self.memcached.set(key, value, expire=300)
result = self.memcached.get(key)
self.memcached.delete(key)

# Batch operations
self.memcached.set_many({'key1': 'val1', 'key2': 'val2'})
results = self.memcached.get_many(['key1', 'key2'])

# Increment/decrement operations
self.memcached.incr(key, delta=1)
self.memcached.decr(key, delta=1)

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.1.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.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nameko_pymemcache-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 f73916e796d1b3598d759b465f8823816c7302a3e7b6a2a19455cf56b08d42b2
MD5 2be06d811d11796fa327781bb176e7c3
BLAKE2b-256 59280c22e06f8d5155e3114874cc3eb83e7def1b18d714603551cf870aa0cadb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nameko_pymemcache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 075992db63b5e664fe428c47be75ea7cff0b3329b22570b003cb7418778a6183
MD5 92f4eff89296ba2edea4e8bf6946fdd9
BLAKE2b-256 6df71b8ae988fb98e0d6297d34712cf431063e5748d079d534f6c9faec6d0552

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