Redis

    Main commands

    • List all keys: KEYS *
    • Get content: GET index_key

    Comparison with Memcached

    • Item storage limit: Memcached is limited to 1 MB per item while Redis is limited to 512 MB per item stored (not advised to change this configuraiton for both).
    • Expiration method: Memcached uses LRU. Redis is configurable and so you can use LRU, LFU, random exiction, TTL...
    • Usage of resources (cpu/memory): Memcached is multi-threaded, both for background processes and event processing. Can be vertically scaled. Redis is mostly single threaded, with some background tasks such as the defragger running in the background, but for adds/updates/deletes events hit a core event loop.

    Memcached organises memory into pages, slabs and chunks. When you start the service you define how much memory memcached is allowed to use to the nearest MB. Memcached on starting will allocate that much memory and then (by default) break it into 1 MB pages. These pages when first allocated are empty and called free pages. Once you add an item to memcached it looks at the size in bytes of what you are adding, and then assignes a free page a slab class. The slab class determines the number of chunks, containing items that go into the slab. By default the smallest chunk size is 80 bytes.

    Memcached memory item storage Memcached memory item storage slab

    Once you add an item to memcached it looks at the size in bytes of what you are adding, and then assignes a free page a slab class. The slab class determines the number of chunks, containing items that go into the slab. By default the smallest chunk size is 80 bytes. [...] Say you wanted to store a 70 byte item in memcached. Given that 80 bytes is the smallest chunk size, when memcached stores the item there is an overhead of 10 bytes out of the 80 which remains unused.

    Redis by contrast allocates memory on a per item stored basis. When an item comes into Redis, it allocates memory through a malloc call and stores the item in the space. Add another record? Same thing, malloc and store. Expiration of items has a free called on the allocated space. As such after time you end up with "holes" in memory.

    Redis memory item storage

    Which one to use ?

    Reasons to use memcached, If you need to be able to scale up by throwing more CPU at the problem If you are really hammering the cache If you are caching lots of very small values, ideally all the same size If you are running the cluster yourself, memcached is much easier to setup and maintain Otherwise I would suggest using Redis and use the other things it brings, such as its lovely data types, streams and such. However there is an unmentioned secret about memcached and redis. Which is that both are network caches.

    source