Skip to content

Encryption performance improvement on Java client #23121

Closed

Description

@ocadaruma

Search before asking

  • I searched in the issues and found nothing similar.

Motivation

We found that using encryption will add significant performance overhead and could be the bottleneck especially in high-throughput production/consumption.

The micro benchmark shows that MessageCryptoBc#encrypt throughput is only around 2k ops/s in below setting:

Setting

  • Pulsar version: 3.4.0-SNAPSHOT (commit: f3c177e)
  • JDK version: Temurin JDK21.0.4 x86_64
  • CPU: Xeon Silver 4210 2.20GHz
  • Payload size: 32KB

Benchmark result

Benchmark                                Mode  Cnt      Score     Error  Units
MessageCryptoBcBenchmark.encryptBc      thrpt   10   1972.095 ±  72.436  ops/s

Solution

MessageCryptoBc uses AES-GCM for the data encryption.

Hotspot JVM has intrinsics to use hardware AES instruction from JDK8 and upwards for SunJCE security provider.
By switching AES-GCM cipher to use SunJCE provider instead of BouncyCastle provider, the throughput is improved by x23.

Benchmark result

Benchmark                                Mode  Cnt      Score     Error  Units
MessageCryptoBcBenchmark.encryptBc      thrpt   10   1972.095 ±  72.436  ops/s
MessageCryptoBcBenchmark.encryptSunJCE  thrpt   10  46490.933 ± 762.167  ops/s

Alternatives

No response

Anything else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. added
    type/enhancement
    on Aug 4, 2024
  2. hpvd commented on Aug 4, 2024

    @hpvd

    awsome finding. Do you also have a direct comparison of "no encryption" vs "encryption using SunJCE provider"?

  3. ocadaruma commented on Aug 4, 2024

    @ocadaruma
    ContributorAuthor

    @hpvd below is the pulsar-perf result on standalone pulsar.

    # No encryption
    % bin/pulsar-perf produce persistent://public/default/test-topic -r 2147483647 -time 10
    ...
    Aggregated throughput stats --- 2590389 records sent --- 128708.152 msg/s --- 1005.532 Mbit/s
    
    # Encryption with SunJCE
    % bin/pulsar-perf produce persistent://public/default/test-topic -r 2147483647 -time 10 -k test-key -v $PWD/test_ecdsa_pubkey.pem
    ...
    Aggregated throughput stats --- 2368008 records sent --- 117528.344 msg/s --- 918.190 Mbit/s
    
    # Encryption with BouncyCastle
    % bin/pulsar-perf produce persistent://public/default/test-topic -r 2147483647 -time 10 -k test-key -v $PWD/test_ecdsa_pubkey.pem
    ...
    Aggregated throughput stats --- 410815 records sent --- 20478.432 msg/s --- 159.988 Mbit/s
    
  4. hpvd commented on Aug 4, 2024

    @hpvd

    @ocadaruma many thanks - this looks great! Are there other places in pulsar where this could be relevant?

  5. ocadaruma commented on Aug 4, 2024

    @ocadaruma
    ContributorAuthor

    @hpvd decryption performance on consumer-side will be also improved

  6. lhotari commented on Mar 26, 2026

    @lhotari
    Member

    @hpvd decryption performance on consumer-side will be also improved

    @ocadaruma This PR contains some improvements to consumer-side decryption performance: #25400. This impacts the case when there are multiple producers and multiple public keys used in the producers. Currently the decryption is very poor in this type of real-world scenario. If the decryption fails, it will retry with the next key that succeeds. CPU will be unnecessarily spent in these attempts. In the Java client implementation, the producer will use the same key for 4 hours before rotating the key. That's why it's useful to cache the decrypted key by producer name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions