---
name: "memcached_behavior_set"
section: "3"
platform: "linux"
package: "libmemcached-awesome"
version: "1.1.4-4"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/memcached_behavior_set"
---

# memcached\_behavior\_set(3)

libmemcached Documentation Manipulate the behavior of a memcached\_st structure.

## NAME

memcached\_behavior\_set - libmemcached Documentation

Manipulate the behavior of a memcached\_st structure.

## SYNOPSIS

- **#include \<libmemcached/memcached.h\>**

  Compile and link with -lmemcached

- **uint64\_t memcached\_behavior\_get(memcached\_st \<#\_CPPv412memcached\_st\> \*ptr, memcached\_behavior\_t flag)**

  - **Parameters**

    - •

      **ptr** -- pointer to initialized **memcached\_st** \<#\_CPPv412memcached\_st\> struct

    - •

      **flag** -- **memcached\_behavior\_t** to query

  - **Returns**

    the vaue set for **flag**

- **memcached\_return\_t \<#\_CPPv418memcached\_return\_t\> memcached\_behavior\_set(memcached\_st \<#\_CPPv412memcached\_st\> \*ptr, memcached\_behavior\_t flag, uint64\_t data)**

  - **Parameters**

    - •

      **ptr** -- pointer to initialized **memcached\_st** \<#\_CPPv412memcached\_st\> struct

    - •

      **flag** -- **memcached\_behavior\_t** to set

    - •

      **data** -- the value to set for **flag**

  - **Returns**

    **memcached\_return\_t** \<#\_CPPv418memcached\_return\_t\> indicating success

  Changed in version 0.17: The **data** argument of **memcached\_behavior\_set()** was changed from taking a pointer to data value, to taking a uin64\_t.

- **typedef enum memcached\_behavior\_t memcached\_behavior\_t**

  - **enum memcached\_behavior\_t**

  - **enumerator MEMCACHED\_BEHAVIOR\_USE\_UDP**

    Causes **libmemcached** to use the UDP transport when communicating with a memcached server. Not all I/O operations are tested when this behavior is enabled.

    The following operations will return **MEMCACHED\_NOT\_SUPPORTED** \<#\_CPPv4N18memcached\_return\_t23MEMCACHED\_NOT\_SUPPORTEDE\> when executed with **MEMCACHED\_BEHAVIOR\_USE\_UDP** enabled:

    - •

      **memcached\_version()** \<#\_CPPv417memcached\_versionP12memcached\_st\>,

    - •

      **memcached\_stat()** \<#\_CPPv414memcached\_statP12memcached\_stPcP18memcached\_return\_t\>,

    - •

      **memcached\_get()** \<#\_CPPv413memcached\_getP12memcached\_stPKc6size\_tP6size\_tP8uint32\_tP18memcached\_return\_t\>,

    - •

      **memcached\_get\_by\_key()** \<#\_CPPv420memcached\_get\_by\_keyP12memcached\_stPKc6size\_tPKc6size\_tP6size\_tP8uint32\_tP18memcached\_return\_t\>,

    - •

      **memcached\_mget()** \<#\_CPPv414memcached\_mgetP12memcached\_stPPCKcPK6size\_t6size\_t\>,

    - •

      **memcached\_mget\_by\_key()** \<#\_CPPv421memcached\_mget\_by\_keyP12memcached\_stPKc6size\_tPPCKcPK6size\_t6size\_t\>,

    - •

      **memcached\_fetch()** \<#\_CPPv415memcached\_fetchP12memcached\_stPcP6size\_tP6size\_tP8uint32\_tP18memcached\_return\_t\>,

    - •

      **memcached\_fetch\_result()** \<#\_CPPv422memcached\_fetch\_resultP12memcached\_stP19memcached\_result\_stP18memcached\_return\_t\>,

    - •

      **memcached\_fetch\_execute()** \<#\_CPPv423memcached\_fetch\_executeP12memcached\_stP20memcached\_execute\_fnPv8uint32\_t\>.

    All other operations are tested but are executed in a 'fire-and-forget' mode, in which once the client has executed the operation, no attempt will be made to ensure the operation has been received and acted on by the server.

    **libmemcached** does not allow TCP and UDP servers to be shared within the same **libmemcached** client 'instance'. An attempt to add a TCP server when this behavior is enabled will result in a **MEMCACHED\_INVALID\_HOST\_PROTOCOL** \<#\_CPPv4N18memcached\_return\_t31MEMCACHED\_INVALID\_HOST\_PROTOCOLE\>, as will attempting to add a UDP server when this behavior has not been enabled.

  - **enumerator MEMCACHED\_BEHAVIOR\_NO\_BLOCK**

    This enables **SO\_LINGER** only, so the **[close(2)](/close/2.md) \<https://linux.die.net/man/2/close\>** call on the socket returns immediately.  I/O is always handled asynchronously in recent versions of **libmemcached**.

  - **enumerator MEMCACHED\_BEHAVIOR\_SND\_TIMEOUT**

    This sets the microsecond behavior of the socket against the SO\_SNDTIMEO flag.

  - **enumerator MEMCACHED\_BEHAVIOR\_RCV\_TIMEOUT**

    This sets the microsecond behavior of the socket against the SO\_RCVTIMEO flag.

  - **enumerator MEMCACHED\_BEHAVIOR\_TCP\_NODELAY**

    Disables Nagle's algorithm. See RFC 896 \<https://tools.ietf.org/html/rfc896\>.

  - **enumerator MEMCACHED\_BEHAVIOR\_HASH**

    Set the **hash algorithm** \<#\_CPPv416memcached\_hash\_t\> used for keys.

    Each hash has its advantages and its weaknesses. If you don't know or don't care, just go with the default.

  - **enumerator MEMCACHED\_BEHAVIOR\_DISTRIBUTION**

    Setting a **memcached\_server\_distribution\_t** you can enable different means of distributing values to servers.

    The default method is **MEMCACHED\_DISTRIBUTION\_MODULA** (hash of the key modulo number of servers).

    You can enable consistent hashing by setting **MEMCACHED\_DISTRIBUTION\_CONSISTENT**. Consistent hashing delivers better distribution and allows servers to be added to the cluster with minimal cache losses.

    Currently **MEMCACHED\_DISTRIBUTION\_CONSISTENT** is an alias for the value **MEMCACHED\_DISTRIBUTION\_CONSISTENT\_KETAMA**.

  - **enumerator MEMCACHED\_BEHAVIOR\_CACHE\_LOOKUPS**

    Deprecated since version 0.46(?): DNS lookups are now always cached until an error occurs with the server.

    Memcached can cache named lookups so that DNS lookups are made only once.

  - **enumerator MEMCACHED\_BEHAVIOR\_SUPPORT\_CAS**

    Support CAS operations (this is not enabled by default at this point in the server since it imposes a slight performance penalty).

  - **enumerator MEMCACHED\_BEHAVIOR\_KETAMA**

    Sets the default distribution to **MEMCACHED\_DISTRIBUTION\_CONSISTENT\_KETAMA** and the hash to **MEMCACHED\_HASH\_MD5** \<#\_CPPv4N16memcached\_hash\_t18MEMCACHED\_HASH\_MD5E\>.

  - **enumerator MEMCACHED\_BEHAVIOR\_KETAMA\_WEIGHTED**

    Sets the default distribution to **MEMCACHED\_DISTRIBUTION\_CONSISTENT\_KETAMA** with the weighted tests. Makes the default hashing algorithm for keys use **MEMCACHED\_HASH\_MD5** \<#\_CPPv4N16memcached\_hash\_t18MEMCACHED\_HASH\_MD5E\>.

  - **enumerator MEMCACHED\_BEHAVIOR\_KETAMA\_HASH**

    Sets the **hashing algorithm** \<#\_CPPv416memcached\_hash\_t\> for host mapping on continuum.

  - **enumerator MEMCACHED\_BEHAVIOR\_KETAMA\_COMPAT**

    Sets the compatibility mode. The value can be set to either **MEMCACHED\_KETAMA\_COMPAT\_LIBMEMCACHED** (this is the default) or **MEMCACHED\_KETAMA\_COMPAT\_SPY** to be compatible with the SPY Memcached client for Java.

  - **enumerator MEMCACHED\_BEHAVIOR\_POLL\_TIMEOUT**

    Modify the timeout in milliseconds value that is used by poll. The default value is -1. An signed int must be passed to **memcached\_behavior\_set()** to change this value (this requires casting). For **memcached\_behavior\_get()** a 'signed int' value will be cast and returned as 'unsigned long long'.

  - **enumerator MEMCACHED\_BEHAVIOR\_USER\_DATA**

    Deprecated since version \<: 0.30

  - **enumerator MEMCACHED\_BEHAVIOR\_BUFFER\_REQUESTS**

    Enabling buffered IO causes commands to "buffer" instead of being sent. Any action that gets data causes this buffer to be be sent to the remote connection. Quiting the connection or closing down the connection will also cause the buffered data to be pushed to the remote connection.

  - **enumerator MEMCACHED\_BEHAVIOR\_VERIFY\_KEY**

    Enabling this will cause **libmemcached** to test all keys to verify that they are valid keys.

  - **enumerator MEMCACHED\_BEHAVIOR\_SORT\_HOSTS**

    Enabling this will cause hosts that are added to be placed in the host list in sorted order. This will defeat consistent hashing.

  - **enumerator MEMCACHED\_BEHAVIOR\_CONNECT\_TIMEOUT**

    Set the timeout during socket connection in milliseconds. Specifying -1 means an infinite time‐out.

  - **enumerator MEMCACHED\_BEHAVIOR\_BINARY\_PROTOCOL**

    Enable the use of the binary protocol. Please note that you cannot toggle this flag on an open connection.

  - **enumerator MEMCACHED\_BEHAVIOR\_IO\_MSG\_WATERMARK**

    Set this value to tune the number of messages that may be sent before **libmemcached** should start to automatically drain the input queue.

    Setting this value to high, may cause **libmemcached** to deadlock (trying to send data, but the send will block because the input buffer in the kernel is full).

  - **enumerator MEMCACHED\_BEHAVIOR\_IO\_BYTES\_WATERMARK**

    Set this value to tune the number of bytes that may be sent before **libmemcached** should start to automatically drain the input queue (need at least 10 IO requests sent without reading the input buffer).

    Setting this value to high, may cause libmemcached to deadlock (trying to send data, but the send will block because the input buffer in the kernel is full).

  - **enumerator MEMCACHED\_BEHAVIOR\_IO\_KEY\_PREFETCH**

    The binary protocol works a bit different than the textual protocol in that a multiget is implemented as a pipe of single get-operations which are sent to the server in a chunk.

    If you are using large multigets from your application, you may improve the latency of the gets by setting this value so you send out the first chunk of requests when you hit the specified limit.  It allows the servers to start processing the requests to send the data back while the rest of the requests are created and sent to the server.

  - **enumerator MEMCACHED\_BEHAVIOR\_NOREPLY**

    Set this value to specify that you really don't care about the result from your storage commands (set, add, replace, append, prepend).

  - **enumerator MEMCACHED\_BEHAVIOR\_NUMBER\_OF\_REPLICAS**

    Specify the number of replicas **libmemcached** should store of each item (on different servers).

    This replication does not dedicate certain memcached servers to store the replicas in, but instead it will store the replicas together with all of the other objects (on the 'n' next servers specified in your server list).

    Requires the binary protocol and only supports (M)GET/SET/DELETE.

    **NOTE**: **libmemcached** does not guarantee nor enforce any consistency.

  - **enumerator MEMCACHED\_BEHAVIOR\_RANDOMIZE\_REPLICA\_READ**

    Allows randomizing the replica reads starting point. Normally the read is done from primary server and in case of failure the read is done from primary + 1, then primary + 2 all the way to 'n' replicas.

    This allows distributing read load to multiple servers with the expense of more write traffic.

    **NOTE**: Only errors to communicate with a server are considered failures, so **MEMCACHED\_NOTFOUND** \<#\_CPPv4N18memcached\_return\_t18MEMCACHED\_NOTFOUNDE\> does *not* account for failure.

  - **enumerator MEMCACHED\_BEHAVIOR\_CORK**

    Deprecated since version ?: This option has been deprecated with the behavior now built in and used appropriately on selected platforms.

  - **enumerator MEMCACHED\_BEHAVIOR\_KEEPALIVE**

    Enable TCP\_KEEPALIVE behavior.

  - **enumerator MEMCACHED\_BEHAVIOR\_KEEPALIVE\_IDLE**

    Specify time, in seconds, to mark a connection as idle. This is only available as an option on Linux.

  - **enumerator MEMCACHED\_BEHAVIOR\_SOCKET\_SEND\_SIZE**

    Find the current size of SO\_SNDBUF. A value of 0 means either an error occurred or no hosts were available. It is safe to assume system default if this occurs.

    If an error occurs you can check the last cached errno to find the specific error.

  - **enumerator MEMCACHED\_BEHAVIOR\_SOCKET\_RECV\_SIZE**

    Find the current size of SO\_RCVBUF. A value of 0 means either an error occurred or no hosts were available. It is safe to assume system default if this occurs.

    If an error occurs you can check the last cached errno to find the specific error.

  - **enumerator MEMCACHED\_BEHAVIOR\_SERVER\_FAILURE\_LIMIT**

    Deprecated since version 0.48: See **MEMCACHED\_BEHAVIOR\_REMOVE\_FAILED\_SERVERS**

    Set this value to enable the server be removed after continuous **MEMCACHED\_BEHAVIOR\_SERVER\_FAILURE\_LIMIT** times connection failure.

  - **enumerator MEMCACHED\_BEHAVIOR\_AUTO\_EJECT\_HOSTS**

    Deprecated since version 0.48: See **MEMCACHED\_BEHAVIOR\_REMOVE\_FAILED\_SERVERS**

    If enabled any hosts which have been flagged as disabled will be removed from the list of servers in the **memcached\_st** \<#\_CPPv412memcached\_st\> structure. This must be used in combination with **MEMCACHED\_BEHAVIOR\_SERVER\_FAILURE\_LIMIT**.

  - **enumerator MEMCACHED\_BEHAVIOR\_REMOVE\_FAILED\_SERVERS**

    If enabled any hosts which have been flagged as disabled will be removed from the list of servers in the **memcached\_st** \<#\_CPPv412memcached\_st\> structure.

  - **enumerator MEMCACHED\_BEHAVIOR\_RETRY\_TIMEOUT**

    When enabled a host which is problematic will only be checked for usage based on the amount of time set by this behavior. The value is in seconds.

  - **enumerator MEMCACHED\_BEHAVIOR\_HASH\_WITH\_PREFIX\_KEY**

    When enabled the prefix key will be added to the key when determining server by hash. See **MEMCACHED\_CALLBACK\_NAMESPACE** \<#\_CPPv4N20memcached\_callback\_t28MEMCACHED\_CALLBACK\_NAMESPACEE\> for additional information.

- **typedef enum memcached\_server\_distribution\_t memcached\_server\_distribution\_t**

  - **enum memcached\_server\_distribution\_t**

  - **enumerator MEMCACHED\_DISTRIBUTION\_MODULA**

    Distribute keys by hash modulo number of servers.

  - **enumerator MEMCACHED\_DISTRIBUTION\_CONSISTENT**

    Alias for **MEMCACHED\_DISTRIBUTION\_CONSISTENT\_KETAMA**.

  - **enumerator MEMCACHED\_DISTRIBUTION\_CONSISTENT\_KETAMA**

    Unweighted consistent key distribution.

  - **enumerator MEMCACHED\_DISTRIBUTION\_RANDOM**

    Distribute keys by **[rand(3)](/rand/3.md) \<https://linux.die.net/man/3/rand\>** modulo number of servers.

  - **enumerator MEMCACHED\_DISTRIBUTION\_CONSISTENT\_KETAMA\_SPY**

    Unweighted consistent key distribution compatible with the SPY client.

  - **enumerator MEMCACHED\_DISTRIBUTION\_CONSISTENT\_WEIGHTED**

    Weighted consistent key distribution.

  - **enumerator MEMCACHED\_DISTRIBUTION\_VIRTUAL\_BUCKET**

    Consistent key distribution by virtual buckets.

## DESCRIPTION

**libmemcached** behavior can be modified by using **memcached\_behavior\_set()**. Default behavior is the library strives to be quick and accurate. Some behavior, while being faster, can also result in not entirely accurate behavior (for instance, **memcached\_set()** \<#\_CPPv413memcached\_setP12memcached\_stPKc6size\_tPKc6size\_t6time\_t8uint32\_t\> will always respond with **MEMCACHED\_SUCCESS** \<#\_CPPv4N18memcached\_return\_t17MEMCACHED\_SUCCESSE\>).

**memcached\_behavior\_get()** takes a behavior **flag** and returns whether or not that behavior is currently enabled in the client.

**memcached\_behavior\_set()** changes the value of a particular option of the client. It takes both a **flag** and a **value**. For simple on or off options you just need to pass in a value of 1. Calls to **memcached\_behavior\_set()** will flush and reset all connections.

## RETURN VALUE

**memcached\_behavior\_get()** returns either the current value of the key, or 0 or 1 on simple flag behaviors (1 being enabled). **memcached\_behavior\_set()** returns failure or success.

## NOTES

The **data** argument of **memcached\_behavior\_set()** was changed in version 0.17 from taking a pointer to data value, to taking a uin64\_t.

## SEE ALSO

**[memcached(1)](/memcached/1.md) \<https://linux.die.net/man/1/memcached\>** **setsockopt(3) \<https://linux.die.net/man/3/setsockopt\>** **[libmemcached(3)](/libmemcached/3.md) \<https://linux.die.net/man/3/libmemcached\>** **[memcached\_strerror(3)](/memcached_strerror/3.md) \<https://linux.die.net/man/3/memcached\_strerror\>**
