matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.83k stars 2.12k forks source link

Key upload seems slow on matrix.org #4395

Open jryans opened 5 years ago

jryans commented 5 years ago

Uploading keys as part of E2E key backup seems slow to me. Here's a capture of my recent attempt on matrix.org with PII removed:

2019-01-15 20:15:48,131 - synapse.access.http.8080 - 302 - INFO - PUT-34926278 - <IP> - 8080 - {@jryans:matrix.org} Processed request: 5.185sec/0.003sec (0.100sec, 0.024sec) (0.968sec/2.317sec/403) 2B 200 "PUT /_matrix/client/r0/room_keys/keys?version=16 HTTP/1.1" <UA> [0 dbevts]
2019-01-15 20:15:51,011 - synapse.access.http.8080 - 302 - INFO - PUT-34926997 - <IP> - 8080 - {@jryans:matrix.org} Processed request: 1.987sec/0.001sec (0.080sec, 0.048sec) (0.407sec/0.925sec/403) 2B 200 "PUT /_matrix/client/r0/room_keys/keys?version=16 HTTP/1.1" <UA> [0 dbevts]
2019-01-15 20:15:54,246 - synapse.access.http.8080 - 302 - INFO - PUT-34927329 - <IP> - 8080 - {@jryans:matrix.org} Processed request: 2.315sec/0.000sec (0.084sec, 0.036sec) (0.390sec/1.173sec/403) 2B 200 "PUT /_matrix/client/r0/room_keys/keys?version=16 HTTP/1.1" <UA> [0 dbevts]
2019-01-15 20:15:54,989 - synapse.access.http.8080 - 302 - INFO - PUT-34927390 - <IP> - 8080 - {@jryans:matrix.org} Processed request: 2.736sec/0.001sec (0.052sec, 0.004sec) (0.172sec/0.339sec/202) 2B 200 "PUT /_matrix/client/r0/room_keys/keys?version=16 HTTP/1.1" <UA> [0 dbevts]
2019-01-15 20:15:55,022 - synapse.access.http.8080 - 302 - INFO - PUT-34927676 - <IP> - 8080 - {@jryans:matrix.org} Processed request: 0.195sec/0.004sec (0.000sec, 0.004sec) (0.007sec/0.013sec/3) 2B 200 "PUT /_matrix/client/r0/room_keys/keys?version=16 HTTP/1.1" <UA> [0 dbevts]

Request body size:

Looking at the first request for example, we have:

jryans commented 5 years ago

Each request contains up to 200 sessions to store keys for.

Looking at upload_room_keys:

https://github.com/matrix-org/synapse/blob/aa955f2d15ae0367cd698d14788135a89272b297/synapse/handlers/e2e_room_keys.py#L164-L169

we may want to do what this comment suggests and add the whole batch at once, instead of doing separate DB inserts for each session.