2

My main function is I am creating a link-shortening app. When someone entered a long URL, it will give a short URL. If the user clicked on the short link it will search for the long URL on the DB and redirect it to the long URL.

Meantime I want to get the click count and clicked user's OS.

I am currently using current code :

app.get('/:shortUrl', async (req, res) => {
    const shortUrl = await ShortUrl.findOne({short: req.params.shortUrl})
    if (shortUrl == null) return res.sendStatus(404)

    res.redirect(shortUrl.full)
})

findOne is finding the Long URL on the database using ShortID. I used mongoDB here

My questions are :

  1. Are there multiple redirect methods in JS?
  2. Is this method work if there is a high load?
  3. Any other methods I can use to achieve the same result?
  4. What other facts that matter on redirect time
  5. What is 'No Redirection Tracking'?

This is a really long question, Thanks to those who invested their time in this.

Siyum
  • 151
  • 8
  • 1
    `process` is your Node process, not the client. – tadman Dec 16 '22 at 17:51
  • 1
    That's a lot of questions for one question. What do you mean by "high load"? We don't know what `findOne` does, or how that table is indexed, but that's your most likely failure point. – tadman Dec 16 '22 at 17:52
  • @tadman noted that – Siyum Dec 16 '22 at 17:52
  • 2
    Strictly speaking there are **no** redirect methods in JavaScript. There are libraries etc for Node, but a redirect is a pretty simple HTTP process and the network latency is by far the biggest part of redirect performance. – Pointy Dec 16 '22 at 17:53
  • 1
    "No Redirection Tracking" is something done client-side as part of the link which suppresses relaying of the "Referer" [sic] header. – tadman Dec 16 '22 at 17:53
  • @tadman, I mean a high load if 1000 users use the service at the same time.findOne is find the long URL on DB (I used mongoDb) – Siyum Dec 16 '22 at 18:01
  • 1
    That's not really "high". 1000TPS is trivial for a database even on middling hardware, especially if it's just a quick indexed lookup and single row retrieval. If you're curious about performance, develop a useful *benchmarking tool* and find out what the performance characteristics are like. At 1ms per request, which is surely something MongoDB can handle, you'll be fine even at that load level With multiple connections and processes it will run even better. – tadman Dec 16 '22 at 18:03

2 Answers2

1

Are there multiple redirect methods in JS?

First off, there are no redirect methods in Javascript. res.redirect() is a feature of the Express http framework that runs in nodejs. This is the only method built into Express, though all a redirect response consists of is a 3xx (often 302) http response status and setting the Location header to the redirect location. You can manually code that just as well as you can use res.redirect() in Express.

You can look at the res.redirect() code in Express here.

The main things it does are set the location header with this:

this.location(address)

And set the http status (which defaults to 302) with this:

this.statusCode = status;

Then, the rest of the code has to do with handling variable arguments, handling an older design for the API and sending a body in either plain text or html (neither of which is required).

Is this method work if there is a high load?

res.redirect() works just fine at a high load. The bottleneck in your code is probably this line of code:

const shortUrl = await ShortUrl.findOne({short: req.params.shortUrl})

And, how high a scale that goes to depends upon a whole bunch of things about your database, configuration, hardware, setup, etc... You should probably just test how many request/sec of this kind your current database can handle.

Any other methods I can use to achieve the same result?

Sure there are. But, you will have to use some data store to look up the shortUrl to find the long url and you will have to create a 302 response somehow. As said earlier, the scale you can achieve will depend entirely upon your database.

What other facts that matter on redirect time

This is pretty much covered above (hint, its all about the database).

What is 'No Redirection Tracking'?

You can read about it here on MDN.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

Your code is ok, the only limitation is where you run it and mongodb.

I have created apps that are analytics tracker, handling billion rows per day.

I suggest you run your node code using AWS Beanstalk APP. It has low latency and scales on your needs.

And you need to put redis between your request and mongodb, you will call mongodb only if your data is not yet in redis. Mongodb has more read limitations than a straight redis instance.

Daniel Aranda
  • 6,426
  • 2
  • 21
  • 28
  • Thanks for the advice. I found that we can use the Redis as the primary database too. What if we used to like that? – Siyum Dec 17 '22 at 05:45
  • Since you created apps that are analytics trackers, can you give me some tips and tricks to collect the data of the users that clicked the link, such as OS, Carrier, Browser ...etc without impacting the redirecting time. – Siyum Dec 17 '22 at 06:11
  • 1
    Yes, use redis directly. In the request store the useragent this contains everything, browser, OS, versions, plugins, etc. Check this: https://stackoverflow.com/questions/48543405/how-can-i-get-user-info-with-node-js Reading that does not impact redirect time. Save that info in redis and post process later not in the redirect process. – Daniel Aranda Dec 17 '22 at 15:10
  • Are you recommending microservice architecture for this, or the monolithic will work? – Siyum Apr 20 '23 at 07:47