Friday, February 5, 2016

Memcached

What is Memcached?

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
 What is it Made Up Of?
  • Client software, which is given a list of available memcached servers.
  • A client-based hashing algorithm, which chooses a server based on the "key".
  • Server software, which stores values with their keys into an internal hash table.
  • LRU, which determine when to throw out old data (if out of memory), or reuse memory.

Forgetting is a Feature

Memcached is, by default, a Least Recently Used cache. Items expire after a specified amount of time. Both of these are elegant solutions to many problems; Expire items after a minute to limit stale data being returned, or flush unused data in an effort to retain frequently requested information.
No "pauses" waiting for a garbage collector ensures low latency, and free space is lazily reclaimed.

Cache Invalidation

Rather than broadcasting changes to all available hosts, clients directly address the server holding the data to be invalidated.

Cache Results

function get_foo(foo_id)
    foo = memcached_get("foo:" . foo_id)
    return foo if defined foo

    foo = fetch_foo_from_database(foo_id)
    memcached_set("foo:" . foo_id, foo)
    return foo
end

Play with telnet

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get foo
VALUE foo 0 2
hi
END
stats
STAT pid 8861
(etc)

No comments:

Post a Comment