This document provides examples of implementing effective timeout strategies in Lemline workflows. Proper timeout handling is essential for building robust workflows that gracefully handle slow or unresponsive services, preventing workflows from hanging indefinitely and ensuring predictable execution times.
Run the Examples
Complete the SQLite setup and keep its worker running. Run the commands from a second terminal in the setup directory. With the JAR, substitute java -jar lemline.jar for bin/lemline. Start the tutorial shop from the root of the Lemline source checkout; its delayMs query parameter makes an answer slow:
docker compose -f examples/tutorial-services/compose.yaml up -d
The event examples also need the tutorial publisher. Save it as tutorial-publisher.yaml and register it:
The first instance returns the stock. In the second, the shop waits 40 seconds; the HTTP client gives up after 30 and the catch returns success: false with the communication error titled Socket timeout has expired ….
Graduated Timeouts
This example shows graduated timeouts for a cache, a database and a computed fallback. Lemline accepts the timeouts but does not apply them; the fallbacks run when a call fails.
The first instance answers from the cache (source: cache), the second from the database after a cache miss (source: database). In the third, the database takes 4 seconds: the 3-second timeout is not applied, so the call waits and still answers with source: database. Only the failure of a call, not its duration, reaches the next fallback.
Timeouts with Event Waiting
These examples receive events from the tutorial publisher on the worker's SQLite channel.
Event Timeout
This workflow waits up to 30 seconds for a specific process to complete; a real process would get minutes. The producer stores each status before emitting the event. On timeout, the workflow reads the stored status so it can still recover a completion sent before the listener was registered.
The instance completes with success: true and the event data. To see the timeout path, reserve an order in the shop, which stands for the durable status store, and start a listener for it without publishing anything:
After 30 to 40 seconds the listener times out, the workflow reads the stored reservation, and completes with {"success":true,"status":"reserved","processId":"ORD-1402"}.
Multiple Event Timeout
This example demonstrates waiting for multiple events with a timeout.
The first instance has 30 minutes and finishes: the shop's audit list shows Finished with 99 units available for ORD-1403: the ORD-1402 reservation above holds one unit. The second gives itself one second; the 2-second preparation uses it up, so the workflow returns {"success":false,"reason":"Deadline reached before the final step"} without calling the final step.
Timeout Strategies for Different Services
Database Query Timeouts
This example shows timeout settings for database-style queries, here the shop's catalog. Lemline accepts them but does not apply them.
Both instances complete with queryResult: 7 and complexResult: ["SKU-1"]. In the second, the complex query takes 2 seconds: its 1-second timeout is not applied, so the fallback query does not run.
External API Timeouts
This example declares different timeouts for different types of external APIs. Lemline accepts them but does not apply them.
Both instances complete with the live product Desk lamp and source: live: the second waits 6 seconds for the shop, beyond its 5-second timeout. The cached fallback only runs if the call fails, for example after the 30-second client limit. Stop the shop when you are done:
docker compose -f examples/tutorial-services/compose.yaml down
Best Practices for Timeout Handling
When implementing timeout strategies in Lemline workflows, consider these best practices:
Appropriate Duration: Set timeout durations appropriate to the operation being performed (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)
Graduated Timeouts: Use shorter timeouts for quick operations, longer for complex ones (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)
Retry Strategy: Combine timeouts with appropriate retry strategies
Fallback Logic: Implement fallback mechanisms when operations timeout
Monitoring: Log and monitor timeout occurrences to identify patterns
Circuit Breaking: Consider implementing circuit breakers for frequently timing out services
Global Limits: Set workflow-level timeouts to prevent indefinite execution (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)
Compensating Actions: Implement cleanup actions when timeouts occur mid-process
Timeout Prediction: Monitor remaining time and adjust behavior as deadlines approach
Documentation: Document expected timeouts for each integration point
Conclusion
These examples demonstrate various approaches to implementing effective timeout handling in Lemline workflows. By combining appropriate timeout durations with retry strategies, fallback mechanisms, and cleanup actions, you can build resilient workflows that handle external service failures gracefully.
Proper timeout handling ensures that workflows remain responsive even when dependent services are slow or unresponsive, improving overall system reliability and user experience.
For more examples of error handling and resilience patterns, see the Error Handling Examples document.