Skip to content

Commit f9006a5

Browse files
committed
apachebench and multiple backends
1 parent 8051152 commit f9006a5

File tree

2 files changed

+184
-2
lines changed

2 files changed

+184
-2
lines changed

multiple-servers/IMPLEMENTATION.md

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Copied over from server-database, with file split up:
5151

5252
This has the same setup steps as server-database, so those can be copied over.
5353

54-
# Ports
54+
## Ports
5555

5656
`cmd` files should allow ports to be configred using `--port`.
5757

@@ -64,3 +64,177 @@ Switch static server over to 8082.
6464
```
6565
nginx -c `pwd`/config/nginx.conf
6666
```
67+
68+
## Benchmark
69+
70+
`ab` the API:
71+
72+
```console
73+
> ab -n 5000 -c 25 "http://127.0.0.1:8080/api/images.json"
74+
This is ApacheBench, Version 2.3 <$Revision: 1901567 $>
75+
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
76+
Licensed to The Apache Software Foundation, http://www.apache.org/
77+
78+
Benchmarking 127.0.0.1 (be patient)
79+
80+
Completed 500 requests
81+
Completed 1000 requests
82+
Completed 1500 requests
83+
Completed 2000 requests
84+
Completed 2500 requests
85+
Completed 3000 requests
86+
Completed 3500 requests
87+
Completed 4000 requests
88+
Completed 4500 requests
89+
Completed 5000 requests
90+
Finished 5000 requests
91+
92+
93+
Server Software: nginx/1.23.1
94+
Server Hostname: 127.0.0.1
95+
Server Port: 8080
96+
97+
Document Path: /api/images.json
98+
Document Length: 4 bytes
99+
100+
Concurrency Level: 25
101+
Time taken for tests: 1.866 seconds
102+
Complete requests: 5000
103+
Failed requests: 0
104+
Total transferred: 885000 bytes
105+
HTML transferred: 20000 bytes
106+
Requests per second: 2680.08 [#/sec] (mean)
107+
Time per request: 9.328 [ms] (mean)
108+
Time per request: 0.373 [ms] (mean, across all concurrent requests)
109+
Transfer rate: 463.26 [Kbytes/sec] received
110+
111+
Connection Times (ms)
112+
min mean[+/-sd] median max
113+
Connect: 0 1 2.0 1 59
114+
Processing: 1 8 24.2 3 322
115+
Waiting: 0 7 20.3 3 322
116+
Total: 1 9 24.3 4 323
117+
118+
Percentage of the requests served within a certain time (ms)
119+
50% 4
120+
66% 4
121+
75% 5
122+
80% 6
123+
90% 28
124+
95% 34
125+
98% 37
126+
99% 62
127+
100% 323 (longest request)
128+
```
129+
130+
And the static server:
131+
132+
```console
133+
> ab -n 5000 -c 25 "http://127.0.0.1:8080/"
134+
This is ApacheBench, Version 2.3 <$Revision: 1901567 $>
135+
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
136+
Licensed to The Apache Software Foundation, http://www.apache.org/
137+
138+
Benchmarking 127.0.0.1 (be patient)
139+
Completed 500 requests
140+
Completed 1000 requests
141+
Completed 1500 requests
142+
Completed 2000 requests
143+
Completed 2500 requests
144+
Completed 3000 requests
145+
Completed 3500 requests
146+
Completed 4000 requests
147+
Completed 4500 requests
148+
Completed 5000 requests
149+
Finished 5000 requests
150+
151+
152+
Server Software: nginx/1.23.1
153+
Server Hostname: 127.0.0.1
154+
Server Port: 8080
155+
156+
Document Path: /
157+
Document Length: 607 bytes
158+
159+
Concurrency Level: 25
160+
Time taken for tests: 1.502 seconds
161+
Complete requests: 5000
162+
Failed requests: 0
163+
Total transferred: 4165000 bytes
164+
HTML transferred: 3035000 bytes
165+
Requests per second: 3328.26 [#/sec] (mean)
166+
Time per request: 7.511 [ms] (mean)
167+
Time per request: 0.300 [ms] (mean, across all concurrent requests)
168+
Transfer rate: 2707.46 [Kbytes/sec] received
169+
170+
Connection Times (ms)
171+
min mean[+/-sd] median max
172+
Connect: 0 1 0.3 1 3
173+
Processing: 1 7 10.2 3 115
174+
Waiting: 1 6 9.2 3 115
175+
Total: 2 7 10.1 4 116
176+
177+
Percentage of the requests served within a certain time (ms)
178+
50% 4
179+
66% 4
180+
75% 5
181+
80% 9
182+
90% 23
183+
95% 25
184+
98% 27
185+
99% 28
186+
100% 116 (longest request)
187+
```
188+
189+
## Multiple backends
190+
191+
```nginx
192+
http {
193+
...
194+
195+
# Define a group of API servers that nginx can use
196+
upstream api {
197+
server localhost:8081;
198+
}
199+
200+
...
201+
202+
proxy_pass http://api/;
203+
204+
...
205+
}
206+
```
207+
208+
Add alternatives:
209+
210+
```nginx
211+
server localhost:8083;
212+
server localhost:8084;
213+
```
214+
215+
Run them:
216+
217+
```console
218+
> DATABASE_URL='postgres://localhost:5432/go-server-database' go run ./cmd/api-server --port 8083
219+
> DATABASE_URL='postgres://localhost:5432/go-server-database' go run ./cmd/api-server --port 8084
220+
```
221+
222+
Run a small `ab` (`ab -n 10 -c 10 "http://127.0.0.1:8080/api/images.json"`) and observe the server logs: the requests are distributed between the servers.
223+
224+
Turn off one of the servers and run another small `ab`: `ab -n 10 -c 10 "http://127.0.0.1:8080/api/images.json"`
225+
226+
Look at the `nginx` logs:
227+
228+
```
229+
127.0.0.1 - - [21/Aug/2022:17:07:44 +0100] "GET /api/images.json HTTP/1.0" 200 4 "-" "ApacheBench/2.3"
230+
2022/08/21 17:07:44 [error] 31112#0: *4088 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://127.0.0.1:8084/images.json", host: "127.0.0.1:8080"
231+
2022/08/21 17:07:44 [warn] 31112#0: *4088 upstream server temporarily disabled while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://127.0.0.1:8084/images.json", host: "127.0.0.1:8080"
232+
2022/08/21 17:07:44 [error] 31112#0: *4090 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://[::1]:8084/images.json", host: "127.0.0.1:8080"
233+
2022/08/21 17:07:44 [warn] 31112#0: *4090 upstream server temporarily disabled while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://[::1]:8084/images.json", host: "127.0.0.1:8080"
234+
2022/08/21 17:07:44 [error] 31113#0: *4092 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://127.0.0.1:8084/images.json", host: "127.0.0.1:8080"
235+
2022/08/21 17:07:44 [warn] 31113#0: *4092 upstream server temporarily disabled while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://127.0.0.1:8084/images.json", host: "127.0.0.1:8080"
236+
2022/08/21 17:07:44 [error] 31113#0: *4092 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://[::1]:8084/images.json", host: "127.0.0.1:8080"
237+
2022/08/21 17:07:44 [warn] 31113#0: *4092 upstream server temporarily disabled while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/images.json HTTP/1.0", upstream: "http://[::1]:8084/images.json", host: "127.0.0.1:8080"
238+
```
239+
240+
Note `upstream server temporarily disabled while connecting to upstream` — it is automatically spotting this and disabling the server.

multiple-servers/config/nginx.conf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ http {
4545
# https://docs.nginx.com/nginx/admin-guide/web-server/compression/
4646
gzip on;
4747

48+
# Define a group of API servers that nginx can use
49+
upstream api {
50+
server localhost:8081;
51+
# server localhost:8083;
52+
# server localhost:8084;
53+
}
54+
4855
# Sets configuration for a virtual server.
4956
# https://nginx.org/en/docs/http/ngx_http_core_module.html#server
5057
server {
@@ -55,8 +62,9 @@ http {
5562
# https://nginx.org/en/docs/http/ngx_http_core_module.html#location
5663
location /api/ {
5764
# Set URL to which the request is passed.
65+
# In this case, pass to the "api" upstream.
5866
# https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
59-
proxy_pass http://0.0.0.0:8081/;
67+
proxy_pass http://api/;
6068
}
6169

6270
# Other request forwarded to a local server running on port 8082

0 commit comments

Comments
 (0)