eu-federation-gateway-service / efgs-federation-gateway

The goal of this project is to develop the official European solution for the interoperability between national backend servers of decentralised contact tracing applications to combat COVID-19.
Apache License 2.0
59 stars 25 forks source link

Probable performance issue in DbEncryptionService.java #271

Closed a-trzewik closed 2 years ago

a-trzewik commented 3 years ago

Describtion

DbEncryptionService use synchronize block for encryption.

private byte[] encrypt(byte[] plain) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException { synchronized (cipher) { cipher.init(Cipher.ENCRYPT_MODE, key, getInitializationVector()); return cipher.doFinal(plain); } }

This limit the server to enrypt only one thing in the time. All other threads will block Because the encryption is done for many column fields in the time it will have inpact on data insert performance, which now can be done only in sequence

Expected behaviour

Do not block here

Possible Fix

Please create bunch on cipher(more or less depending on available processors) and reuse them in parallel. You may use ConcurrentLinkedDeque to hold the ciphers for it So take chiper from queue use it and put it back see https://www.baeldung.com/java-lifo-thread-safe Perhats there is already something in spring for it.