Python Memcached Append (Detailed Guide w/ Code Examples)

Use Case(s)

Appending data to an existing key in memcached can be useful when you want to add more information to the value of a key without overwriting the existing data. This is common in scenarios like logging actions, collaboration tools, or when processing incremental data.

Code Examples

Let's use python-memcached module which is a pure python memcached client.

To append data, first, we will set a key-value pair and then append data:

import memcache # create a connection to memcached server mc = memcache.Client([''], debug=0) # set a key-value pair mc.set("sample_key", "Hello") # append data to the existing key mc.append("sample_key", ", World") # get appended key print(mc.get("sample_key")) # Output: Hello, World

Here, we first connect to the memcached server running at localhost on port 11211. We then set the value "Hello" for the key "sample_key". Finally, we append ", World" to the same key. When we fetch that key again, the output reflects the appended data.

Best Practices

  • Always check if the key exists before appending to it, as attempting to append to a non-existing key won't create a new one. You can use get method to ensure the key is present.
  • Use this feature wisely, because it affects memory usage. Too much appending may cause eviction of other keys if memory fills up.

Common Mistakes

  • Attempting to append to a non-existing key. It doesn't create a new key; instead, it simply does nothing.
  • Overusing the append operation without considering memory management. If you are appending a large amount of data, it might be more efficient to consider other storage options.


Q: Can I prepend data as well in memcached? A: Yes, the prepend operation can be used to add data to the beginning of an existing key's value.

Q: What happens if I attempt to append to a non-existing key? A: Attempting to append to a non-existing key will simply do nothing. It won't create a new key.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.