EventBridge

Architecting AWS workflows, when to choose EventBridge or Batch

Selecting the right service for your workflow can often be challenging when building on AWS. You might think of it as choosing between two powerful tools in your toolbox: Amazon EventBridge and AWS Batch. While both have robust functionalities, they cater to different types of tasks. Knowing when to use each and how to combine them can make all the difference in building efficient, scalable applications.

Let’s look into each service, understand their unique roles, and explore practical scenarios where one outshines the other.

Amazon EventBridge: Real-Time reactions in action

Imagine Amazon EventBridge as a highly efficient “event router” for your system. In EventBridge, everything is an event, from user actions to system-generated notifications. This service shines when you need instant, real-time responses across multiple AWS services.

For instance, let’s consider a modern e-commerce platform. When a customer makes a purchase, EventBridge steps in to orchestrate the sequence of actions: it updates the inventory in DynamoDB, sends an email notification via SES (Simple Email Service), records analytics data in Redshift, and notifies third-party shipping services. All these tasks happen simultaneously, without delays. EventBridge acts as a conductor, keeping everything in sync in real-time.

Why EventBridge?

EventBridge is especially powerful for real-time processing, integration of different services, and flexible routing of events. When your system is composed of microservices or serverless components, EventBridge provides the glue to hold them together. It has built-in integrations with over 20 AWS services and supports custom SaaS applications. And thanks to “event schemas”, essentially standardized formats for different types of events, you can ensure consistent communication across diverse components.

To simplify: EventBridge excels in fast, lightweight operations. It’s the ideal choice when your priority is speed and responsiveness, and when you’re dealing with workflows that require instant reactions and coordinated actions.

AWS Batch: Powering through heavy lifting with batch processing

If EventBridge is your “quick response” tool, AWS Batch is your “muscle.” AWS Batch specializes in executing computationally intensive jobs that can take longer to complete. Imagine a factory floor filled with machinery working on heavy-duty tasks. AWS Batch is designed to handle these large, sometimes complex processes in an organized, efficient way.

Let’s look at data science or machine learning workloads as an example. Suppose you need to process large datasets or train models that take hours, sometimes even days, to complete. AWS Batch allows you to allocate exactly the resources you need, whether that means using more powerful CPUs or accessing GPU instances. Batch jobs can run on EC2 instances or Fargate, enabling flexibility and resource optimization.

Array Jobs: Maximizing Throughput

One of the most powerful features in AWS Batch is Array Jobs. Think of Array Jobs as a way to break down massive tasks into hundreds or thousands of smaller tasks, each working on a piece of the overall puzzle. This is especially useful in fields like genomics, where each gene sequence needs to be analyzed separately, or in video rendering, where each frame can be processed in parallel. Array Jobs allow all these smaller tasks to run at the same time, significantly speeding up the entire process.

In short, AWS Batch is ideal for heavy-duty computations, data-heavy processes, and tasks that can run in parallel. It’s the go-to choice when you need a high level of control over computational resources and are dealing with workflows that aren’t as time-sensitive but are resource-intensive.

When should You use each?

Use EventBridge when:

  1. Real-Time monitoring: EventBridge excels in event-driven architectures where immediate responses are critical, like monitoring applications in real-time.
  2. Serverless integration: If your architecture relies on serverless components (such as AWS Lambda), EventBridge provides the ideal connectivity.
  3. Complex routing needs: The service’s routing rules let you direct events based on content, scheduling, and custom patterns, perfect for sophisticated integrations.
  4. API integrations: EventBridge simplifies B2B interactions by acting as a “contract” between systems, making it easy to exchange real-time updates without directly managing API dependencies.

Use AWS Batch when:

  1. High computational demand: For tasks like data processing, machine learning, and scientific simulations, Batch allows access to specialized resources, including EC2 instances and GPUs.
  2. Large-Scale data processing: Array Jobs enables AWS Batch to break down and process enormous datasets simultaneously, perfect for fields that handle large volumes of data.
  3. Asynchronous or Background processing: Tasks that don’t require immediate responses, like video processing or data analysis, are best suited to Batch’s queue-based setup.

Hybrid scenarios: Using EventBridge and AWS Batch together

In some cases, EventBridge and Batch can complement each other to form a hybrid approach. Imagine you have an image-processing pipeline for a photography website:

  1. Image upload: EventBridge receives the image upload event and triggers a validation process to check the file type and size.
  2. Processing trigger: If the image meets requirements, EventBridge kicks off an AWS Batch job to generate multiple versions (like thumbnails and high-resolution images).
  3. Parallel processing with Array Jobs: AWS Batch processes each image version as an Array Job, optimizing performance and speed.
  4. Event notification: When Batch completes the task, EventBridge routes a completion notification to other parts of the system (e.g., updating the image gallery).

In this scenario, EventBridge handles the quick actions and routing, while Batch takes care of the intensive processing. Combining both services allows you to leverage real-time responsiveness and high computational power, meeting the needs of diverse workflows efficiently.

Choosing the right tool for the job

Selecting between Amazon EventBridge and AWS Batch boils down to the nature of your task:

  • For real-time event handling and multi-service integrations, EventBridge is your best choice. It’s agile, responsive, and designed for systems that need to react immediately to changes.
  • For resource-intensive processing and background jobs, AWS Batch is unbeatable. With fine-grained control over compute resources, it’s tailor-made for workflows that require significant computational power.
  • In cases that demand both real-time responses and heavy processing, don’t hesitate to use both services in tandem. A hybrid approach lets you harness the strengths of each service, optimizing your architecture for efficiency, speed, and scalability.

In the end, each service has unique strengths tailored for specific workloads. With a clear understanding of what each offers, you can design workflows that are not only optimized but also built to handle the demands of modern applications in AWS.

AWS EventBridge Essentials. A Guide to Rules and Scheduler

Let’s take a look into AWS EventBridge, a powerful service designed to connect applications using data from our own apps, integrated Software as a Service (SaaS) apps, and AWS services. In particular, we’ll focus on the two main features: EventBridge Rules and the relatively new EventBridge Scheduler. These features overlap in many ways but also offer distinct functionalities that can significantly impact how we manage event-driven applications. Let’s explore what each of these features brings to the table and how to determine which one is right for our needs.

What is AWS EventBridge?

AWS EventBridge is a serverless event bus that makes it easy to connect applications using data from our applications, integrated SaaS applications, and AWS services. EventBridge simplifies the process of building event-driven architectures by routing events from various sources to targets such as AWS Lambda functions, Amazon SQS queues, and more. With EventBridge, we can set up rules to determine how events are routed based on their content.

EventBridge Rules

Overview

EventBridge Rules allow you to define how events are routed to targets based on their content. Rules enable you to match incoming events and send them to the appropriate target. There are two primary types of invocations:

  1. Event Pattern-Based Invocation
  2. Timer-Based Invocation

Event Pattern-Based Invocation

This feature lets us create rules that match specific patterns in event payloads. Events can come from various sources, such as AWS services (e.g., EC2 state changes), partner services (e.g., Datadog), or custom applications. Rules are written in JSON and can match events based on specific attributes.

Example:

Suppose we have an e-commerce application, and we want to trigger a Lambda function whenever an order’s status changes to “pending.” We would set up a rule that matches events where the orderState attribute is pending and routes these events to the Lambda function.

{
  "detail": {
    "orderState": ["pending"]
  }
}

This rule ensures that only events with an orderState of pending invoke the Lambda function, ignoring other states like delivered or shipped.

Timer-Based Invocation

EventBridge Rules also support timer-based invocations, allowing you to trigger events at specific intervals using either rate expressions or cron expressions.

  • Rate Expressions: Trigger events at regular intervals (e.g., every 5 minutes, every hour).
  • Cron Expressions: Provide more flexibility, enabling us to specify exact times for event triggers (e.g., every day at noon).

Example:

To trigger a Lambda function every day at noon, we would use a cron expression like this:

{
 "scheduleExpression": "cron(0 12 * * ? *)"
}

Limitations of EventBridge Rules

  1. Fixed Event Payload: The payload passed to the target is static and cannot be changed dynamically between invocations.
  2. Requires an Event Bus: All rule-based invocations require an event bus, adding an extra layer of configuration.

EventBridge Scheduler

Overview

The EventBridge Scheduler is a recent addition to the AWS arsenal, designed to simplify and enhance the scheduling of events. It supports many of the same scheduling capabilities as EventBridge Rules but adds new features and improvements.

Key Features

  1. Rate and Cron Expressions: Like EventBridge Rules, the Scheduler supports both rate and cron expressions for defining event schedules.
  2. One-Time Events: A unique feature of the Scheduler is the ability to create one-time events that trigger a single event at a specified time.
  3. Flexible Time Windows: Allows us to define a time window within which the event can be triggered, helping to stagger event delivery and avoid spikes in load.
  4. Automatic Retries: We can configure automatic retries for failed event deliveries, specifying the number of retries and the time interval between them.
  5. Dead Letter Queues (DLQs): Events that fail to be delivered even after retries can be sent to a DLQ for further analysis and handling.

Example of One-Time Events

Imagine we want to send a follow-up email to customers 21 days after they place an order. Using the Scheduler, we can create a one-time event scheduled for 21 days from the order date. When the event triggers, it invokes a Lambda function that sends the email, using the context provided when the event was created.

{
 "scheduleExpression": "at(2023-06-01T00:00:00)",
 "target": {
 "arn": "arn:aws:lambda:region:account-id:function:sendFollowUpEmail",
 "input": "{\"customerId\":\"123\",\"email\":\"customer@example.com\"}"
 }
}

Comparing EventBridge Rules and Scheduler

When to Use EventBridge Rules

  • Pattern-Based Event Routing: If we need to route events to different targets based on the event content, EventBridge Rules are ideal. For example, routing different order statuses to different Lambda functions.
  • Complex Event Patterns: When we have complex patterns that require matching against multiple attributes, EventBridge Rules provide the necessary flexibility.

When to Use EventBridge Scheduler

  • Timer-Based Invocations: For any time-based scheduling (rate or cron), the Scheduler is preferred due to its additional features like start and end times, flexible time windows, and automatic retries.
  • One-Time Events: If you need to schedule events to occur at a specific time in the future, the Scheduler’s one-time event capability is invaluable.
  • Simpler Configuration: The Scheduler offers a more straightforward setup for time-based events without the need for an event bus.

AWS Push Towards Scheduler

AWS seems to be steering users towards the Scheduler for timer-based invocations. In the AWS Console, when creating a new scheduled rule, you’ll often see prompts suggesting the use of the EventBridge Scheduler instead. This indicates a shift in focus, suggesting that AWS may continue to invest more heavily in the Scheduler, potentially making some of the timer-based functionalities of EventBridge Rules redundant in the future.

Summing It Up

AWS EventBridge Rules and EventBridge Scheduler are powerful tools for building event-driven architectures. Understanding their capabilities and limitations will help us choose the right tool for our needs. EventBridge Rules excel in dynamic, pattern-based event routing, while EventBridge Scheduler offers enhanced features for time-based scheduling and one-time events. As AWS continues to develop these services, keeping an eye on new features and updates will ensure that we leverage the best tools for our applications.