Can Nginx be used as a load balancer for TiDB?

This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: nginx可否作为tidb的负载均衡器

| username: 像风一样的男子

It’s rare to see people using Nginx to proxy TiDB for load balancing. Are there any drawbacks to using Nginx as a proxy?

| username: 有猫万事足 | Original post link

As long as it supports Layer 4 proxying.

If it only proxies protocols like HTTP, then it operates at Layer 7. In that case, port 4000 cannot be accessed because it is not an HTTP protocol. However, for HTTP protocols like port 10080, you can use either Layer 4 or Layer 7 with an Nginx proxy.

| username: 像风一样的男子 | Original post link

Nginx supports both Layer 4 and Layer 7, but I see that almost no one uses it, so I want to know if there are any other drawbacks.

| username: 有猫万事足 | Original post link

I think the possibility of a defect is very low because it’s too basic. If there were bugs in such a widely used feature, they would be fixed quickly.

However, you need an additional stream module to use Nginx at layer 4. This module might not be available in the version you compile or install via yum. Unlike HAProxy, which works directly at layer 4 and can proxy any TiDB port without additional modules.

In fact, if you are familiar with networking, how to proxy is not an issue at all.

| username: 有猫万事足 | Original post link

It is indeed a bit troublesome. The nginx I installed via yum includes the stream module in the compilation parameters.

When using it, you still need to load it. But this .so file cannot be found. It may need to be recompiled.

In contrast, the configuration in the official HAProxy documentation is basically brainless; you can use it right after downloading. :joy:

| username: tidb菜鸟一只 | Original post link

There are no defects. I’ve used nginx to proxy MySQL many times and haven’t encountered any issues. TiDB is theoretically no different from MySQL, so you can use the stream module to proxy it without any problem.

| username: xfworld | Original post link

The only drawback is that check fail is not so timely. You can use tiproxy… not sure if GA is available,

tiproxy can sense PD information, and when the status of the TiDB node changes, it will notify PD…

| username: ShawnYan | Original post link

Not yet… let the bullets fly for a while

| username: Fly-bird | Original post link

nginx+keepalive can be used for TiDB load balancing.

| username: 随缘天空 | Original post link

Yes, it is possible, but the performance is slightly worse compared to HAProxy.

| username: TiDBer_vfJBUcxl | Original post link


| username: Hacker_8Yfzq2KF | Original post link

This is how we use it here…

| username: Tom_wu | Original post link

Yes, but the configuration is a bit more complex than haproxy, and the performance is not as good as haproxy.

| username: andone | Original post link


| username: ShawnYan | Original post link

Nginx related column articles:

| username: TiDBer_vfJBUcxl | Original post link


| username: zhanggame1 | Original post link

Sure, but it’s not necessary.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.