Friday, March 31, 2017

Proxy TCP stream (MySQL and Redis) with Nginx

Leave a Comment

I read about Nginx Fabric Model and it brings my attention to reconfigure how an application communicates to MySQL and Redis. If local Nginx instance can proxy HTTP traffic efficiently and fast, now it can also proxy TCP without worrying of network, even using database slave as master in case of emergency and potentially encapsulate database sharding. All the benefits can simplify application configuration and its logic, network (congestion, latency, timeouts, retries) won't be a focus in features development anymore.

I use latest Docker and set of containers: Nginx, Redis, MySQL. I tried the following configuration:

user  nginx; worker_processes  1;  error_log  /var/log/nginx/error.log info; pid        /var/run/nginx.pid;   events {   worker_connections  1024; }  stream {   upstream redis {     # prefer first server but limit connections     server 172.17.0.8:6379 weight=2 max_conns=1;     server 172.17.0.3:6379;   }    upstream mysql {     # use second server in case of failure     server 172.17.0.4:3306;     server 172.17.0.5:3306 backup;   }    server {     listen 6379 so_keepalive=on;     proxy_pass redis;   }    server {     listen 3306 so_keepalive=on;     proxy_pass mysql;   } } 

I have some questions:

  • logging - how can I know which endpoint is in use, how many times did Nginx retry a particular request?

  • real-time stats - is it possible to get throughput for stream module?

  • from database sharding perspective - is it possible to dispatch request to sharded database based on some logic apart from $remote_addr?

Last question is quite important, I found modules ngx_stream_map_module and ngx_stream_split_clients_module but $remote_addr is not suitable for sharding, can we intercept cookie from http section and reuse in stream section where we don't have any headers? Can we inject Lua code in the stream section? Is ngx_stream_ssl_preread_module a solution for this problem, how to make it work for connection without encryption?

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment