title: Concurrent AWS Lambda processing causing issues
body: <p>I am building a temperature sensor app which measures the temperature from a real-time sensor and generates an alert if the temperature is out of range. I am using AWS Lambda and AWS IOT platforms for it. An API gateway publishes the sensor data to AWS IOT which triggers a rule "sensor/temperature" which is a lambda. This lambda gets the sensor data stores it in dynamo db and then compares the data with stored temperature range and generates alerts. This is the response that goes to temperature lambda.</p>
  
  <pre><code>{
   "Timestamp": "2017-09-14 19:12:05",
   "Raw Timestamp": 1505416325.907,
   "Mac": "9c:65:f9:1b:f8:3c",
   "Diagnostics": [
      "SensorMon",
      "1/1 Entries"
   ],
   "SensorReadings": {
      "sensor-1": {
         "Door": 0,
         "Temperature": 36,
         "Battery": "3.06",
         "Humidity": 27,
         "SensorModuleId": "sensor-1"
      }
   }
  }
  </code></pre>
  
  <p>Now if the temperature is out of range I generate an alert say "out_of_range" at the 'start time' equal to 'Raw Timestamp' and set the 'Completed' attribute in Alert table to 'false' this way I check every minute the data comes if it is still 'out_of_range' and 'Completed' attribute is 'false' I don't generate a new alert. Now say the temperature is in range so I'll end the alert by setting its 'end time' as 'Raw Timestamp' and and 'Completed' attribute as 'true'. Only now I can generate a new alert. After this, I calculate the duration by doing 'end time' - 'start time'.</p>
  
  <p>Now the issue I am facing is that this works fine when data changes take place at slower rate that is temperature if out of range remains out of range for a long time and if it is in range then it remains in range for a long time. When the data changes are fast I start getting negative values of 'Duration'.</p>
  
  <p>I think I have figured out that the negative values of duration are due to a deadlock which occurs once a lambda request is still processing and in the meantime, a new request comes. Say we had temperature 'out of range' alert from start time 'x'. Now sensor shows temperature is in range at a time say 'y' (y>x) so this processing starts. Now as this processing is taking place sensor says temperature is again 'out of range' so a new alert will be generated at time say 'z' (z>y>x) saying temperature 'out of range'.</p>
  
  <p>Now the request at time 'y' is still processing. As it processes it will set the end time of both alerts at time 'x' and at time 'z' to 'y' as we consider the 'Completed' field, which will result in the duration of alert at start time 'z' to be negative.</p>
  
  <p>So can you think of a scenario where new request for lambda processing occurs only when the previous request is complete or some other way through this? Any help would be appreciated.</p>