Search code examples
javascriptnode.jsrestinstances

Do Node.js object instantiaton create one object per user?


So I'm working on this tutorial to learn how to make a RESTful API in Node.js, and one part of it suddenly got me a bit worried. In the app an object is instantiated to handle the RESTful requests called TaskRepository().

As per Gist related to the tutorial, you'll see this code snippet:

var taskRepository = new TaskRepository();

My question is, will this instantiate one TaskRepository() object per user? In that case, isn't there a chance you'll run rather quickly out of memory if there's high enough traffic?

What's best practice here?

Also, if that is the case, how would you get around it programmatically to avoid a future traffic jam?


Solution

  • In that specific API, there is an API to create a task and it returns a task ID. That task will exist until some future API call refers to that specific ID and uses the delete operation.

    The TaskRepository is per server (created once for your server), not per-user.

    These tasks are not particularly per-user, but when you create a task and return it to the requestor, it is only that requestor that will likely know the ID and use it, though since this example does not create random IDs, they are predictable so anyone could reference a specific ID.

    If you do not delete tasks after they are created, they will accumulate over time. Usually, something like this would create some sort of inactivity timeout and would automatically delete tasks if they are not used in some period of time (say 30 minutes).