ERR Can't execute '<cmd>': only (P|S)SUBSCRIBE / (P|S)UNSUBSCRIBE / PING / QUIT / RESET are allowed in this context: Command not allowed in subscribe context
RedisWhat does it mean?
After the connection entered Pub/Sub subscribe mode, you tried to run a regular command (e.g., GET/SET) that is not allowed in this mode. A connection in subscribe mode accepts only a limited set of commands. In older versions the message was "ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context".
Why does it happen?
Sending data commands over a connection that has switched to subscribe mode with SUBSCRIBE/PSUBSCRIBE/SSUBSCRIBE. In the RESP2 protocol a subscribed connection allows only (P|S)SUBSCRIBE, (P|S)UNSUBSCRIBE, PING, QUIT and RESET. Sharing a single connection for both pub/sub and regular work is the typical mistake.
How to fix it
Use a dedicated connection for Pub/Sub and open another one for regular commands (most client libraries do this automatically). Send UNSUBSCRIBE/PUNSUBSCRIBE to leave subscribe mode; regular commands then work. In the RESP3 protocol (HELLO 3) other commands are also allowed on a subscribed connection; switch to RESP3 if your driver supports it.
More from this engine
- NOGROUP No such key or consumer groupNo such key or consumer group in stream
- NOTBUSY No scripts in execution right nowNo scripts in execution right now
- DENIED Redis is running in protected modeRedis is running in protected mode
- ERR SELECT is not allowed in cluster modeSELECT is not allowed in cluster mode
- ERR wrong number of arguments for commandWrong number of arguments for command
- ERR Background save already in progressBackground save already in progress
- ERR bit offset is not an integer or out of rangeBit offset is not an integer or out of range