skip to Main Content

I’m learning about redis/memcache and redis is clearly the more popular option. My question is about supported data types. At my company we use the memcashier library which is built in memcached. We store temporary user data when they’re making a purchase in memcache. We can easily update this object as things are added to the cart or more info about the user is given. This appears to be the same functionality as a hash in redis. I don’t understand how this is only a basic string data type and how it’s less powerful than a hash.

2

Answers


  1. If you are using strings, that’s fine – but any change involves loading the data to your application, parsing it, modifying it, and serializing it back to Redis/Memcache.

    This has two problems: it’s slow and non atomic. You can have two servers modifying the same object arriving in an inconsistent state – such as double or missing items in a shopping cart. And again, it’s slow.

    With a Redis hash key, you can atomically modify specific fields of the object without loading the entire object into memory. Instead of read, parse, modify, save – you just update.

    Besides, Redis has many many data structures that can create very flexible data stores with different properties, whereas Memcache can only store strings.

    BTW Redis has a module that allows you to store JSON objects just as you would a string, and manipulate them directly and atomically without getting them to the client. See Rejson.io for details.

    Login or Signup to reply.
  2. Memcached doesn’t support complex datastructures

    In redis you have Lists, Sets, SortedSets, HashTables , and more.
    Each data-structure mentioned above supports mutation of one or more of its elements atomically and without replacing the entire data-structure/value.

    Memcached on the other hand , is a simple key-value store – that means every operation involving an attribute change within a complex object is a read-modify-write. If you just go around blindly replacing fields in objects then you are risking race-conditions and operations atomicity issues (which you can get away from by using CAS )

    If the library abstracts that complexity, well – that’s great but it’s still less efficient than mutating only the relevant field(s)

    This answer only relates to your usecase. Redis holds many other virtues over memcached, which are not relevant to this question.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search