skip to Main Content

In my application users create “teams” (aka workspaces). I’d like to configure AWS ALB sticky sessions to route requests from the same team to the same EC2 instance so that in-memory team-level caches are more effective. It’s not a requirement that all requests go to the same EC2 instance but it would mean there are fewer cache misses.

The team ID is present in either the URL or an HTTP header depending on the request.

It’s unclear to me how to accomplish this from the AWS ALB sticky session documentation. In the section titled “Application-based stickiness” the documentation says:

Application-based stickiness gives you the flexibility to set your own criteria for client-target stickiness. When you enable application-based stickiness, the load balancer routes the first request to a target within the target group based on the chosen algorithm.

Which sounds like what I want? Though the docs don’t detail how to configure the “chosen algorithm” for that initial routing.

How would you accomplish routing multiple users of the same team to the same EC2 instance with AWS ALB? Is it possible?

2

Answers


  1. Chosen algorithm would be under Traffic configuration in the AWS Console UI for the Target group. It can be one of:

    • Round robin (this is the default option)
    • Least outstanding requests
    • Weighted random

    Now, for the primary part of your question:

    If you want to route multiple people from the same team to the same target, you can use application-based stickiness as you mentioned. To use it, you need to generate a cookie in your application. When you are setting up the stickiness on the target group, set:

    • Stickiness type – Application-based cookie
    • App cookie name – the name of the cookie that you will generate in your application (for example team_session)

    On the AWS ALB part, that’s it. In your application, you should now generate this cookie and make sure that the value of the cookie is the same for all members of the team, so incorporate that in the logic of generating this cookie for users.

    Login or Signup to reply.
  2. You can:

    1. Tell the ALB to look at a cookie for which you specify the name here (section: "To enable application-based stickiness using the console"), and then
    2. You set that cookie’s name and value in your application logic. You would want all the members of the same team to have the same cookie name and value.

    But separate from your question, I have a concern:

    • What if you have a team/workspace with a lot of users? Would this defeat the purpose of using a load balancer in the first place if you route them all to the same EC2 instance? Rhetorical question, you don’t need to answer. Just something to think about.
    • You could use Amazon ElastiCache or Redis or an in-memory database separate from the EC2 instances for a cloud memory store and then go that route as an alternative architecture direction to reduce cache-misses rather than trying to use the ALB to route traffic to 1 instance per team/workspace.

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search