Skip to content

Memcached - Append Data

The Memcached append command adds data to the end of an existing value. This operation is atomic but less common than set or cas. It’s useful for scenarios where you need to build up a value over time, such as logging or tracking, without fetching the entire value first.

Important: append only works if the key already exists. It will not create a new key.

The basic syntax for the append command is:

append key flags exptime bytes [noreply]
value

The parameters flags and exptime are currently ignored by the server but are required for syntax compatibility. The bytes and value are the data you wish to append.

  • STORED indicates the data was successfully appended.
  • NOT_STORED indicates that the append failed, most commonly because the key does not exist.
  • CLIENT_ERROR indicates a syntax error.

This example shows that append fails on a non-existent key, then succeeds after the key has been created with set.

# 1. Attempt to append to a key that doesn't exist.
append activity_log 0 0 7
,start
NOT_STORED
# 2. Set an initial value for the key.
set activity_log 0 900 4
init
STORED
# 3. Append to the existing key.
append activity_log 0 900 7
,start
STORED
# 4. Check the result.
get activity_log
VALUE activity_log 0 11
init,start
END
# 5. Append again.
append activity_log 0 900 5
,stop
STORED
# 6. Check the final result.
get activity_log
VALUE activity_log 0 16
init,start,stop
END

Modern Application: Appending Data with Python

Section titled “Modern Application: Appending Data with Python”

Here’s how to use the append functionality with the pymemcache library in Python.

Ensure pymemcache is installed:

pip install pymemcache
from pymemcache.client.base import Client
MEMCACHED_SERVER = ('127.0.0.1', 11211)
def run_append_example():
"""Demonstrates the append operation."""
client = Client(MEMCACHED_SERVER)
key = 'audit_trail:user:77'
# Ensure we have a clean slate
client.delete(key)
try:
# 1. Attempt to append to a non-existent key
# Note: Appending bytes is required.
was_appended = client.append(key, b':login_failed')
print(f"-> APPEND to non-existent key '{key}'... Success: {was_appended}")
# 2. Set an initial value
# Note: Values must be bytes, strings, or serializable.
client.set(key, b'2023-10-27T10:00:00Z:session_created', expire=600)
print(f"-> SET: Initialized key '{key}'.")
# 3. Append to the existing key
was_appended = client.append(key, b'\n2023-10-27T10:01:00Z:profile_viewed')
print(f"-> APPEND to existing key... Success: {was_appended}")
# 4. Append another event
was_appended = client.append(key, b'\n2023-10-27T10:02:00Z:logged_out')
print(f"-> APPEND again... Success: {was_appended}")
# 5. Retrieve the final concatenated value
final_value = client.get(key)
if final_value:
print("\n--- Final Value in Cache ---")
print(final_value.decode('utf-8'))
print("--------------------------")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
client.close()
if __name__ == "__main__":
run_append_example()
-> APPEND to non-existent key 'audit_trail:user:77'... Success: False
-> SET: Initialized key 'audit_trail:user:77'.
-> APPEND to existing key... Success: True
-> APPEND again... Success: True
--- Final Value in Cache ---
2023-10-27T10:00:00Z:session_created
2023-10-27T10:01:00Z:profile_viewed
2023-10-27T10:02:00Z:logged_out
--------------------------
  • Data Format: append operates at the byte level. It does not understand data structures like JSON or lists. If you store a serialized JSON string and append to it, you will likely create invalid JSON. It is best used for raw text, logs, or comma-separated values.
  • Alternative to Read-Modify-Write: The main advantage of append is avoiding a get -> concatenate -> set cycle, which reduces network traffic and eliminates the need for CAS if you are only ever adding to the end of a value.
  • prepend Command: A corresponding prepend command also exists, which adds data to the beginning of the value. It has the same syntax and constraints as append.
  • Limited Use Cases: In modern systems, structured data (like JSON) is more common. For lists, it’s often better to use a tool designed for data structures, like Redis Lists. Therefore, append has more niche applications today.