DEV Community

Adil Rahiman
Adil Rahiman

Posted on Originally published at adilrahman.cc

Building a Concurrency-Safe WhatsApp & Google Sheets Pipeline with Real-Time Error Alerts

When teachers across 50+ batches finish classes at the top of the hour, dozens of attendance messages hit your webhook simultaneously.

If your backend is Google Apps Script writing to Google Sheets, simultaneous writes without concurrency locks will produce race conditions and lost data.

The Fix: LockService Mutex

function doPost(e) {
  var lock = LockService.getScriptLock();
  if (!lock.tryLock(10000)) {
    return ContentService.createTextOutput('Server busy, request locked');
  }
  try {
    var payload = JSON.parse(e.postData.contents);
    // Process, validate, and write safely
  } finally {
    lock.releaseLock();
  }
}
Enter fullscreen mode Exit fullscreen mode

Key Components of the Architecture:

  • Descending-Length Boundary Regex: Prevents substring collision when identifying batch IDs (e.g., distinguishing TU2600276 from TU260027).
  • State Validation: Differentiates continuous standard batches from tuition batches that reset monthly.
  • Two-Way Event Loop: If an invalid session is submitted, Google Apps Script makes an outbound HTTP POST to dispatch an approved Meta WhatsApp Utility warning template directly back to the sender.

Read the full case study with complete schemas and regex patterns:
https://adilrahman.cc/blog/whatsapp-automation-google-sheets-case-study?utm_source=devto

Top comments (0)