Site icon JD Bots

Fixed: “Could not connect to Valkey at 127.0.0.1:6379: setsockopt(TCP_NODELAY): Invalid argument” on macOS

Featured image for the fix of setsockopt(TCP_NODELAY): Invalid argument on macOS, with a warning sign, the words Could not connect to Valkey, and the fix, brew services start valkey.

Fixed: setsockopt(TCP_NODELAY): Invalid argument on macOS.

If valkey-cli on a Mac prints the message below, Valkey® isn’t answering at that address. The words setsockopt(TCP_NODELAY) make it look like a network setting is wrong, but it’s the same problem as “Connection refused”.

Output

Could not connect to Valkey at 127.0.0.1:6379: setsockopt(TCP_NODELAY): Invalid argument

What I Was Doing

I had just installed Valkey 9.1.4 with Homebrew on an Apple silicon Mac with macOS 26, for How to Install Valkey on macOS. I stopped the service with brew services stop valkey while writing that guide’s stop and start steps, then ran valkey-cli ping.

What I Expected

A message saying that nothing was listening, such as “Connection refused”.

What I Got

Terminal

valkey-cli ping

Output

Could not connect to Valkey at 127.0.0.1:6379: setsockopt(TCP_NODELAY): Invalid argument
The message valkey-cli prints on macOS when Valkey isn’t running.

What I Checked

Valkey’s installation guide for macOS checks that Valkey is running with brew services info valkey:

Terminal

brew services info valkey

Output

valkey (sh.brew.valkey)
Running: ✘
Loaded: ✘
Schedulable: ✘
brew services info valkey shows that Valkey isn’t running.

Running: ✘ means Valkey isn’t running, so nothing answers on port 6379.

Why It Happens

valkey-cli connects to 127.0.0.1, port 6379, unless you give it another host or port. When nothing listens there, the connection fails. On macOS, Valkey’s client library mistakes that failed connection for an open one, so its next step, switching on the TCP_NODELAY option for the connection, fails with “Invalid argument”. That later error is the one it prints.

Valkey’s own tests meet the same message on macOS, where they expect “Connection refused” (valkey-io/valkey issue #5330), and a fix to the client library is open (pull request #5331). The message also appears for a port where nothing listens:

Terminal

valkey-cli -p 6380 ping

Output

Could not connect to Valkey at 127.0.0.1:6380: setsockopt(TCP_NODELAY): Invalid argument

The Fix

Start Valkey

Terminal

brew services start valkey

Output

==> Successfully started `valkey` (label: sh.brew.valkey)

Or point valkey-cli at the right port

If you gave valkey-cli a port with -p, use the port your Valkey listens on, or leave -p out. The Homebrew service uses port 6379, set in /opt/homebrew/etc/valkey.conf.

After the Fix

Terminal

valkey-cli ping

Output

PONG
After starting Valkey, valkey-cli ping answers PONG.

Valkey answers PONG, and brew services info valkey now shows Running: ✔.

Common Mistakes

MistakeWhat happensDo this instead
Changing network or firewall settings because the message names TCP_NODELAYNothing improves, because that option isn’t the problemStart Valkey, or point valkey-cli at the port Valkey uses
Running valkey-cli -p 6380 for the Homebrew serviceNothing listens on 6380, so you get the same messageLeave out -p to use port 6379

FAQ

Is this a bug in Valkey?

The wording is misleading on macOS, and Valkey tracks it in issue #5330. The situation behind it is normal: valkey-cli couldn’t reach a Valkey server.

Does this happen on Linux?

Valkey’s tests expect “Connection refused” for the same situation and fail only on macOS, so on Linux you see the usual “Connection refused” message.

Do I need to change TCP_NODELAY?

No. valkey-cli switches that option on by itself for every network connection. Nothing in your settings needs to change.

Conclusion

On macOS, “setsockopt(TCP_NODELAY): Invalid argument” from valkey-cli means nothing answered at that address and port. Check with brew services info valkey, then start Valkey or use the right port.

Additional Resources

Related

Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.

Exit mobile version