Introduction to Sticky Cookie in OpenResty Edge

Today, I will demonstrate enabling Sticky Cookies in OpenResty Edge.

Embeded image

We’ll send two requests with Sticky Cookie enabled and will be able to see that the two requests are sent to the same backend server.

Embeded video

Embeded video

Embeded video

Then send two requests without Sticky cookies enabled, and you will see that the two requests were sent to two different backend servers.

Embeded video

Embeded video

Embeded video

Enable Sticky Cookie for the sample application

Let’s go to the web console of OpenResty Edge. This is our sample deployment of the console. Every user would have their deployment.

Screenshot

We use our continuing sample application for the test-edge.com domain.

Screenshot

Let’s enter this application.

Screenshot

Go to the Upstreams page.

Screenshot

We already have an upstream defined.

Screenshot

This upstream currently has an upstream server.

Screenshot

We need two upstream servers. let’s add another one.

Screenshot

Click the “Add a new upstream server” button.

Screenshot

Enter the upstream host.

Screenshot

Verify upstream server availability.

Screenshot

The default index page of the open-source OpenResty was returned, which was as expected.

Save the upstream.

Screenshot

Go to the Page Rules page.

Screenshot

Screenshot

We already have a reverse proxy page rule set up in an earlier tutorial in this application.

Now let’s edit the page rule to enable the Sticky Cookie.

Screenshot

Turn on the Sticky Cookie switch.

Screenshot

Our upstream servers are within an upstream cluster, so we use server-level Sticky Cookies.

Screenshot

Select the server level.

Screenshot

Here we can set the expiry time of the Sticky Cookie.

Screenshot

We use the default value of 0, which never expires.

Save the page rules.

Screenshot

Next, we add a dynamic metric to see the distribution of requests. This is not required. The sticky cookie feature does not rely on any dynamic metrics.

Screenshot

Click the New Metric button.

Screenshot

We name it “Upstream Server Request Count”.

Screenshot

Add a description.

Screenshot

Set the Report Interval to 10 seconds.

Screenshot

We type in the Metric SQL statement for this metric. It will select the upstream server address.

Screenshot

from all requests.

Screenshot

group by upstream server address.

Screenshot

Save the metric.

Screenshot

We need to make a new release to push out our new changes, as always.

Screenshot

Click on this button.

Screenshot

Ship it!

Screenshot

Our new release is now synchronized to all our gateway servers.

Screenshot

Now the change has been pushed to all the gateway clusters and servers.

Embeded video

Embeded video

Embeded video

Our configuration changes do NOT require server reload, restart, or binary upgrade. So it’s very efficient and scalable.

Embeded image

Test

Then send two requests. Send the first request.

Screenshot

Send the second request.

Screenshot

All requests completed.

we now look at the data recorded by the dynamic metrics.

Screenshot

Select the bar chart.

Screenshot

Screenshot

All requests fall on the same upstream server.

As shown below.

Embeded video

Embeded video

Embeded video

Disable Sticky Cookie

Next, we demonstrate the case without Sticky Cookies enabled.

Screenshot

Let’s clean up the metric data.

Screenshot

Confirm.

Screenshot

Now that the cleanup is complete let’s disable the Sticky Cookie.

Screenshot

Edit this page rule again.

Screenshot

Turn off the Sticky Cookie switch.

Screenshot

Note that the balancing policy is “Round Robin”.

Screenshot

Save the page rules.

Screenshot

We need to make a new release to push out our new changes, as always.

Screenshot

Click on this button.

Screenshot

Ship it!

Screenshot

Our new release is now synchronized to all our gateway servers.

Screenshot

Test

Next, we’ll send two requests again.

Send the first request.

Screenshot

Send the second request.

Screenshot

All requests completed.

Recheck the dynamic metrics.

Screenshot

Click on the bar chart.

Screenshot

both requests visit the different upstream server this time.

Screenshot

As shown below.

Embeded video

Embeded video

Embeded video

We can keep the client affinity with the upstream cluster or upstream server with the Sticky Cookie. This is what I’d like to cover today.

What is OpenResty Edge

OpenResty Edge is our all-in-one gateway software for microservices and distributed traffic architectures. It combines traffic management, private CDN construction, API gateway, security, and more to help you easily build, manage, and protect modern applications. OpenResty Edge delivers industry-leading performance and scalability to meet the demanding needs of high concurrency, high load scenarios. It supports scheduling containerized application traffic such as K8s and manages massive domains, making it easy to meet the needs of large websites and complex applications.

If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!

About The Author

Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.

OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.

As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.