Three types of Lambda Invocation

  1. Synchronous Invocation: In this scenario, an event triggers a Lambda function through API Gateway or directly invokes it. The function executes and returns a response immediately to the client that initiated the request. This is commonly used in web applications where users make direct requests, such as submitting a form or fetching data.
  2. Asynchronous Invocation: With asynchronous invocation, a function is triggered by an event source, but the caller doesn’t wait for the function to complete or receive a response. Instead, the event is sent to AWS services like EventBridge (formerly CloudWatch Events) or S3. This is ideal for scenarios where the response isn’t immediately needed or when you want to decouple components of your application.
  3. Polling: In this model, the Lambda function continuously checks a resource for new events or messages. Common sources include SQS (Simple Queue Service) or Kinesis streams. When new data is available, the Lambda function is invoked to process it. This approach is efficient for handling high-throughput data streams or processing messages from various sources.

Each of these invocation types serves different use cases and allows you to design your serverless architecture to best fit your application’s requirements, whether it’s optimizing for responsiveness, scalability, or event-driven processing.

When do we use Synchronous Invocation in AWS Serverless?

  1. API Endpoints: When building web applications or microservices, you often expose APIs to handle requests from clients. These requests might include actions like retrieving data, updating resources, or performing calculations. Synchronous invocation via API Gateway allows you to trigger Lambda functions in response to these HTTP requests and return a timely response back to the client.
  2. Real-time Processing: In some cases, you may require real-time processing of events or data. For example, if you’re building a chat application, you might use Lambda functions to process incoming messages and immediately send responses back to users. Synchronous invocation ensures that users receive prompt feedback.
  3. User Interfaces: When interacting with user interfaces, such as web or mobile applications, users expect quick responses to their actions. By using synchronous invocation, you can ensure that the backend logic triggered by user interactions executes promptly and provides feedback to the user in near real-time.
  4. Transactional Operations: For operations that involve transactions or require immediate confirmation of completion, synchronous invocation is often preferred. For instance, if a user initiates a purchase on an e-commerce platform, you might use synchronous invocation to process the payment, update inventory, and provide immediate feedback on the success or failure of the transaction.

In summary, synchronous invocation in AWS Serverless is best suited for use cases where immediate response and feedback are essential, such as handling API requests, real-time processing, user interfaces, and transactional operations.

When do we use Async invocation?

Eg: EventBridge and lambda function usage

AWS EventBridge is a powerful event bus service that enables you to build event-driven architectures by routing events from various sources to targets like AWS Lambda functions, SNS topics, SQS queues, and more. You can certainly use EventBridge in conjunction with Lambda functions for asynchronous invocation.

When do we use Polling invocation?

Polling invocation in AWS Serverless, often involving services like AWS Lambda, is used in scenarios where you need to continuously monitor a resource for new events or messages. Here are some common situations where polling invocation is appropriate:

  1. Message Queues:
    • Polling is frequently used with message queues like Amazon SQS (Simple Queue Service) or Amazon Kinesis Data Streams. Lambda functions can continuously poll these queues to retrieve messages as they become available.
    • This approach is useful for asynchronous processing of messages, such as processing orders, handling notifications, or performing batch data processing.
  2. Data Streams:
    • When dealing with continuous streams of data, such as clickstream data or IoT sensor data, Lambda functions can poll data streams like Amazon Kinesis Data Streams to process the data in near real-time.
    • Polling allows Lambda functions to consume data as it arrives, enabling timely analysis, aggregation, or transformation of streaming data.
  3. Database Changes:
    • Polling can be used to monitor database tables for changes. For example, you might use Lambda functions to poll DynamoDB Streams or relational database change data capture (CDC) streams to react to changes in data.
    • This enables you to implement reactive workflows, such as updating search indexes, sending notifications, or triggering downstream processes based on database changes.
  4. External APIs:
    • In cases where external APIs do not support push-based notifications or webhooks, polling can be used to periodically check for updates or new data.
    • Lambda functions can poll external APIs at regular intervals to retrieve data, perform updates, or synchronize information between systems.
  5. File System Monitoring:
    • Polling can be used to monitor changes in file systems, such as detecting new files or updates in an Amazon S3 bucket.
    • Lambda functions can periodically poll the S3 bucket to process new files, trigger workflows, or perform data processing tasks based on file contents.
  6. Custom Event Sources:
    • Polling can be employed with custom event sources or applications that do not natively support event-driven architectures.
    • Lambda functions can periodically poll these custom sources to retrieve events, enabling integration with existing systems or applications.

In summary, polling invocation in AWS Serverless is suitable for scenarios where you need to continuously monitor resources for new events or data. It enables event-driven processing, reactive workflows, and integration with various event sources, making it a versatile approach for building scalable and responsive applications.

Leave a Reply

Your email address will not be published. Required fields are marked *