Answers: <p>In my point of view, you can have a future gain separating the function into multiple parts because it will allow to use another combining system/upload/whatever as input to the processing part and another system to process the images later.</p>
  
  <p>My personal rule of thumb for separation is the main concern:</p>
  
  <ul>
  <li>Step 1 is giving the input, it's already somethhing it itself.</li>
  <li>Steps 3 and 4 are, basically, take an input, do the work andgive an output. It waits base64 strings as input and output an image file to a bucket, it should be autonomous in its processing</li>
  <li>Step 5 is an api call, it waits image as input and "whatever vision API does" as result/output, that's a task in itself.</li>
  </ul>
  
  <p>I did omit step 2 as it's the trigger of step 3 and 4.</p>
  
  <p>The main interest into this separation of duty is the possibility to work with contracts between the functions (mostly bullet 2 and 3): 2 is expected to output images in a bucket, 3 expects to find images in a bucket, you can then replace 2 or 3 with whatever you want as long as this contract is fulfilled.</p>
  
  <p>Now for when to do this split and is it necessary, if you don't expect things to change in a near future and there's no hard limit approaching, like the time to do the whole work is approaching the max execution time, then there's no need to over anticipate problems in my humble opinion, the whole step 3 to 5 can be seen as a single task unit as you said and this make sense.</p>
  
  <p>My take would be to measure the time within the function to see if there's an interest (for parallel processing for exemple) in taking the combine and transform to image to its own unit.</p>
  
  <p>In brief to answer the question title: there's no silver bullet or hard rule for the granularity, it's always a matter of weighting the benefits toward the cost of slicing the process.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-10-03T13:56:42.250Z
Answers: Great response, thank you! I like the idea of contracts being the conceptual point of separation. At the very least, as you said, it's a good place to start a cost-benefit analysis of further refactoring/splitting.
Answers: swigganicks
Answers: CC BY-SA 4.0
Answers: 2018-10-03T11:44:20.707Z
Answers: 2018-10-03T11:44:20.707Z
Answers: Tensibai
Body: <p>For clarification, when I say "FaaS" I am referring to serverless offerings such as AWS Lambda and Google Cloud Functions. </p>
  
  <p>I have a specific case in mind that I'll describe but I would very much like to hear about any guiding principles in this area as well.</p>
  
  <p>I have a Google Cloud Function that is currently doing some image processing. The flow looks like this: </p>
  
  <ol>
  <li>JSON file containing several base64 encoded images is uploaded to storage bucket</li>
  <li>Cloud Fucntion downloads file</li>
  <li>Converts and merges all base64 strings into image file</li>
  <li>Uploads image file to storage bucket</li>
  <li>Calls Google Cloud Vision API once file is uploaded</li>
  </ol>
  
  <p>This works, but it has me wondering: how much should a single function <em>do</em>? Most discussions I see regarding single purpose vs. monolithic functions are centered around monolithic functions having logic branches e.g. "if-else". In the above case, this is an example of doing a lot, but, it's conceptually a single unit of "work". </p>
  
  <p>Were I to refactor this into true single purpose functions, I imagine I'd have two functions -- one for downloading and merging images and the other for calling the Vision API. This splits what I consider "image processing" into two sub-units. I have no problem doing this but I'm curious to know whats a good rule-of-thumb for "splitting" FaaS function?</p>
  
  <p>Edit: if anyone is wondering why the image file gets uploaded back into a storage bucket and then the Vision API is called, this due to a limitation of that API where inputs can only be storage bucket URIs and not local files</p>
  
Tags: <aws-lambda><serverless>
Title: How granular in purpose should FaaS functions be?
UserDisplayName: swigganicks
ThreadText: # How granular in purpose should FaaS functions be?
  
  (Asked by: username_0 on 2018-10-03)
  
  For clarification, when I say "FaaS" I am referring to serverless offerings such as AWS Lambda and Google Cloud Functions.
  
  I have a specific case in mind that I'll describe but I would very much like to hear about any guiding principles in this area as well.
  
  I have a Google Cloud Function that is currently doing some image processing. The flow looks like this:
  
  1. JSON file containing several base64 encoded images is uploaded to storage bucket
  2. Cloud Fucntion downloads file
  3. Converts and merges all base64 strings into image file
  4. Uploads image file to storage bucket
  5. Calls Google Cloud Vision API once file is uploaded
  
  This works, but it has me wondering: how much should a single function *do*? Most discussions I see regarding single purpose vs. monolithic functions are centered around monolithic functions having logic branches e.g. "if-else". In the above case, this is an example of doing a lot, but, it's conceptually a single unit of "work".
  
  Were I to refactor this into true single purpose functions, I imagine I'd have two functions -- one for downloading and merging images and the other for calling the Vision API. This splits what I consider "image processing" into two sub-units. I have no problem doing this but I'm curious to know whats a good rule-of-thumb for "splitting" FaaS function?
  
  Edit: if anyone is wondering why the image file gets uploaded back into a storage bucket and then the Vision API is called, this due to a limitation of that API where inputs can only be storage bucket URIs and not local files
  
  **Tags:** aws-lambda, serverless
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-03. Score: 2 | Accepted answer
  
  In my point of view, you can have a future gain separating the function into multiple parts because it will allow to use another combining system/upload/whatever as input to the processing part and another system to process the images later.
  
  My personal rule of thumb for separation is the main concern:
  
  * Step 1 is giving the input, it's already somethhing it itself.
  * Steps 3 and 4 are, basically, take an input, do the work andgive an output. It waits base64 strings as input and output an image file to a bucket, it should be autonomous in its processing
  * Step 5 is an api call, it waits image as input and "whatever vision API does" as result/output, that's a task in itself.
  
  I did omit step 2 as it's the trigger of step 3 and 4.
  
  The main interest into this separation of duty is the possibility to work with contracts between the functions (mostly bullet 2 and 3): 2 is expected to output images in a bucket, 3 expects to find images in a bucket, you can then replace 2 or 3 with whatever you want as long as this contract is fulfilled.
  
  Now for when to do this split and is it necessary, if you don't expect things to change in a near future and there's no hard limit approaching, like the time to do the whole work is approaching the max execution time, then there's no need to over anticipate problems in my humble opinion, the whole step 3 to 5 can be seen as a single task unit as you said and this make sense.
  
  My take would be to measure the time within the function to see if there's an interest (for parallel processing for exemple) in taking the combine and transform to image to its own unit.
  
  In brief to answer the question title: there's no silver bullet or hard rule for the granularity, it's always a matter of weighting the benefits toward the cost of slicing the process.
  
  > **Comments:**
  > **username_0**: Great response, thank you! I like the idea of contracts being the conceptual point of separation. At the very least, as you said, it's a good place to start a cost-benefit analysis of further refactoring/splitting.  
  
  ---
  
  