Memcac...ehavior(3pm)

✨ Memcac...ehavior(3pm) β€” the behavior page, but make it make sense ✨

okay so this is straight out of the Memcac...ehavior(3pm) corner of the User Contributed Perl Documentation, and it's literally just two little functions that let you flip all of libmemcached's vibes on and off. part 1 of 2, so don't go anywhere bestie 🫢

πŸ“› NAME πŸ“›

memcached_behavior_get, memcached_behavior_set β€” manipulate behavior πŸ’…

πŸ“š LIBRARY πŸ“š

C Client Library for memcached (libmemcached, -lmemcached). yes we're in C land, bring a snack πŸ“

πŸŽ€ SYNOPSIS πŸŽ€

#include <memcached.h>

uint64_t
  memcached_behavior_get (memcached_st *ptr,
                          memcached_behavior flag);

memcached_return_t
  memcached_behavior_set (memcached_st *ptr,
                          memcached_behavior flag,
                          uint64_t data);

🌸 DESCRIPTION 🌸

okay so libmemcached(3)'s behavior is fully customizable β€” you poke at it with memcached_behavior_set(). By default the library is out here striving to be quick and accurate, which is honestly iconic of her. but some behaviors trade accuracy for speed, and that's a whole tradeoff you're choosing. for example, if you turn on that energy, memcached_set() will always respond with "MEMCACHED_SUCCESS" β€” like she's manifesting. no cap. 😭

memcached_behavior_get() takes a behavior flag and tells you whether that behavior is currently enabled in the client. that's it. that's the function.

memcached_behavior_set() changes the value of a particular option on the client. it wants a flag (all the ones listed below, bestie) and a value. for the simple on-or-off options you literally just pass in a value of 1 and you're done. πŸ’…

⚠️ heads up: every call to memcached_behavior_set() will flush and reset all connections. every single one. so don't be out here toggling flags mid-conversation like it's nothing.

🧚 the flags, one by one, no skips 🧚

MEMCACHED_BEHAVIOR_USE_UDP

Makes libmemcached(3) talk to the memcached server over UDP instead of TCP. cute, but not all I/O operations are supported when this is enabled. these ones will hand you back "MEMCACHED_NOT_SUPPORTED" when executed with MEMCACHED_BEHAVIOR_USE_UDP on:

  • memcached_version()
  • memcached_stat()
  • memcached_get()
  • memcached_get_by_key()
  • memcached_mget()
  • memcached_mget_by_key()
  • memcached_fetch()
  • memcached_fetch_result()
  • memcached_value_fetch()

every other operation is supported, but they run in a 'fire-and-forget' mode β€” meaning once the client has executed the operation, there is literally no attempt made to check that the server actually received it and acted on it. sending a text and not caring if it's read energy. πŸ’€

also, libmemcached(3) does not allow TCP and UDP servers to be shared within the same libmemached(3) client 'instance'. if you try to add a TCP server while this behavior is enabled you get a "MEMCACHED_INVALID_HOST_PROTOCOL", and same story if you try to add a UDP server while this behavior has not been enabled. pick a lane, she said. 🚫

MEMCACHED_BEHAVIOR_NO_BLOCK

Makes libmemcached(3) use asynchronous IO. this is the fastest transport available for storage functions. slay. πŸ”₯

MEMCACHED_BEHAVIOR_SND_TIMEOUT

sets the microsecond behavior of the socket against the SO_SNDTIMEO flag. so if you're in a situation where you can't use non-blocking IO, this still lets you have timeouts on the sending of data. bestie has boundaries. ✨

MEMCACHED_BEHAVIOR_RCV_TIMEOUT

sets the microsecond behavior of the socket against the SO_RCVTIMEO flag. same deal as above, except it's timeouts on the reading of data when you can't use non-blocking IO. 🫢

MEMCACHED_BEHAVIOR_TCP_NODELAY

turns on the no-delay feature for connecting sockets. it may be faster in some environments. it's giving low latency. πŸ‘‘

MEMCACHED_BEHAVIOR_HASH

makes the default hashing algorithm for keys use MD5. the value can be set to either MEMCACHED_HASH_DEFAULT, MEMCACHED_HASH_MD5, MEMCACHED_HASH_CRC, MEMCACHED_HASH_FNV1_64, MEMCACHED_HASH_FNV1A_64, MEMCACHED_HASH_FNV1_32, MEMCACHED_HASH_FNV1A_32, MEMCACHED_HASH_JENKINS, MEMCACHED_HASH_HSIEH, and MEMCACHED_HASH_MURMUR.

each hash has its advantages and its weaknesses, fr fr. if you don't know and you don't care, just go with the default β€” she's default for a reason. ⭐

πŸŽ€ note: support for MEMCACHED_HASH_HSIEH is a compile time option that is disabled by default. to enable support for this hashing algorithm, configure and build libmemcached with the --enable-hash_hsieh. you gotta opt in, she's not gonna do it for you.

MEMCACHED_BEHAVIOR_DISTRIBUTION

this one lets you enable different means of distributing values to servers. the default method is MEMCACHED_DISTRIBUTION_MODULA. you can enable consistent hashing by setting MEMCACHED_DISTRIBUTION_CONSISTENT. consistent hashing delivers better distribution and lets servers be added to the cluster with minimal cache losses β€” no more whole-cache meltdowns when you scale. currently MEMCACHED_DISTRIBUTION_CONSISTENT is an alias for the value MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA. iykyk. πŸ’«

MEMCACHED_BEHAVIOR_CACHE_LOOKUPS

memcached can cache named lookups so that DNS lookups are made only once. one and done, we love efficiency. πŸ’…

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. she's expensive, but she's worth it.

MEMCACHED_BEHAVIOR_KETAMA

sets the default distribution to MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA and the hash to MEMCACHED_HASH_MD5. a whole preset, just for you. 🎁

MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED

sets the default distribution to MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA with the weighted support, and the hash to MEMCACHED_HASH_MD5. same as above but she's got weights now. main character energy. βš–οΈ

MEMCACHED_BEHAVIOR_KETAMA_HASH

sets the hashing algorithm for host mapping on continuum. the value can be set to either MEMCACHED_HASH_DEFAULT, MEMCACHED_HASH_MD5, MEMCACHED_HASH_CRC, MEMCACHED_HASH_FNV1_64, MEMCACHED_HASH_FNV1A_64, MEMCACHED_HASH_FNV1_32, and MEMCACHED_HASH_FNV1A_32.

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, which makes you compatible with the SPY Memcached client for Java. international relations, honestly. 🌈

MEMCACHED_BEHAVIOR_POLL_TIMEOUT

modify the timeout value that's used by poll(). the default value is -1. a signed int pointer must be passed to memcached_behavior_set() to change this value. for memcached_behavior_get() a signed int value will be cast and returned as the unsigned long long. yes it's a whole thing, just pass the right pointer bestie. πŸ™ˆ

MEMCACHED_BEHAVIOR_USER_DATA

this lets you store a pointer to a specific piece of data. you can retrieve it from inside of memcached_fetch_execute(). cloning a memcached_st will copy the pointer to the clone.

🚨 this was deprecated in 0.14 in favor of memcached_callback_set(3). this will be removed in 0.15. do not build your whole personality on this one, she's leaving.

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 sent to the remote connection. quitting the connection or closing down the connection will also cause the buffered data to be pushed to the remote connection. she's saving it all up for a dramatic entrance. πŸ’…

MEMCACHED_BEHAVIOR_VERIFY_KEY

enabling this will cause libmemcached(3) to test all keys to verify that they're valid keys. a little quality control never hurt nobody. βœ…

MEMCACHED_BEHAVIOR_SORT_HOSTS

enabling this will cause hosts that are added to be placed in the host list in sorted order.

⚠️ heads up: this will defeat consistent hashing. so pick one, bestie, you cannot have both. 😭

MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT

in non-blocking mode this changes the value of the timeout during socket connection. give her a deadline. ⏰

MEMCACHED_BEHAVIOR_BINARY_PROTOCOL

enable the use of the binary protocol. please note that you cannot toggle this flag on an open connection. decide before you commit, that's the rule. 🚫

MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT

set this value to enable the server to be removed after continuous MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT times connection failure. three strikes and you're out of the server list, periodt. πŸ₯Ύ

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 too high may cause libmemcached to deadlock β€” she'll be trying to send data, but the send will block because the input buffer in the kernel is full. don't do that to her. πŸ’€

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).

🚨 same warning, same drama: setting this value too high may cause libmemcached to deadlock, because it's trying to send data and the send will block since the input buffer in the kernel is full.

MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH

the binary protocol works a bit different than the textual protocol β€” a multiget is implemented as a pipe of single get-operations which are sent to the server in a chunk. if you're 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 lets the servers start processing the requests and sending data back while the rest of the requests are still being created and sent to the server. pipelining queen. πŸ”₯

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). she said "seen, don't reply." πŸ™ˆ

MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS

if you just want "a poor mans HA", you may 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 β€” instead it will store the replicas together with all of the other objects (on the 'n' next servers specified in your server list). lowkey HA, highkey fine. πŸ’œ

MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ

allows randomizing the replica reads starting point. normally the read is done from the primary server and in case of a miss the read is done from primary + 1, then primary + 2, all the way to 'n' replicas. if this option is set, the starting point of the replica reads is randomized between the servers. this allows distributing read load to multiple servers at the expense of more write traffic. tradeoffs, we love to see them. βš–οΈβœ¨

MEMCACHED_BEHAVIOR_CORK

enable TCP_CORK behavior. 🍾

⚠️ this is only available as an option on Linux.

return value drama, so you know what's up:

  • MEMCACHED_NO_SERVERS is returned if no servers are available to test with.
  • MEMCACHED_NOT_SUPPORTED is returned if we were not able to determine if support was available.
  • all other responses than MEMCACHED_SUCCESS report an error of some sort.

also, setting this behavior also enables MEMCACHED_BEHAVIOR_TCP_NODELAY. package deal. πŸŽ€

MEMCACHED_BEHAVIOR_KEEPALIVE

enable TCP_KEEPALIVE behavior. keep the connection alive, keep the friendship alive. 🫢

MEMCACHED_BEHAVIOR_KEEPALIVE_IDLE

specify time, in seconds, to mark a connection as idle.

⚠️ this is only available as an option on Linux.

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 the system default if this occurs. don't panic, she's just telling you the truth. πŸ’…

MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE

bestie this one's just a peek at the current size of SO_RCVBUF. that's it. and if it hands you back a 0? that means either an error happened or no hosts were available β€” so just assume the system default kicked in. iconic of it, honestly. πŸ’…

MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT

this is the number of times a host can throw an error before it gets disabled. like a lil patience meter. πŸ™ˆ

MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS

turn this on and any hosts that got flagged as disabled get yeeted straight out of the list of servers in the memcached_st structure. πŸ¦‹

fr fr though β€” this one must be used in combination with MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT. no solo missions. ✨

MEMCACHED_BEHAVIOR_RETRY_TIMEOUT

when it's enabled, a host that's being problematic only gets checked for usage based on the amount of time set by this behavior. so it's on a lil timeout, in the literal sense. πŸ‘‘


🌸 RETURN 🌸

  • memcached_behavior_get() hands you back either the current value of the get, or 0 or 1 on simple flag behaviors β€” and 1 means enabled, periodt. πŸ’–
  • memcached_behavior_set() returns failure or success. short n sweet.

🚨 NOTES 🚨

heads up bestie: memcached_behavior_set() in version .17 was changed from taking a pointer to data value, to taking a uin64_t. literally different. do not come for me. πŸ’€


🏠 HOME 🏠

to find out more information please check: <https://launchpad.net/libmemcached>

(no links in my heart but this one's the homie. 🫢)


πŸ‘‘ AUTHOR πŸ‘‘

Brian Aker, <brian@tangent.org>


πŸ¦‹ SEE ALSO πŸ¦‹


and that's the whole vibe check, bestie β€” perms of love to the perl crew, v5.42.3, dated 2026-08-19, and this page has been giving Memcac...ehavior(3pm) main character energy from the very start. go forth and cache responsibly. πŸŒˆβœ¨πŸ’œ

πŸ“¦ where this page comes from