Start learning free

LESSON 154 OF 173 · BACKEND: SHIPPING IT

Health and Readiness

Practice

On a server, the shop usually runs as several copies behind a load balancer, which shares the visitors' requests between them. Every few seconds, the load balancer and the platform that starts the copies ask each copy two questions over HTTP.

Health: are you alive? If a copy doesn't answer 200, the platform restarts it, so keep this check simple: a restart can't fix a database problem. Readiness: can you serve requests right now? If a copy doesn't answer 200, the load balancer sends it no requests until it does. Here you really try what the app needs, and answer 503 Service Unavailable when it fails.

For example, an app that keeps uploads in a folder could check that the folder is there:

app.get("/uploads-ok", (req: Request, res: Response) => {
  try {
    fs.readdirSync("uploads");
  } catch (error) {
    return res.status(503).json({ uploads: "missing" });
  }
  res.json({ uploads: "ok" });
});

The check plays the load balancer: it asks /health and /ready, takes away the disk with shop.db, asks again, then brings the disk back. When your code is done, it prints:

Every few seconds, each copy of the shop is asked:
GET /health -> 200 {"status": "ok"} -> alive, keep it running
GET /ready -> 200 {"status": "ready"} -> ready, the load balancer sends it requests
The disk with shop.db is gone:
GET /health -> 200 {"status": "ok"} -> alive, keep it running
GET /ready -> 503 {"status": "not ready"} -> not ready, the load balancer sends it no requests for now
The disk is back:
GET /ready -> 200 {"status": "ready"} -> ready, the load balancer sends it requests

Your task: add GET /health, which answers { status: "ok" }, and GET /ready, which runs SELECT COUNT(*) FROM products on shop.db and answers { status: "ready" }, or { status: "not ready" } with status 503 when the query throws an error. Type each route's function with Request and Response, as usual.


Assignment
Follow the Coding Tutorial to add the /health and /ready routes.


Hint
Don't check the database in /health. In /ready, run a query, not just a connection: when shop.db is missing, new DatabaseSync quietly makes a new, empty file. You don't need the query's answer, so there is no row to type with as. Put return in front of the 503 answer, so the route doesn't go on to answer ready as well.


STEP 1 OF 9
    Ctrl + Enter to check