3
19 Comments

How did you speed up the response time of your API?

I know cache system and sql/db query optimisation but apart from these two, what else did you do to speed up response time of your APIs? Please share your experiences.

It will be helpful for all of us. ๐Ÿ˜Š

posted to Icon for group Developers
Developers
on October 8, 2023
  1. 2

    Forget about changing framework or language. Consider using New Relic or something like this to track every time consuming activity on your application. Also I would consider using db indexes, and compare queue against async/await flows, what works better in each situation. Algorithm optimization takes bigger place than language and framework in terms of time consuming.

    1. 1

      Hi @SpecterSpy, DB indexes helped me a lot to make a difference. I will surely look into optimising algorithm. Any links to share if you have? Thanks

      1. 2

        @nitin_k The book Refactoring, by Martin Fowler.

      2. 1

        This comment was deleted 2 years ago.

  2. 2

    The biggest method for me is to "do everything at the same time".
    Suppose you want to handle signing up and you want to do the following:

    1. Hash the password
    2. Redraw the image to make sure it's an image and not a virus
    3. Check if that email already exists.

    Most people do them in order. I would (I use nodejs) use -> await Promise.all().
    I would simply pass the array of all promises into Promise.all so everything is done at the same time instead of awaiting each one.

    That is the way I speed things up.

    1. 1

      Hi @Virock,
      I tried so many things at other major changes like db, those were working but impact was in code.
      Your suggestion really helped me as i am doing all the operations parallelly, god bless you!

    2. 1

      Hi @Virock thanks this seems to be good!
      Can you share any example/github so i can check the implementation?

  3. 2

    Some ideas that come to my mind:

    1. Which framework do you use? Sometimes frameworks add some overhead, like runtime schema checking or slow JSON serialization.
    2. It might also be useful to use a profiling tool to optimize the executed code, esp. if a lot of data is loaded from a DB and processed.
    3. Use pagination and limits - add 'start', 'offset' to your APIs (for some APIs, streaming might be appropriate)
    4. Network - Check the latency of all downstream services and see if moving them "closer" to your API server can help

    Hope this helps!

    1. 1

      Hey @eyalzh thanks for writing.
      Yes it will surely help.
      I will surely check for point 3 & 4.

  4. 1

    There are a couple of tips for reducing the API response time.

    1. Make sure indexes are effective. This is crucial when you are querying against a database especially it's large.
    2. Try to optimize algorithm complexity. When you are computing in the backend, you can try to cache things in memory and avoid multiple for loops.
    3. Use async instead of sync requests. Some long tasks are running in the background, then you can return responses in time instead of waiting.
  5. 1

    Response time also depends on hosting/cloud plan and location of user.

  6. 1

    Usually not caching, missing database indexes, and/or calling external APIs are the top bottlenecks for most applications.

  7. 1

    For me, it's always using a faster language. Golang to be precise. Between 50 - 500x.

  8. 1

    What stack are you using? Do you know where your bottle neck is? Are you doing some processing before returning the data? I'd start with these questions to be able to give you a direction of where to look

    1. 1

      Hi @0xconstantin, thanks for responding.
      stack: node js + mongodb
      Yes i am making smtp connections.
      Most time consuming part for my api is SMTP connection.
      I am trying to figure out how to speed up this.

      1. 1

        Is there no possibility to run that connection in a background task?

        1. 1

          I have been thinking of handling it in multiple smtp connections to distribute load. Will it work?

          1. 1

            It is a possibility, could you describe a little bit the flux of this API and SMTP connection? I feel like a doctor asking questions to get a diagnostic ๐Ÿ˜‚

      2. 1

        Outbound, or inbound?

        1. 1

          Outbound..

  9. 1

    This comment was deleted 2 years ago.

Trending on Indie Hackers
Iย spent $0 on marketingย and got 1,200ย website visitors -ย Here's my exact playbook User Avatar 28 comments Why Early-Stage Founders Should Consider Skipping Prior Art Searches for Their Patent Applications User Avatar 21 comments Codenhack Beta โ€” Full Access + Referral User Avatar 19 comments I built eSIMKitStore โ€” helping travelers stay online with instant QR-based eSIMs ๐ŸŒ User Avatar 18 comments Day 6 - Slow days as a solo founder User Avatar 12 comments How AI-first almost ruined everything User Avatar 8 comments