1

I am developing a service which figures out the freights and frequencies to deliver a product to the customer.

The flow goes as below-

  1. I receive a request for a one item
  2. For every item I have 5 different Time Windows to check
  3. For every Time Window, I have 21 different days to check for weekly frequencies.

If I try to achieve this without spawning threads my service takes around 1 second to compute all the desired functionality.

To reduce the response time, I have built the service by spawning threads at different levels as below -

  1. Spawned threads for every item in the request
  2. Spawned threads per item per window (5 windows would correspond to 5 threads)
  3. Spawned threads per date per window (21 dates would correspond to 21 threads)

So if I get a single item in the request, I would spawn in total of (1x5x21= 105 threads). Does this look correct? Should I worry running out of heap space or Context Switching Time?

Any leads would be helpful. Thanks in advance!!

Bhaskar
  • 337
  • 6
  • 21
  • 1
    What is it about the "checking" that takes all the time? Are you searching in a database (what kind)? An in-memory data structure (what kind)? – Christopher Schultz Nov 15 '16 at 14:47
  • I'd second Christopher. First find out where the bottleneck is. Would be a waste of time if there can be nothing won with that many Threads. – Fildor Nov 15 '16 at 14:50
  • What leads you to think multi threading will help (and not make it worse)? Have you done any profiling of the existing code? – Andrew S Nov 15 '16 at 14:53
  • 1
    Spawning 5 threads will possibly help, but you don't need a separate thread for each date, it's too much in my opinion. Start the profiler and check your code. – Shadov Nov 15 '16 at 15:08
  • Thanks for your comments! I tried to profile my code and reduced the number of db calls I was trying to make. So I have reduced the spawning of threads from 105 to 5. Thanks for all your suggestions! – Bhaskar Nov 16 '16 at 06:15
  • But I would be still curious in understanding what wrong could happen if I spawn lot of threads?? In my previous approach though I was spawning around 100 threads my response time was a few ms which was wonderful. – Bhaskar Nov 16 '16 at 06:22

0 Answers0