Redis · Chapter 23 of 26

Monitoring

By the end of this chapter you can read what Redis says about itself: its hit ratio, its load and its clients from INFO, the slow commands from the slow log, who sent them, what is happening right now with MONITOR and redis-cli --stat, and which keys are big. And you can write a health check that answers even when Redis does not. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis in production
  • 4 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

Gurpreet is on call for match night, and for her a Redis is a box that is fine until it is not. Last season's final went down at 7:31 pm and it took her forty minutes to learn that a dashboard had run KEYS on a million keys every ten seconds. This season she wants a dashboard of her own: how often the cache finds its page, how busy Redis is, how many clients are connected, the slowest commands and who sent them, and a health check the load balancer can trust.

Redis keeps all of this about itself, and answers it with ordinary commands. Nothing needs to be installed. Farhan's rule is that you read these numbers before you guess.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    hit-ratio.js: the cache's hit ratio

    In hit-ratio.js, hitRatio(redis) reads INFO stats and returns { hits, misses, ratio }: hits is keyspace_hits and misses is keyspace_misses, both as numbers, and ratio is hits / (hits + misses), or null when there were no reads at all.

    The Check reads it on a fresh Redis ({ hits: 0, misses: 0, ratio: null }), after 6 hits and 2 misses ({ hits: 6, misses: 2, ratio: 0.75 }), and after one more read of a missing hash field.

  2. Task 2

    slowest.js: the slowest commands

    In slowest.js, slowest(redis, n) reads the whole slow log and returns its n slowest entries, slowest first, each as { id, micros, command, client }: the entry's id, how many microseconds it ran, its arguments joined with single spaces exactly as logged (ioredis logs names in lower case, like keys team:4999*), and the client's name ('' when it has none). Entries that took equally long go newest first, the bigger id first. An empty slow log returns [].

    The Check logs more than 30 commands, so the slowest one is older than the 10 newest.

  3. Task 3

    health.js: a health check

    In health.js, health(redis, timeoutMs) (timeoutMs 500 when left out) asks Redis for PING and INFO. When both answer within timeoutMs and PING says PONG, it returns { ok, reasons, usedMemory, maxMemory, clients }: used_memory, maxmemory and connected_clients from INFO as numbers, reasons ['memory'] when maxmemory is above 0 and used memory is above 90% of it and [] otherwise, and ok true exactly when reasons is [].

    When Redis does not answer within timeoutMs, or answers with an error, it returns exactly { ok: false, reasons: ['down'] }. The Check crashes a Redis and calls health(redis, 300): it must return in well under a second.

  4. Task 4 · interview question

    Interview: who-ran-keys.js, who ran KEYS?

    In who-ran-keys.js, whoRanKeys(redis) reads the whole slow log and returns the clients that ran KEYS, each once, sorted: a client's name, or its address (like 127.0.0.1:41822) when it has no name. [] when nobody ran KEYS.

    The Check logs commands from clients named api-1, api-2 and dashboard and one with no name. Some send keys in lower case (ioredis), some KEYS in upper case; a key named keys and a SCAN whose pattern is keys are not KEYS commands.

    Farhan will ask: the slow log shows a KEYS that ran 113 ms. Why does it not show the PING that waited behind it?

What you will learn

  • INFO and its sections: keyspace_hits and keyspace_misses, instantaneous_ops_per_sec, connected_clients.
  • The hit ratio of a cache.
  • CLIENT LIST and CLIENT SETNAME: who is connected.
  • SLOWLOG: slowlog-log-slower-than, slowlog-max-len, SLOWLOG GET and its entries.
  • MONITOR, from ioredis, for a moment only.
  • redis-cli --stat and --bigkeys, in the terminal.

More in Redis in production

Build "Monitoring" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.