Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example on using aiomcache for a cluster #195

Open
duxing opened this issue Jul 31, 2020 · 6 comments
Open

Example on using aiomcache for a cluster #195

duxing opened this issue Jul 31, 2020 · 6 comments

Comments

@duxing
Copy link

duxing commented Jul 31, 2020

Thanks for building this amazing lib!
I was wondering if you can share an example for using aiomcache to connect to a memcached cluster with more than one host?

Seems like this is how connection are established and I don't see any other source code for connection management for multi-node cluster. Given how widely memcached clusters are used, I think an example would be really helpful

@takeda
Copy link

takeda commented Aug 5, 2023

@duxing I was looking at the source code, as I'm still wondering if I should use aiomcache, emcache or pymemcache (probably will end up with emcache as pymemcache provides most features, but emcache is async, and aiomcache doesn't appear to have cluster support). I don't see any hashing code. I doubt this library provides clustering support at the time of writing this.

@duxing
Copy link
Author

duxing commented Aug 7, 2023

@takeda I did the same assessment and I prefer the features and performance of aiomcache more than other options. I briefly went through the source code and didn't see the client-side of hashing code and I haven't continued to figure out the set up for a multi-node cluster. Not having that support would be a deal breaker for production adoption and I really wish it can be supported

@Dreamsorcerer
Copy link
Member

But, if anyone wants to work on it, we can certainly accept new PRs.

@takeda
Copy link

takeda commented Aug 7, 2023

The hash computation can be expensive, especially with large number of nodes, because (as I understand from looking at emcache code) the hashing is done for every node, and then node with highest score is picked up. That could be the reason why it is slower.

I do like aiomcache interface, get, and multi_get feels clearer than get and get_multi.

@STOCKZE
Copy link

STOCKZE commented Jul 8, 2024

same here, looking for hashing - cluster - hashring implementation through aiomcache

import uhashring
from aiocache import caches, cached, AioCache

# Define your memcached server list
MEMCACHED_SERVERS = [
    {"host": "server1", "port": 11211},
    {"host": "server2", "port": 11211},
    # Add more servers as needed
]

# Create a hashring instance
hash_ring = uhashring.HashRing(initial_servers=MEMCACHED_SERVERS)

# Define a custom cache backend class with hashring
class MemcachedHashRingBackend(AioCache):
    async def get(self, key):
        server = hash_ring.get_node(key)  # Get server using hashring
        async with caches[server["host"]].get(key) as value:
            return value

    async def set(self, key, value, ttl=None):
        server = hash_ring.get_node(key)
        await caches[server["host"]].set(key, value, ttl)

    async def delete(self, key):
        server = hash_ring.get_node(key)
        await caches[server["host"]].delete(key)

# Configure aiocache with the custom backend
cache = caches.get("my_cache", backend=MemcachedHashRingBackend)

# Example usage with caching
@cached(ttl=60, cache=cache)
async def fetch_data(key):
    # Simulate data fetching
    data = f"Data for key {key}"
    return data

async def main():
    data = await fetch_data("my_key")
    print(data)

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

@STOCKZE
Copy link

STOCKZE commented Jul 18, 2024

facebook/mcrouter#368 (comment) mcrouter has raft / consensus while replicating as seen from this reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants