GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Pubsub: Consider a blocking publisher to prevent OOMing · Issue #16 · googleapis/python-pubsub

This repository was archived by the owner on Oct 13, 2026. It is now read-only.
This repository was archived by the owner on Oct 13, 2026. It is now read-only.

Pubsub: Consider a blocking publisher to prevent OOMing #16

Copy link
Copy link
Closed
#96
Closed
Pubsub: Consider a blocking publisher to prevent OOMing#16
#96
Copy link
Assignees
plamut
Labels
api: pubsubIssues related to the googleapis/python-pubsub API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@evanj
evanj
opened on May 16, 2019
Issue body actions

Publishing medium-sized (32 kiB) messages in a loop causes the process's memory usage to quickly run out. I set a 2 GiB memory limit, and the process runs out of memory after publishing about 20000 messages. I believe there are two issues:

  1. There is no limit to the number of queued bytes in the process.
  2. The client library keeps 3 copies of the messages and retains them until the batch "monitor" thread finishes.

As far as I can see, there no good way to use the current client library to publish large batches of messages without HUGE memory consumption. I believe this can be fixed in two ways:

Reduce memory of the current library:

  1. Release messages when the batch is done: In thread.Batch._commit, set self._messages to None after it is published. This seems to make memory consumption better, but I didn't test it super carefully.

  2. Eliminate an extra copy: Change thread.Batch to build a PubsubRequest, and pass that into the gapic.PublisherClient, rather than a list of messages that then need to be copied. This reduces one copy of messages, which seems to make memory consumption better (and possibly make things faster? again not tested carefully)

Back Pressure

Change the publisher to keep a queue of batches, and block after there are more than N in flight at once.

I implemented a client that takes the blocking queue approach. It can publish this workload without ever exceeding 300 MiB of memory. Only 3 in-flight batches were necessary to have the process be CPU bound when running in Google Cloud, so I don't think the queue doesn't even need to be too large.

Environment details

OS: Linux, ContainerOS (GKE), Container is Debian9 (using distroless)
Python: 3.5.3
API: google-cloud-python 0.41.0

Steps to reproduce

  1. Package the following program in a Docker container.
  2. Run it in Kubernetes with a memory limit of 2 GiB
  3. Watch it blow up when you run it.

Code example

#!/usr/bin/env python3
'''
Create the topic and a subscription:

gcloud pubsub topics create deleteme_publish_memleak_test
gcloud pubsub subscriptions create deleteme_memleak_subscription --topic=deleteme_publish_memleak_test
'''

from google.cloud import pubsub_v1
import argparse
import google.auth
import logging
import time


def main():
    parser = argparse.ArgumentParser(description='mass publish to pub/sub')
    parser.add_argument('--topic',
                        default='deleteme_publish_memleak_test',
                        help='topic to publish to')
    parser.add_argument('--messages',
                        type=int,
                        default=200000,
                        help='number of messages to publish')
    parser.add_argument('--bytes_per_message',
                        type=int,
                        default=32 * 1024,
                        help='bytes per message to publish')
    parser.add_argument('--log_every_n', type=int, default=2000, help='log every N messages')
    args = parser.parse_args()

    credentials, project_id = google.auth.default()
    publish_client = pubsub_v1.PublisherClient(credentials=credentials)
    topic_path = publish_client.topic_path(project_id, args.topic)

    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s %(levelname)s %(name)s: %(message)s',
    )
    logging.info('publishing %d messages to topic %s; %d bytes per message; %.1f total MiB',
                 args.messages, topic_path, args.bytes_per_message,
                 (args.messages * args.bytes_per_message) / 1024. / 1024.)

    start = time.time()
    for i in range(args.messages):
        message = (i % 256).to_bytes(1, byteorder='little') * args.bytes_per_message
        publish_client.publish(topic_path, message)
        if i % args.log_every_n == 0:
            logging.info('published %d messages', i)

    end = time.time()
    logging.info('done publishing in %f s', end - start)


if __name__ == '__main__':
    main()

Activity

  1. sduskis
    assigned
    plamut
    on May 17, 2019
  2. sduskis commented on May 17, 2019

    @sduskis
    sduskis
    on May 17, 2019
    Contributor
    More actions

    @evanj, thanks for raising the issue, and the clear explanation.

    @plamut can you please check this out? The only thing we can likely tackle in the near term is the extra copies.

    We'd need to reevaluate the current non-blocking approach, which is the default across all clients. It would be worthwhile to explore a blocking option in the long term.

    @kamalaboulhosn we'll need you to make the call about this feature.

  3. sduskis
    changed the title [-]pubsub: publishing in a loop uses TONS of memory[/-] [+]pubsub: Consider a blocking publisher to prevent OOMing[/+] on May 17, 2019
  4. tseaver
    changed the title [-]pubsub: Consider a blocking publisher to prevent OOMing[/-] [+]Pubsub: Consider a blocking publisher to prevent OOMing[/+] on May 20, 2019
  5. sduskis
    unassigned
    plamut
    on Jun 11, 2019
  6. dangermike commented on Jul 3, 2019

    @dangermike
    dangermike
    on Jul 3, 2019
    More actions

    @sduskis you have unassigned @plamut. @kamalaboulhosn: What was the determination about creating a back-pressure mechanism?

  7. kamalaboulhosn commented on Jul 10, 2019

    @kamalaboulhosn
    kamalaboulhosn
    on Jul 10, 2019
    Contributor
    More actions

    I think it's something we'll still need to discuss. We had considered it at some point. We also considered exposing the stats for the outstanding requests so that the user could make the decision about what they wanted to do when there were a lot of outstanding sends.

  8. evanj commented on Jul 11, 2019

    @evanj
    evanj
    on Jul 11, 2019
    Author
    More actions

    FWIW, I believe the official Go client will return an error when the number of buffered bytes exceeds a threshold, by default: 10 * maxMessageLength: https://github.com/googleapis/google-cloud-go/blob/master/pubsub/topic.go#L103

  9. plamut
    transferred this issue fromgoogleapis/google-cloud-pythonon Jan 31, 2020
  10. product-auto-label
    added
    api: pubsubIssues related to the googleapis/python-pubsub API.
    on Jan 31, 2020
  11. yoshi-automation
    added
    🚨This issue needs some love.
    triage meI really want to be triaged.
    on Jan 31, 2020
  12. plamut
    added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Jan 31, 2020
  13. yoshi-automation
    removed
    triage meI really want to be triaged.
    🚨This issue needs some love.
    on Jan 31, 2020
  14. plamut
    self-assigned this
    on May 7, 2020
  15. plamut commented on May 7, 2020

    @plamut
    plamut
    on May 7, 2020
    Contributor
    More actions

    @evanj I have an update on this, #96 will add the desired functionality to the publisher client.

    It will allow configuring the limits for in-flight messages being published, and the action to take if these limits are exceeded. The default action is IGNORE, but one will also be able to specify BLOCK or ERROR.

    Reacted by Marcel Schoffelmeer
  16. evanj commented on May 8, 2020

    @evanj
    evanj
    on May 8, 2020
    Author
    More actions

    Amazing! Thank you!

  17. plamut
    closed this as completedin #96on Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @plamut
    plamut

Labels

api: pubsubIssues related to the googleapis/python-pubsub API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.