Real-time Monitoring

Difficulty: Intermediate

Question

What is the MONITOR command in Redis, when would you use it, and why is it risky in production?

Answer

MONITOR streams every command the Redis server processes, in real time, to the client that ran it. It is a debugging tool for answering "what is my application actually sending to Redis?", for example spotting an unexpected KEYS * or a loop that fires thousands of GETs per request. It has a real performance cost, so in production you run it only briefly, or reach for lighter tools like SLOWLOG and INFO commandstats.

What the output looks like

Each line has a Unix timestamp with microseconds, the database number and client address, then the command and its arguments:

1760258412.551203 [0 10.0.3.14:52144] "HGETALL" "cart:42"

Commands run inside a Lua script show lua in place of the client address. For security, administrative commands are not logged and sensitive arguments, such as the password in AUTH, are redacted.

Why it hurts performance

  • Every command processed must also be formatted and written to each monitoring client, which adds work on the single main thread.
  • The Redis documentation shows a single MONITOR client can noticeably reduce throughput, and running several makes it worse.
  • If the monitoring client reads slowly (for example over a VPN to your laptop), its output buffer grows on the server, consuming memory.
  • It also prints actual data, including user details or tokens in values, which is a privacy concern.

When MONITOR is the right tool

  • In development or staging, to confirm what an ORM, cache wrapper, or library is really doing.
  • In production, for a few seconds, piped through head, to catch a hot key or a chatty code path during an incident.
  • To verify that a deploy stopped sending a command, such as a removed KEYS call.

Lighter alternatives for production

NeedTool
Find slow commandsSLOWLOG GET 10 (threshold slowlog-log-slower-than, default 10000 microseconds)
Which commands dominate CPUINFO commandstats
Latency spikes and their causesLATENCY DOCTOR, LATENCY LATEST
Who is connected and what they last ranCLIENT LIST
Live ops per second overviewredis-cli --stat
Biggest or hottest keysredis-cli --bigkeys, --memkeys, --hotkeys (LFU only)
Ongoing tracingClient-side instrumentation such as OpenTelemetry spans

How to use it safely

  1. Run it from a machine close to the server, so it reads output quickly.
  2. Limit it by time or lines: timeout 5 redis-cli MONITOR or redis-cli MONITOR | head -n 500.
  3. Filter locally with grep, since the server cannot filter for you.
  4. Make sure your user has permission; ACLs can and should block it for application users.
  5. On managed services it may be disabled or restricted; use the provider's slow log and metrics instead.
Debugging with MONITOR safely. Capture a short sample, filter locally, then confirm with lighter tools.
  1. Symptom: Redis CPU high: Ops per second spiked after a deploy.
  2. Check INFO commandstats: Find which command count jumped.
  3. MONITOR for 5 seconds: Pipe through head and grep.
  4. Identify the code path: For example an N+1 GET loop.
  5. Verify with SLOWLOG: Confirm the fix after deploy.

Code examples

Capturing a short MONITOR sample

# Capture at most 5 seconds of traffic
timeout 5 redis-cli MONITOR > monitor.log

# Most frequent commands in the sample
awk '{print $4}' monitor.log | sort | uniq -c | sort -nr | head

# Most frequently accessed keys
awk '{print $5}' monitor.log | sort | uniq -c | sort -nr | head

# Lighter alternatives
redis-cli SLOWLOG GET 5
redis-cli INFO commandstats | head
redis-cli --stat

In this sample one key is read tens of thousands of times in 5 seconds, a sign it should be cached in the application process instead.

Key points

  • MONITOR streams every processed command to the client in real time.
  • It costs throughput and memory, and exposes data; use only briefly in production.
  • Administrative commands are not shown and AUTH passwords are redacted.
  • Prefer SLOWLOG, INFO commandstats, LATENCY DOCTOR and CLIENT LIST for routine monitoring.
  • Always limit the capture with timeout or head and filter locally.

Common follow-up questions

  • How does SLOWLOG decide which commands to record?
  • How would you find a hot key in production without MONITOR?
  • What does CLIENT LIST show and how would you use it to find a connection leak?
  • How would you restrict MONITOR to admins only?

Interview tips

  • Lead with the performance warning; interviewers often ask this to see if you know it is dangerous.
  • List two or three safer alternatives and when to use each.
  • A short story about catching a stray KEYS * or N+1 loop makes the answer memorable.

Concepts covered

MONITOR, SLOWLOG, Debugging, Command Tracing, Production Safety

More Redis & Caching questions