Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions postgresql/ha-proxysql-benchmark/APP-ONLY-BENCHMARK.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Simulating Application Load (No Proxy)

To emulate some random load from the three application servers to the primary DB run the benchmark. ProxySQL is already pre-configured via [proxysql.cnf](cnf/proxysql.cnf) to have defined the primary server in hostgroup `10`.
To emulate some random load from the three application servers to the primary DB run the benchmark. ProxySQL is already pre-configured via [proxysql.cnf](cnf/proxysql.cnf) to have defined the primary server in hostgroup `1`.

```
$ ./run-benchmark.sh
Expand All @@ -15,20 +15,22 @@ App Server 3: PID=7205 RATE=200 THREADS=10
```


Using the `summary.sh` script to review the host connections per database you will find connections to the primary database `100` originating from application servers `.6`, `.16`, `.26`.
Using the `summary.sh` script to review the host connections per database you will find connections to the primary database `.1` originating from application servers `.20`, `.21`, `.22`.

```
$ scripts/summary.sh hosts-per-db

##### For DB 'primary' #####

client_addr | count
---------------+-------
172.113.0.16 | 11
172.113.0.26 | 5
172.113.0.6 | 5
172.113.0.101 | 1
172.113.0.102 | 1
client_addr | count
-------------+-------
10.0.0.20 | 11
10.0.0.21 | 10
10.0.0.222 | 9
10.0.0.22 | 7
10.0.0.3 | 1
10.0.0.2 | 1
(6 rows)
```

You will find no connections to the replicas.
Expand All @@ -46,3 +48,27 @@ You will find no connections to the replicas.
-------------+-------
(0 rows)
```

You can view SQL traffic using where for a benchmark you will find primary benchmark traffic, but no replica benchmark traffic.

```
scripts/summary.sh queries-per-db

##### For DB 'primary' #####

calls | total_ms | avg_ms | stddev_ms | rows | query
-------+----------+--------+-----------+-------+-----------------------------------------------------------------------
227 | 43.82 | 0.19 | 0.09 | 22700 | SELECT DISTINCT c FROM sbtest5 WHERE id BETWEEN $1 AND $2 ORDER BY c
215 | 41.95 | 0.20 | 0.08 | 21500 | SELECT DISTINCT c FROM sbtest4 WHERE id BETWEEN $1 AND $2 ORDER BY c
207 | 37.27 | 0.18 | 0.07 | 20700 | SELECT DISTINCT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 ORDER BY c
210 | 36.33 | 0.17 | 0.07 | 21000 | SELECT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c
195 | 36.02 | 0.18 | 0.08 | 19500 | SELECT DISTINCT c FROM sbtest9 WHERE id BETWEEN $1 AND $2 ORDER BY c
193 | 36.02 | 0.19 | 0.06 | 19300 | SELECT DISTINCT c FROM sbtest7 WHERE id BETWEEN $1 AND $2 ORDER BY c
219 | 35.40 | 0.16 | 0.05 | 21900 | SELECT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 ORDER BY c
191 | 35.29 | 0.18 | 0.07 | 19100 | SELECT DISTINCT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c
192 | 35.21 | 0.18 | 0.08 | 19200 | SELECT DISTINCT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 ORDER BY c
188 | 34.94 | 0.19 | 0.07 | 18800 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN $1 AND $2 ORDER BY c

```

Next, checkout the [Simulating Application Load (With Proxy)](WRITE-ONLY-BENCHMARK.md) benchmark.
15 changes: 5 additions & 10 deletions postgresql/ha-proxysql-benchmark/Dockerfile.proxysql
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ RUN apt-get update && \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Create proxysql user and data directory
RUN useradd -r -s /bin/false proxysql && \
mkdir -p /var/lib/proxysql && \
chown -R proxysql:proxysql /var/lib/proxysql
ARG PROXYSQL_VERSION=3.0.8

# Download ProxySQL binary
RUN curl -fSL "https://repo.proxysql.com/ProxySQL/tmp/demo/proxysql.ubuntu22" -o /usr/bin/proxysql && \
chmod +x /usr/bin/proxysql

# Download web interface plugin
RUN curl -fSL "https://repo.proxysql.com/ProxySQL/tmp/demo/Web_Interface_plugin.so.ubuntu22" -o /var/lib/proxysql/Web_Interface_plugin.so
RUN ARCH=$(dpkg --print-architecture) && \
curl -fSL "https://repo.proxysql.com/ProxySQL/proxysql-3.0.x/jammy/proxysql_${PROXYSQL_VERSION}-ubuntu22_${ARCH}.deb" -o /tmp/proxysql.deb && \
dpkg -i /tmp/proxysql.deb && \
rm /tmp/proxysql.deb

# Configure supervisor to run ProxySQL
COPY cnf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
Expand Down
36 changes: 20 additions & 16 deletions postgresql/ha-proxysql-benchmark/READ-LOAD-BALANCING-BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ If you have been running other tests, you may need to cleanup database connectio

To emulate load from the three application servers to the Proxy Layer that splits Writes and Reads between the Primary DB and a replica DB we first configure ProxySQL.

1. Remove any existing hostgroup `20` servers.
2. Add the second replica as a known server to ProxySQL using hostgroup `20`. NOTE: hostgroup `10` defines the primary.
1. Remove any existing hostgroup `2` servers.
2. Add the second replica as a known server to ProxySQL using hostgroup `2`. NOTE: hostgroup `1` defines the primary.
3. Set the `weight` of each server equally. To emulate a percentage, we make the two servers 50, so this mirrors 50%, however any equal value will produce equal load.


```
$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost
$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost -v PROMPT1='ProxySQLAdmin> '
```

```
DELETE FROM pgsql_servers WHERE hostgroup_id=20;
INSERT INTO pgsql_servers (hostgroup_id, hostname, port, status, max_connections, weight)
VALUES (20, 'replica1', 5432, 'ONLINE', 50, 50);
VALUES (2, 'replica1', 5432, 'ONLINE', 50, 50);
INSERT INTO pgsql_servers (hostgroup_id, hostname, port, status, max_connections, weight)
VALUES (20, 'replica2', 5432, 'ONLINE', 50, 50);
VALUES (2, 'replica2', 5432, 'ONLINE', 50, 50);
LOAD PGSQL SERVERS TO RUNTIME;

SELECT * FROM pgsql_servers;
```

NOTE: If you would like to persist these changes then also run `SAVE PGSQL SERVERS TO DISK;`.

## Run Benchmark

To emulate some random load from the three application servers to the databases run the benchmark.
Expand All @@ -42,26 +44,28 @@ $ scripts/summary.sh hosts-per-db

##### For DB 'primary' #####

client_addr | count
---------------+-------
172.113.0.222 | 7
172.113.0.101 | 1
172.113.0.102 | 1
client_addr | count
-------------+-------
10.0.0.222 | 11
10.0.0.3 | 1
10.0.0.2 | 1
(3 rows)


##### For DB 'replica1' #####

client_addr | count
---------------+-------
172.113.0.222 | 34
client_addr | count
-------------+-------
10.0.0.222 | 2
(1 row)


##### For DB 'replica2' #####

client_addr | count
---------------+-------
172.113.0.222 | 26
client_addr | count
-------------+-------
10.0.0.222 | 2
(1 row)
```

Review the ProxySQL stats with commands in [SQL.md](SQL.md).
32 changes: 18 additions & 14 deletions postgresql/ha-proxysql-benchmark/READ-WRITE-SPLIT-BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@ If you have been running other tests, you may need to cleanup database connectio

To emulate load from the three application servers to the Proxy Layer that splits Writes and Reads between the Primary DB and a replica DB we first configure ProxySQL.

1. Add the replica as a known server to ProxySQL using hostgroup `20`. NOTE: hostgroup `10` defines the primary.
2. Add a query rule that directs `SELECT` statements to hostgroup `20`. Queries that do not match will goto the primary.
1. Add the replica as a known server to ProxySQL using hostgroup `2`. NOTE: hostgroup `1` defines the primary.
2. Add a query rule that directs `SELECT` statements to hostgroup `2`. Queries that do not match will goto the primary. NOTE: This is not advised in production. You should use advanced routing rules with a query digest or more specific query pattern.


```
$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost
$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost -v PROMPT1='ProxySQLAdmin> '
```

```
INSERT INTO pgsql_servers (hostgroup_id, hostname, port, status, max_connections, weight)
VALUES (20, 'replica1', 5432, 'ONLINE', 50, 100);
VALUES (2, 'replica1', 5432, 'ONLINE', 50, 100);
LOAD PGSQL SERVERS TO RUNTIME;

SELECT * FROM pgsql_servers;

INSERT INTO pgsql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply)
VALUES (10, 1, '^SELECT.*FOR UPDATE', 10, 1),
(20, 1, '^SELECT', 20, 1);
VALUES (10, 1, '^SELECT.*FOR UPDATE', 1, 1),
(20, 1, '^SELECT', 2, 1);
LOAD PGSQL QUERY RULES TO RUNTIME;

SELECT * FROM pgsql_query_rules;

```

NOTE: These updates are to `RUNTIME` only and will not be persisted between restarts. You would use `TO DISK` in a customer environment.

## Run Benchmark

To emulate some random load from the three application servers to the databases run the benchmark.
Expand All @@ -46,19 +48,19 @@ $ scripts/summary.sh hosts-per-db

##### For DB 'primary' #####

client_addr | count
---------------+-------
172.113.0.222 | 9
172.113.0.101 | 1
172.113.0.102 | 1
client_addr | count
-------------+-------
10.0.0.222 | 8
10.0.0.3 | 1
10.0.0.2 | 1
(3 rows)


##### For DB 'replica1' #####

client_addr | count
---------------+-------
172.113.0.222 | 12
client_addr | count
-------------+-------
10.0.0.222 | 9
(1 row)


Expand All @@ -68,3 +70,5 @@ $ scripts/summary.sh hosts-per-db
-------------+-------
(0 rows)
```

Be sure to [CLEANUP.md](CLEANUP.md) before commencing benchmark before running the [Application Read Load Balancing Benchmark](READ-LOAD-BALANCING-BENCHMARK.md).
14 changes: 7 additions & 7 deletions postgresql/ha-proxysql-benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ This tutorial will launch a three PostgreSQL servers (one primary with two repli
running [sysbench](https://github.com/akopytov/sysbench), an open-source, multi-threaded, and modular benchmarking tool used to evaluate system performance, and a Proxy running [proxysql](https://proxysql.com), an open-source, transparent database proxy.

### Container Topology
For this setup we will use the `172.113.0/24` network with:
For this setup we will use the `10.0.0.0/16` network with:
- Databases
- Primary `.100`
- Replica 1 `.101`
- Replica 2 `.102`
- Primary `.1`
- Replica 1 `.2`
- Replica 2 `.3`
- Application
- Server 1 `.6`
- Server 2 `.16`
- Server 3 `.26`
- Server 1 `.20`
- Server 2 `.21`
- Server 3 `.22`
- Proxy
- ProxySQL `.222`

Expand Down
14 changes: 0 additions & 14 deletions postgresql/ha-proxysql-benchmark/SIMPLE-BENCHMARK.md

This file was deleted.

62 changes: 31 additions & 31 deletions postgresql/ha-proxysql-benchmark/SQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ You can view monitoring of ProxySQL via the

```
. .envrc
docker exec -it proxysql psql -U ${PROXYSQL_ADMIN_USER} -h ${PROXYSQL_DB_HOST} -p ${PROXYSQL_ADMIN_PORT}
docker exec -it proxysql psql -U ${PROXYSQL_ADMIN_USER} -h ${PROXYSQL_DB_HOST} -p ${PROXYSQL_ADMIN_PORT} -v PROMPT1='ProxySQLAdmin> '
```

## Insert Query Rules for SELECT traffic
```sql
DELETE FROM pgsql_query_rules;
INSERT INTO pgsql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply)
VALUES (10, 1, '^SELECT.*FOR UPDATE', 10, 1),
(20, 1, '^SELECT', 20, 1);
VALUES (10, 1, '^SELECT.*FOR UPDATE', 1, 1),
(20, 1, '^SELECT', 2, 1);
LOAD PGSQL QUERY RULES TO RUNTIME;
```

Expand Down Expand Up @@ -42,26 +42,26 @@ For Example
```
hostgroup | digest_text | count_star | sum_time
-----------+----------------------------------------------------------------------+------------+----------
20 | SELECT c FROM sbtest2 WHERE id=$1 | 1519 | 380617
20 | SELECT c FROM sbtest3 WHERE id=$1 | 1493 | 497395
20 | SELECT c FROM sbtest10 WHERE id=$1 | 1403 | 805586
20 | SELECT c FROM sbtest6 WHERE id=$1 | 1354 | 460381
20 | SELECT c FROM sbtest9 WHERE id=$1 | 1333 | 459937
20 | SELECT c FROM sbtest5 WHERE id=$1 | 1283 | 402680
20 | SELECT c FROM sbtest7 WHERE id=$1 | 1231 | 373084
20 | SELECT c FROM sbtest4 WHERE id=$1 | 1203 | 495676
20 | SELECT c FROM sbtest1 WHERE id=$1 | 1073 | 476558
20 | SELECT c FROM sbtest8 WHERE id=$1 | 1033 | 364307
20 | SELECT c FROM sbtest4 WHERE id BETWEEN $1 AND $2 ORDER BY c | 180 | 80777
20 | SELECT c FROM sbtest6 WHERE id BETWEEN $1 AND $2 ORDER BY c | 178 | 65076
20 | SELECT c FROM sbtest9 WHERE id BETWEEN $1 AND $2 ORDER BY c | 177 | 58879
20 | SELECT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 | 174 | 48598
20 | SELECT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c | 172 | 64982
20 | SELECT SUM(k) FROM sbtest6 WHERE id BETWEEN $1 AND $2 | 170 | 48491
20 | SELECT c FROM sbtest2 WHERE id BETWEEN $1 AND $2 | 169 | 31988
20 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN $1 AND $2 ORDER BY c | 167 | 84833
20 | SELECT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 | 166 | 62944
20 | SELECT DISTINCT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 ORDER BY c | 166 | 42421
1 | SELECT c FROM sbtest2 WHERE id=$1 | 1519 | 380617
1 | SELECT c FROM sbtest3 WHERE id=$1 | 1493 | 497395
1 | SELECT c FROM sbtest10 WHERE id=$1 | 1403 | 805586
1 | SELECT c FROM sbtest6 WHERE id=$1 | 1354 | 460381
1 | SELECT c FROM sbtest9 WHERE id=$1 | 1333 | 459937
1 | SELECT c FROM sbtest5 WHERE id=$1 | 1283 | 402680
1 | SELECT c FROM sbtest7 WHERE id=$1 | 1231 | 373084
1 | SELECT c FROM sbtest4 WHERE id=$1 | 1203 | 495676
1 | SELECT c FROM sbtest1 WHERE id=$1 | 1073 | 476558
1 | SELECT c FROM sbtest8 WHERE id=$1 | 1033 | 364307
1 | SELECT c FROM sbtest4 WHERE id BETWEEN $1 AND $2 ORDER BY c | 180 | 80777
1 | SELECT c FROM sbtest6 WHERE id BETWEEN $1 AND $2 ORDER BY c | 178 | 65076
1 | SELECT c FROM sbtest9 WHERE id BETWEEN $1 AND $2 ORDER BY c | 177 | 58879
1 | SELECT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 | 174 | 48598
1 | SELECT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c | 172 | 64982
1 | SELECT SUM(k) FROM sbtest6 WHERE id BETWEEN $1 AND $2 | 170 | 48491
1 | SELECT c FROM sbtest2 WHERE id BETWEEN $1 AND $2 | 169 | 31988
1 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN $1 AND $2 ORDER BY c | 167 | 84833
1 | SELECT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 | 166 | 62944
1 | SELECT DISTINCT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 ORDER BY c | 166 | 42421
```

## Select Traffic Balancing
Expand All @@ -80,18 +80,18 @@ In the example where we are sending different 25% of traffic to one replica, and
```
hostgroup | srv_host | Queries | pct
-----------+----------+---------+-------
20 | replica2 | 17194 | 74.79
20 | replica1 | 5797 | 25.21
10 | primary | 0 |
2 | replica2 | 17194 | 74.79
2 | replica1 | 5797 | 25.21
1 | primary | 0 |

radmin=# select hostgroup_id, hostname, status, weight from pgsql_servers;
radmin=# SELECT hostgroup_id, hostname, status, weight FROM pgsql_servers;
hostgroup_id | hostname | status | weight
--------------+----------+--------+--------
10 | primary | ONLINE | 1
20 | replica1 | ONLINE | 1
20 | replica2 | ONLINE | 3
1 | primary | ONLINE | 1
2 | replica1 | ONLINE | 1
2 | replica2 | ONLINE | 3
```

```
select * from stats_pgsql_query_rules;
SELECT * FROM stats_pgsql_query_rules;
```
Loading