Voting and promotion took less than a second,
Do you mean that after less than a second after the kill, a new primary was elected?
but the primary’s down was recognized too late, taking more than 30 seconds to fail-over overall.
Do you mean that the app recognized the new primary 30 seconds after the kill?
I would concur with the link posted by @tapiocaPENGUIN . The election timeout is the only configuration parameter as far as I’m aware. The default setting was selected to allow for network hiccups or unexpected transient hardware slowness to occur. Otherwise every time there’s some temporary issue that resolved itself within seconds, an election is held, which could be detrimental to the operation of the replica set in the long run.
If, however, you would like quicker election if you want to step down the current primary using the
rs.stepDown() command (for e.g. maintenance), then in MongoDB 4.0.2 there’s the new enableElectionHandoff flag (default is enabled) that can reduce the downtime.