TL;DR

  • Scenario: Java service integrating with Memcached needs to understand Spymemcached’s thread model, sharding routing and serialization details
  • Conclusion: Spymemcached implements async IO via NIO+callback, uses ketama consistent hashing for Sharding
  • Output: Complete architecture breakdown, thread and Sharding mechanism explanation, and error quick reference

Spymemcached Introduction

Spymemcached is a memcached client implemented using NIO.

Protocol Support:

  • Text Protocol: Pure text-based protocol, easy to debug and read
  • Binary Protocol: Binary protocol, higher transmission efficiency

Async Communication Mechanism: Uses NIO (Non-blocking I/O) for efficient network communication, handles responses through callback mechanism.

Cluster and Sharding: Supports Sharding mechanism, uses consistent hashing algorithm to allocate keys, supports dynamic node addition and removal.

Overall Design Architecture

  1. API Interface Design: Provides synchronous and asynchronous calling methods
  2. Task Encapsulation Mechanism: Encapsulates requests as independent Task objects
  3. Routing Partition Strategy: Uses consistent hashing algorithm to determine target node
  4. Task Queue Management: Each connection maintains independent task queue
  5. Async IO Processing Flow: Selector monitors IO events for all connections
  6. Response Processing Mechanism: Matches original Task based on opaque field, executes callback

Thread Design

Spymemcached has two types of threads: business threads and Selector threads.

Business threads are responsible for:

  • Request encapsulation
  • Object serialization
  • Protocol encapsulation
  • Task distribution

Selector threads are responsible for:

  • Send processing: Queue polling, data sending, flow control
  • Receive processing: Response reading, result notification
  • Connection management: Fault detection, automatic recovery, load balancing

Sharding Mechanism

Routing Mechanism

Two hashing algorithms:

  1. arrayMod (Array Modulo Hashing)

    • Calculation: hash(key) % node count = target node index
    • Feature: Node addition/removal causes large-scale data migration
  2. ketama (Consistent Hashing)

    • Generates 160 virtual nodes for each physical node
    • Builds hash ring, evenly distributes virtual nodes
    • Advantage: Only 1/N data needs migration on average when nodes are added/removed

Fault Tolerance

Three Failover handling strategies:

  1. Redistribute strategy (recommended): Round-robin/randomly select next available node
  2. Retry strategy: Max retry 3 times, linearly increasing retry interval
  3. Cancel strategy: Log error, throw ConnectionException

Serialization

Objects are stored in Memcached in binary form. After serialization, if length exceeds threshold of 16384 bytes, GZip compression is applied.

Error Quick Reference

SymptomRoot CauseFix
Many get timeouts under high concurrencySingle node pressure too high, Selector thread can’t keep upReduce single node pressure, increase timeout config
Cache hit rate drops to 0 after node add/removeUsing arrayMod shardingSwitch to ketama consistent hashing
Many connection exceptions after a node failsFailover strategy misconfiguredLimit max retry count
Client process memory continuously grows after QPS increasesTask queue, Future or callback holds large object referencesSet reasonable upper limit for queue
Some keys occasionally return NOT_FOUNDInconsistent sharding routingFix node list order and weights
CPU spikes when writing large objectsSerialization + GZip compression overheadAdjust compression threshold, avoid super large objects in cache