XLEN(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ XLEN ✨

okay bestie, this is XLEN, a little command from the valkey manual, and it's literally the simplest glow-up in the whole stream family πŸ’–

🌸 NAME 🌸

xlen β€” bestie, it counts the messages chillin' in a stream. that's it. that's the whole job πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

XLEN key

one argument. one key. iconic, no notes ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so XLEN returns the number of entries inside a stream. periodt.

if the key you handed it doesn't exist, the command returns zero, like the stream was empty all along. cute, right? πŸ™ˆ but here's the plot twist β€” and listen closely because this is the part that gets people:

unlike other Valkey types, zero-length streams are possible. so if you actually need to know whether a key exists or not, you gotta call TYPE or EXISTS. don't just trust the zero. the zero can lie to you fr fr 😭

and another thing, bestie β€” streams do NOT auto-delete just because they got no entries left inside. like, even after an XDEL call wipes everything out, the stream is still there, still standing, because it might have consumer groups attached to it and those groups have feelings. main character energy honestly. the stream stays. πŸ‘‘


πŸ’œ REPLY πŸ’œ

valkey-protocol(7) Integer reply: the number of entries of the stream at key. just a number. a whole number. gorgeous. ⭐


🌈 COMPLEXITY 🌈

O(1)

constant time, baby!! it does not care how big your stream is, it just knows. that's the confidence we're talking about πŸ’…βœ¨


πŸ’– ACL CATEGORIES πŸ’–

  • @fast
  • @read
  • @stream

πŸ“ HISTORY πŸ“

  • Available since: 5.0.0

a legend since day one, no cap 🫢


🌸 EXAMPLES 🌸

okay watch this, we're gonna build a tiny stream and then count it:

127.0.0.1:6379> XADD mystream * item 1
"1714701492011-0"
127.0.0.1:6379> XADD mystream * item 2
"1714701492021-0"
127.0.0.1:6379> XADD mystream * item 3
"1714701492031-0"
127.0.0.1:6379> XLEN mystream
(integer) 3

three adds, three entries, and XLEN said (integer) 3. we ate that fr πŸ”₯


🧚 SEE ALSO 🧚

the whole stream squad, all linked, all iconic πŸ’…


okay that's the whole page bestie, and before you go β€” this one's from version 9.0.0, dated 2025-10-21 πŸŽ€ go forth and count your messages, you absolute legend πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from