Memcached dependency for nameko services with consistent hashing
Project description
nameko-pymemcache
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nameko_pymemcache-0.1.3.tar.gz.
File metadata
- Download URL: nameko_pymemcache-0.1.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fdbb5e5327a9cd53a4dbd92c4bb907fcab50a43144ca356838b753d4de764afb
|
|
| MD5 |
e3f1d2be6ad80a4c0f5bfe562bd15a45
|
|
| BLAKE2b-256 |
5306885c84bcf1da53696bc414b8eb23588234bc6a1a037f96d5e2d6b0226722
|
File details
Details for the file nameko_pymemcache-0.1.3-py3-none-any.whl.
File metadata
- Download URL: nameko_pymemcache-0.1.3-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56a336b1bc05184f03fa95bc2e4524e854df486918923f6cfd34e7fdc039faaa
|
|
| MD5 |
72788651bc0d53a751100ce23054876d
|
|
| BLAKE2b-256 |
8fff4edf1333a1b3775abd2af8fdb59fc6d973c84caac2647117e125a5c5bb9c
|