title: Redis using 100% CPU after 6 hours of uptime
body: <p><a href="https://i.stack.imgur.com/ecRgz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ecRgz.png" alt="enter image description here"></a></p>
  
  <p>As seen in the image above, I'm experiencing performance issues with Redis after it has been running for an extended period of time; specially, 6 hours.</p>
  
  <p>I'm using Redis to cache responses from an API so I don't have to re-fetch dozens of rows on every request, and I thought - hey this should work. Apparently not.</p>
  
  <pre><code>module.exports = (app, csrfProtection, jsonParser) =&gt; {
      app.get("/api/steam/getPlayerSummaries", [steamRateLimits.getPlayerSummaries, jsonParser, csrfProtection], async(req, res) =&gt; {
          redisClient.hmget("steam_user", req.query.steamids, (err, reply) =&gt; {
              if(err) return res.status(500).send({ message: `Could not read from Redis with error ${err}`});
  
              redisClient.hmget(`steam_user_expires_${req.query.steamids}`, "expires", (err, expire) =&gt; {
                  if(err) return res.status(500).send({ message: `Could not read from Redis with error ${err}`});
  
                  if(reply[0] &amp;&amp; (CURRENT_UNIX_TIME - expire &lt; REDIS_EXPIRE_LIMIT)) return res.status(200).send(JSON.parse(reply));
  
                  const queryParams = querystring.stringify({
                      key: process.env.STEAM_API_KEY,
                      steamids: req.query.steamids
                  }),
                  options = {
                      headers: {
                          "Content-Type": "application/x-www-form-urlencoded"
                      }
                  };
  
                  http.get(`http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?${queryParams}`, options, response =&gt; {
                      const { statusCode } = response;
  
                      if(statusCode != 200) {
                          response.resume();
                          return res.status(statusCode).send({ message: `Could not fetch Steam API getPlayerSummaries with status code ${statusCode}`});
                      }
  
                      response.setEncoding("utf8");
                      let rawData = "";
  
                      response.on("data", chunk =&gt; rawData += chunk);
                      response.on("end", _ =&gt; {
                          try {
                              const parsedData = JSON.parse(rawData);
  
                              redisClient.hmset(["steam_user", req.query.steamids, rawData]);
                              redisClient.hmset([`steam_user_expires_${req.query.steamids}`, "expires", CURRENT_UNIX_TIME]);
  
                              return res.status(200).send(parsedData);
                          } catch (e) {
                              console.error(e.message);
                          }
                      });
                  }).on("error", e =&gt; {
                      console.error(`Got error: ${e.message}`);
                      return res.status(500).send(e);
                  });
              });
          });
      });
  }
  </code></pre>
  
  <p>Here's the progress overview of what happens in chronological order when a user tries to access this route:</p>
  
  <ol>
  <li>User loads the index (default) webpage. This page can have over 20
  rows that need to run 20 API queries from the remote API (one of the
  reasons I cache this).</li>
  <li>Check if user is already cached in redis</li>
  <li>Check if there exists an expiration key for said steam user (even if it doesn't exist).</li>
  <li>If cache exists and it was fetched in less than 15 minutes ago, display result from Redis cache.</li>
  <li>If cache doesn't exist, we query the remote API.</li>
  <li>If request was successful, cache data into Redis and return response.</li>
  </ol>
  
  <p>For some reason this gets caught up in itself and after some time running Redis uses so much CPU and RAM it's insane and I thought I was dealing with this properly.</p>
  
  <p>I am also clearing ALL cache once a day using a shell script and crontab.</p>
  
  <pre><code>redis-cli KEYS "steam_user*" | xargs redis-cli DEL
  </code></pre>
  
  <p>Why does it continue to use up more CPU and RAM (even if there is literally no visitors, and I'm scared to think what would happen if there were a lot of visitors), and how can I work around this issue to fix it?</p>