Skip to content

Instantly share code, notes, and snippets.

@staticaland
Last active October 11, 2024 13:28
Show Gist options
  • Save staticaland/2e199272d8d51447f92b1439f57b98da to your computer and use it in GitHub Desktop.
Save staticaland/2e199272d8d51447f92b1439f57b98da to your computer and use it in GitHub Desktop.

Different kinds of stickiness related to ALBs and target groups

  1. Target Group Target Stickiness (TargetGroup_TargetStickiness): Configured under a Target Group, this type of stickiness ensures that a client's requests are consistently routed to the same target (such as an EC2 instance or container) within that target group.

  2. Listener Rule Target Group Stickiness (ListenerRule_TargetGroupStickiness): Configured under a Listener Rule, this stickiness ensures that a client's requests are consistently routed to the same target group when multiple target groups are associated with that listener rule.

Let's delve into when it makes sense to use each:


1. Target Group Target Stickiness (TargetGroup_TargetStickiness):

What It Does:

  • Ensures that after a client is routed to a specific target within a target group, subsequent requests from that client are consistently sent to the same target.
  • Uses cookies to maintain this stickiness at the target level within the target group.

When to Use It:

  • Stateful Applications: If your application stores session data on individual targets and doesn't share session state across instances (e.g., user login sessions stored in memory), you need requests from a client to consistently reach the same target.

  • Caching Benefits: When targets cache data specific to clients, and you want to maximize cache hits by routing clients to the same target.

  • Legacy Systems: For applications that cannot be easily modified to store session data in a shared database or cache, target-level stickiness can ensure proper functionality.

  • Performance Optimization: Reduces overhead in retrieving session data from external stores by keeping the session data local to the target.

Example Scenario:

You have an application where user sessions are stored in the memory of each EC2 instance (target). Without stickiness, requests might go to different instances, causing session loss. By enabling TargetGroup_TargetStickiness, you ensure that a user's requests consistently go to the same instance, preserving the session.


2. Listener Rule Target Group Stickiness (ListenerRule_TargetGroupStickiness):

What It Does:

  • Ensures that after a client is routed to a specific target group by a listener rule, subsequent requests from that client are consistently sent to the same target group.
  • This is particularly relevant when a listener rule is configured to forward requests to multiple target groups, possibly with different weights.

When to Use It:

  • Canary or Blue-Green Deployments: When rolling out new versions of your application, you might direct a small percentage of traffic to a new target group containing the updated version. ListenerRule_TargetGroupStickiness ensures that clients continue to hit the same version during their session.

  • A/B Testing: If you're testing different application versions or features across target groups, stickiness at the target group level ensures that users have a consistent experience.

  • Weighted Load Balancing: When distributing traffic across multiple target groups with specific weights, and you want clients to remain within the assigned target group for consistency.

  • Service Consistency: If different target groups serve slightly different content or services (e.g., localized content), and you want users to consistently receive content from the same group.

Example Scenario:

You have a listener rule that forwards traffic to two target groups: one with the current stable version of your application and one with a new version for testing. By enabling ListenerRule_TargetGroupStickiness, you ensure that once a client starts interacting with a particular version, they continue to interact with it throughout their session.


Summary of When to Use Each:

  • Use TargetGroup_TargetStickiness when:

    • You need stickiness at the target (instance) level within a single target group.
    • Your application requires that a client's requests are consistently handled by the same server instance.
    • Maintaining session state on the same target is critical for application functionality.
  • Use ListenerRule_TargetGroupStickiness when:

    • You have a listener rule that forwards to multiple target groups, and you need clients to consistently hit the same target group.
    • You're implementing A/B testing, canary deployments, or blue-green deployments, and need session consistency at the target group level.
    • You want to ensure a consistent user experience when target groups serve different content or versions.

Key Differences:

  • Level of Stickiness:

    • TargetGroup_TargetStickiness: Stickiness is at the target (server instance) level within a single target group.
    • ListenerRule_TargetGroupStickiness: Stickiness is at the target group level when multiple target groups are associated with a listener rule.
  • Configuration Location:

    • TargetGroup_TargetStickiness: Configured under the Target Group settings.
    • ListenerRule_TargetGroupStickiness: Configured under the Listener Rule settings.
  • Use Cases:

    • TargetGroup_TargetStickiness: Ideal for session persistence where the application state is stored on individual servers.
    • ListenerRule_TargetGroupStickiness: Ideal for traffic routing strategies involving multiple target groups, such as gradual rollouts or testing scenarios.

Conclusion:

Choosing between TargetGroup_TargetStickiness and ListenerRule_TargetGroupStickiness depends on your application's architecture and session management needs:

  • If session persistence on the same server instance is critical, and you have a single target group handling the traffic, enable Target Group Target Stickiness.

  • If you're using advanced traffic routing with multiple target groups (e.g., for deployments or testing), and you need clients to consistently interact with the same target group, enable Listener Rule Target Group Stickiness.

By aligning your stickiness configuration with your application's requirements and deployment strategies, you ensure optimal performance and a consistent user experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment