Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 17, 2024 01:39 pm GMT

Caching RESTful API requests with Herokus Redis Add-on

Most software developers encounter two main problems: naming things, caching, and off-by-one errors.

In this tutorial, well deal with caching. Well walk through how to implement RESTful request caching with an open source-licensed version of Redis. Well also set up and deploy this system easily with Heroku.

For this demo, well build a Node.js application with the Fastify framework, and well integrate caching with Redis to reduce certain types of latency.

Ready to dive in? Lets go!

Node.js + Fastify + long-running tasks

As Im sure our readers know, Node.js is a very popular platform for building web applications. With its support for JavaScript (or TypeScript, or both at the same time!), Node.js allows you to use the same language for both the frontend and the backend of your application. It also has a rich event loop that makes asynchronous request handling more intuitive.

The concurrency model in Node.js is very performant, able to handle upwards of 15,000 requests per second. But even then, you might still run into situations where the request latency is unacceptably high. Well show this with our application.

As you follow along, you can always browse the codebase for this mini demo at my GitHub repository.

Initialize the basic application

By using Fastify, you can quickly get a Node.js application up and running to handle requests. Assuming you have Node.js installed, youll start by initializing a new project. Well use npm as our package manager.

After initializing a new project, we will install our Fastify-related dependencies.

~/project$ npm i fastify fastify-cli fastify-plugin

Then, we update our package.json file to add two scripts and turn on ES module syntax. We make sure to have the following lines:

  "type": "module",  "main": "app.js",  "scripts": {    "start": "fastify start -a 0.0.0.0 -l info app.js",    "dev": "fastify start -p 8000 -w -l info -P app.js"  },

From there, we create our first file (routes.js) with an initial route:

// routes.jsexport default async function (fastify, _opts) {  fastify.get("/api/health", async (_, reply) => {    return reply.send({ status: "ok" });  });}

Then, we create our app.js file that prepares a Fastify instance and registers the routes:

// app.jsimport routes from "./routes.js";export default async (fastify, opts) => {  fastify.register(routes);};

These two simple filesour application and our route definitionsare all we need to get up and running with a small Fastify service that exposes one endpoint: /api/health. Our dev script in package.json is set to run the fastify-cli to start our server on localhost port 8000, which is good enough for now. We start up our server:

~/project$ npm run dev

Then, in another terminal window, we use curl to hit the endpoint:

~$ curl http://localhost:8000/api/health {"status":"ok"}

Add a simulated long-running process

Were off to a good start. Next, lets add another route to simulate a long-running process. This will help us gather some latency data. In routes.js, we add another route handler within our exported default async function:

 fastify.get("/api/user-data", async (_, reply) => {    await sleep(5000);    const userData = readData();    return reply.send({ data: userData });  });

This exposes another endpoint: /api/user-data. Here, we have a method to simulate reading a lot of data from a database (readData) and a long-running process (sleep). We define those methods in routes.js as well. They look like this:

import fs from "fs";function readData() {  try {    const data = fs.readFileSync("data.txt", "utf8");    return data;  } catch (err) {    console.error(err);  }}function sleep(ms) {  return new Promise((resolve) => {    setTimeout(resolve, ms);  });}

With our new route in place, we restart our server (npm run dev).

Measure latency with curl

How do we measure latency? The simplest way is to use curl. Curl captures various time profiling metrics when it makes requests. We just need to format curls output so that we can easily see the various latency values available. To do this, we define the output we want to see with a text file (curl-format.txt):

    time_namelookup:  %{time_namelookup}s
time_connect: %{time_connect}s
time_appconnect: %{time_appconnect}s
time_pretransfer: %{time_pretransfer}s
time_redirect: %{time_redirect}s
time_starttransfer: %{time_starttransfer}s
------------------- ----------
time_total: %{time_total}s

With our output format defined, we can use it with our next curl call:

curl -w "@curl-format.txt" \     -o /dev/null -s \     "http://localhost:8000/api/user-data"

The response we receive looks like this:

  time_namelookup:  0.000028s        time_connect:  0.000692s     time_appconnect:  0.000000s    time_pretransfer:  0.000772s       time_redirect:  0.000000s  time_starttransfer:  5.055683s                       ----------          time_total:  5.058479s

Well, thats not good. Over five seconds is way too long for a transfer time (the time it takes the server to actually handle the request). Imagine if this endpoint was being hit hundreds or thousands of times per second! Your users would be frustrated, and your server may crash under the weight of continually re-doing this work.

Redis to the rescue!

Caching your responses is the first line of defense to reduce your transfer time (assuming youve addressed any of the poor programming practices that might be causing the latency!). So, lets assume weve done everything we can do to reduce latency, but our application still needs five seconds to put this complex data together and return it to the user.

In our scenario, because the data is the same every time for every request to /api/user-data, we have a perfect candidate for caching. With caching, well perform the necessary computation once, cache the result, and return the cached value for all subsequent requests.

Redis is a performant, in-memory key/value store, and its a common tool used for caching. To leverage it, we first install Redis on our local machine. Then, we need to add Fastifys Redis plugin to our project:

~/project$ npm i @fastify/redis

Register the Redis plugin with Fastify

We create a file, redis.js, which configures our Redis plugin and registers it with Fastify. Our file looks like this:

// redis.jsconst REDIS_URL = process.env.REDIS_URL || "redis://127.0.0.1:6379";import fp from "fastify-plugin";import redis from "@fastify/redis";const parseRedisUrl = (redisUrl) => {  const url = new URL(redisUrl);  const password = url.password;  return {    host: url.hostname,    port: url.port,    password,  };};export default fp(async (fastify) => {  fastify.register(redis, parseRedisUrl(REDIS_URL));});

Most of the lines in this file are dedicated to parsing a REDIS_URL value into a host, port, and password. If we have REDIS_URL set properly at runtime as an environment variable, then registering Redis with Fastify is simple. After configuring our plugin, we just need to modify app.js to use it:

// app.jsimport redis from "./redis.js";import routes from "./routes.js";export default async (fastify, opts) => {  fastify.register(redis);  fastify.register(routes);};

Now we have access to our Redis instance by referencing fastify.redis anywhere within our app.

Modify our endpoint to use caching

With Redis in the mix, lets change our /api/user-data endpoint to use caching:

  fastify.get("/api/user-data", async (_, reply) => {    const { redis } = fastify;    // check if data is in cache    const data = await redis.get("user-data", (err, val) => {      if (val) {        return { data: val };      }      return null;    });    if (data) {      return reply.send(data);    }    // simulate a long-running task    await sleep(5000);    const userData = readData();    // add data to the cache    redis.set("user-data", userData);    return reply.send({ data: userData });  });

Here, you see that weve hardcoded in Redis a single key, user-data, and stored our data under that key. Of course, our key could be a user ID or some other value that identifies a particular type of request or state. Also, we could set a timeout value to expire our key, in the case that we expect data to change after a certain window of time.

If there is data in the cache, then well return it and skip all the time-consuming work. Otherwise, do the long-running computation, add the result to the cache, and then return it to the user.

What do our transfer times look like after hitting this endpoint two more times (the first one to add the data into the cache, and the second one to retrieve it)?

   time_namelookup:  0.000023s        time_connect:  0.000560s     time_appconnect:  0.000000s    time_pretransfer:  0.000729s       time_redirect:  0.000000s  time_starttransfer:  0.044512s                       ----------          time_total:  0.047479s

Much better! Weve reduced our request times from several seconds to milliseconds. Thats a huge improvement in performance!

Redis has many more features that may be useful here, including having key/value pairs timeout after a certain amount of time; thats a more common scenario in production environments.

Using Redis in your Heroku deployment

Up to this point, weve only shown how this works in a local environment. Now, lets go one step further and deploy it all to the cloud. Fortunately, Heroku provides many options for deploying web applications and working with Redis. Lets walk through how to get set up there.

After signing up for a Heroku account and installing their CLI tool, were ready to create a new app. In our case, well call our app fastify-with-caching. Here are our steps:

Step 1: Login to Heroku

~/projects$ heroku login...Logging in... done

Step 2: Create the Heroku app

When we create our Heroku app, well get back our Heroku app URL. We take note of this because well use it in our subsequent curl requests.

~/project$ heroku create -a fastify-with-cachingCreating  fastify-with-caching... donehttps://fastify-with-caching-3e247d11f4ad.herokuapp.com/ | https://git.heroku.com/fastify-with-caching.git

Step 3: Add the Redis add-on

We need to set up a Redis add-on that meets our applications needs. For our demo project, its sufficient to create a Mini-tier Redis instance:

~/project$ heroku addons:create heroku-redis:mini -a fastify-with-cachingCreating heroku-redis:mini on  fastify-with-cachingredis-transparent-98258 is being created in the background.

Spinning up the Redis instance may take two or three minutes. We can check the status of our instance periodically:

~/project$ heroku addons:info redis-transparent-98258...State:        creating

Not too long after, we see this:

State:        created

Were just about ready to go!

When Heroku spins up our Redis add-on, it also adds our Redis credentials as config variables attached to our Heroku app. We can run the following command to see these config variables:

~/project$ heroku config -a fastify-with-caching=== fastify-with-caching Config VarsREDIS_TLS_URL: rediss://:p171d98f7696ab7eb2319f7b78083af749a0d0bb37622fc420e6c1205d8c4579c@ec2-18-213-142-76.compute-1.amazonaws.com:15940REDIS_URL:     redis://:p171d98f7696ab7eb2319f7b78083af749a0d0bb37622fc420e6c1205d8c4579c@ec2-18-213-142-76.compute-1.amazonaws.com:15939

(Your credentials, of course, will be unique and different from what you see above.)

Notice that we have a REDIS_URL variable all set up for us. Its a good thing our redis.js file is coded to properly parse an environment variable called REDIS_URL.

Step 4: Create a Heroku remote

Finally, we need to create a Heroku remote in our git repo so that we can easily deploy with git.

~/project$ heroku git:remote -a fastify-with-cachingset git remote heroku to https://git.heroku.com/fastify-with-caching.git

Step 5: Deploy!

Now, when we push our branch to our Heroku remote, Heroku will build and deploy our application.

~/project$ git push heroku main...remote: Building source:remote: remote: -----> Building on the Heroku-22 stackremote: -----> Determining which buildpack to use for this appremote: -----> Node.js app detectedremote:        remote: -----> Creating runtime environment...remote: -----> Compressing...remote:        Done: 50.8Mremote: -----> Launching...remote:        Released v4remote:        https://fastify-with-caching-3e247d11f4ad.herokuapp.com/ deployed to Herokuremote: remote: Verifying deploy... done.

Our application is up and running. Its time to test it.

Test our deployed application

We start with a basic curl request to our /api/health endpoint:

$ curl https://fastify-with-caching-3e247d11f4ad.herokuapp.com/api/health{"status":"ok"}

Excellent. That looks promising.

Next, lets send our first request to the long-running process and capture the latency metrics:

$ curl \  -w "@curl-format.txt" \  -o /dev/null -s \  https://fastify-with-caching-3e247d11f4ad.herokuapp.com/api/user-data     time_namelookup:  0.035958s        time_connect:  0.101336s     time_appconnect:  0.249308s    time_pretransfer:  0.249389s       time_redirect:  0.000000s  time_starttransfer:  5.384986s  -------------------  ----------          time_total:  6.554382s

When we send the same request a second time, heres the result:

$ curl \  -w "@curl-format.txt" \  -o /dev/null -s \  https://fastify-with-caching-3e247d11f4ad.herokuapp.com/api/user-data     time_namelookup:  0.025807s        time_connect:  0.091763s     time_appconnect:  0.236050s    time_pretransfer:  0.236119s       time_redirect:  0.000000s  time_starttransfer:  0.334859s  -------------------  ----------          time_total:  1.276264s

Much better! Caching allows us to bypass the long-running processes. From here, we can build out a much more robust caching mechanism for our application across all our routes and processes. We can continue to lean on Heroku and Herokus Redis add-on when we need to deploy our application to the cloud.

Bonus Tip: Clearing the cache for future tests

By the way, if you want to test this more than once, then you may occasionally need to delete the user-data key/value pair in Redis. You can use the Heroku CLI to access the Redis CLI for your Redis instance:

~$ heroku redis:cli -a fastify-with-cachingConnecting to redis-transparent-98258 (REDIS_TLS_URL, REDIS_URL):ec2-18-213-142-76.compute-1.amazonaws.com:15940> DEL user-data1

Conclusion

In this tutorial, we explored how caching can greatly improve your web service's response time in cases where identical requests would produce identical responses. We looked at how to implement this with Redis, the industry-standard caching tool. We did this all with ease within a Node.js application that leverages the Fastify framework. Lastly, we deployed our demo application to Heroku, using their built-in Heroku Data for Redis instance management to cache in the cloud.

Happy coding!


Original Link: https://dev.to/heroku/caching-restful-api-requests-with-herokus-redis-add-on-3bg8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To