Configuring sticky cookies in OpenResty Edge gateways
Introduction to Sticky Cookie in OpenResty Edge
Today, I will demonstrate enabling Sticky Cookies in OpenResty Edge.
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.
Then send two requests without Sticky cookies enabled, and you will see that the two requests were sent to two different backend servers.
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.
We use our continuing sample application for the test-edge.com domain.
Let’s enter this application.
Go to the Upstreams page.
We already have an upstream defined.
This upstream currently has an upstream server.
We need two upstream servers. let’s add another one.
Click the “Add a new upstream server” button.
Enter the upstream host.
Verify upstream server availability.
The default index page of the open-source OpenResty was returned, which was as expected.
Save the upstream.
Go to the Page Rules page.
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.
Turn on the Sticky Cookie switch.
Our upstream servers are within an upstream cluster, so we use server-level Sticky Cookies.
Select the server level.
Here we can set the expiry time of the Sticky Cookie.
We use the default value of 0, which never expires.
Save the page rules.
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.
Click the New Metric button.
We name it “Upstream Server Request Count”.
Add a description.
Set the Report Interval to 10 seconds.
We type in the Metric SQL statement for this metric. It will select the upstream server address.
from all requests.
group by upstream server address.
Save the metric.
We need to make a new release to push out our new changes, as always.
Click on this button.
Ship it!
Our new release is now synchronized to all our gateway servers.
Now the change has been pushed to all the gateway clusters and servers.
Our configuration changes do NOT require server reload, restart, or binary upgrade. So it’s very efficient and scalable.
Test
Then send two requests. Send the first request.
Send the second request.
All requests completed.
we now look at the data recorded by the dynamic metrics.
Select the bar chart.
All requests fall on the same upstream server.
As shown below.
Disable Sticky Cookie
Next, we demonstrate the case without Sticky Cookies enabled.
Let’s clean up the metric data.
Confirm.
Now that the cleanup is complete let’s disable the Sticky Cookie.
Edit this page rule again.
Turn off the Sticky Cookie switch.
Note that the balancing policy is “Round Robin”.
Save the page rules.
We need to make a new release to push out our new changes, as always.
Click on this button.
Ship it!
Our new release is now synchronized to all our gateway servers.
Test
Next, we’ll send two requests again.
Send the first request.
Send the second request.
All requests completed.
Recheck the dynamic metrics.
Click on the bar chart.
both requests visit the different upstream server this time.
As shown below.
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.