Sunday, 15 December 2019

How can I debug a memory leak on my Apollo GraphQL server?

I have a graphql server with multiple endpoints. It is basically just a CRUD app, so I'm honestly not sure why there's a memory leak. The only potentially leaky endpoint I have is one that uploads pics to S3.

I've been looking around and have tried taking heap snapshots and comparing them but I'm not even sure which endpoint is the culprit. This is the flow I've been following:

  1. Start the server with the --inspect flag: nodemon --inspect --exec babel-node src/index.js
  2. Take a heap snapshot before I do anything
  3. Start my front end app and hit the endpoint I think has the memory leak (the one where I upload a photo)
  4. Take a heap snapshot again and compare the two

Is this the correct flow for finding a memory leak? Is there a better way of doing this without having to guess which endpoint it is coming from? Are there perhaps tools I can use online that can help me find the source of the memory leak in production without having to guess like this? Perhaps something like Datadog or something?

Update: From Heroku's metrics, it looks like the memory usage increases every time a request is made?

enter image description here

But my src/index.js file doesn't do anything special:

import { ApolloServer, gql } from "apollo-server";
import { connectDb, models } from "./models";

import schema from "./schema";
import resolvers from "./resolvers";
import contexts from "./contexts";

const server = new ApolloServer({
  typeDefs: schema,
  resolvers,
  context: async ({ req, connection }) => {
    console.log(req.body.query);
    console.log(req.body.variables);

    const { getCurrentUser } = contexts;

    const currentUser = await getCurrentUser(req);
    return { models, currentUser };
  },
});

connectDb().then(async () => {
  server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
    console.log(`🚀  Server ready at ${url}`);
  });
});


from How can I debug a memory leak on my Apollo GraphQL server?

No comments:

Post a Comment