Skip to content

Analysis: Making bulk_operations the Default Behavior

Current bulk_operations Behavior

  1. Logging set to WARNING level - No INFO/DEBUG logs during construction
  2. Batch registration - Constructs registered in groups of 100
  3. Deferred ID assignment - IDs assigned when batch flushes

Developer Experience Impact

Pros ✅

  1. Massive Performance Boost
  2. 74.5% faster for typical workloads
  3. Near-instant infrastructure definition
  4. Better experience for large infrastructures

  5. Cleaner Output

  6. No verbose construction logs by default
  7. Only see warnings and errors
  8. Less noise in CI/CD pipelines

  9. Production-Ready

  10. Most production code doesn't need construction logs
  11. Aligns with "quiet by default" philosophy
  12. Similar to how Terraform doesn't log every resource

Cons ❌

  1. Lost Debugging Visibility
  2. Can't see which construct is being created
  3. Harder to debug construction order issues
  4. No progress indication for large infrastructures
  5. Stack traces less informative without context

  6. Deferred ID Assignment Issues

    # This pattern would break:
    resource = Resource(stack, "bucket", type="aws_s3_bucket")
    print(resource._unique_id)  # Would print "__pending__"
    

  7. Batch Registration Complexity

  8. Constructs created in different contexts might batch unexpectedly
  9. Harder to reason about when IDs are assigned
  10. Potential for subtle bugs

  11. Learning Curve

  12. New users expect to see what's happening
  13. Harder to understand construct tree building
  14. Less educational for learning the framework

Potential Solutions

1. Smart Defaults Based on Environment

class App:
    def __init__(self, *args, **kwargs):
        # Default to bulk mode in production-like environments
        if os.getenv("CI") or not sys.stdout.isatty():
            self.default_bulk_mode = True
        else:
            # Interactive development gets verbose logs
            self.default_bulk_mode = False

2. Progress Indicator for Large Operations

class BulkOperationContext:
    def __init__(self, stack, show_progress=True):
        self.show_progress = show_progress and sys.stdout.isatty()
        self._progress_bar = None

    def add_construct_for_batch(self, construct):
        if self.show_progress and len(self._pending_constructs) % 100 == 0:
            print(f"\rCreated {len(self._pending_constructs)} resources...", end="")

3. Configurable Verbosity Levels

class App:
    def __init__(self, verbosity="auto"):
        # verbosity: "quiet", "normal", "verbose", "auto"
        self.verbosity = verbosity

        if verbosity == "quiet":
            self.default_bulk_mode = True
        elif verbosity == "verbose":
            self.default_bulk_mode = False
        elif verbosity == "auto":
            # Smart detection based on environment
            pass

4. Keep Explicit Control

# Don't change default, but make it easier to enable
app = App(bulk_mode=True)  # Simple flag instead of context manager

A setting would make it a flag and a [tool.nf.config] key too, as every setting is (see Settings); nothing is read from the environment.

Recommendation

Don't make bulk_operations the default, but instead:

  1. Add a simple bulk_mode parameter to App:

    app = App(bulk_mode=True)  # Applies to entire app
    

  2. Make it a setting (a flag and a [tool.nf.config] key):

    [tool.nf.config]
    bulk_mode = true
    

  3. Document performance tips prominently:

  4. Add performance section to README
  5. Show bulk_mode in examples for large infrastructures
  6. Explain the trade-offs clearly

  7. Improve logging performance:

  8. Check log level before formatting strings
  9. Use lazy formatting: logger.info("Creating %s", class_name)
  10. Consider structured logging that's more efficient

  11. Add progress indication:

    with app.bulk_mode(show_progress=True):
        # Shows: "Creating resources... 500/1000"
    

Example Implementation

class App:
    def __init__(
        self,
        outdir: str = "cdk.out",
        skip_validation: bool = False,
        lazy_validation: bool = False,
        bulk_mode: bool | None = None,
    ):
        # Auto-detect bulk mode if not specified
        if bulk_mode is None:
            bulk_mode = get_config_value("bulk_mode", default=False) or os.getenv("CI") is not None or not sys.stdout.isatty()

        self.bulk_mode = bulk_mode
        self.lazy_validation = get_config_value("lazy_validation", default=False, app_value=lazy_validation)

        # Apply bulk mode to all stacks by default
        if self.bulk_mode:
            logger.logger.setLevel(logging.WARNING)

Conclusion

Making bulk_operations the default would provide significant performance benefits but at the cost of debugging visibility and developer experience. Instead, we should:

  1. Make it trivially easy to enable (single parameter or env var)
  2. Auto-enable in CI/production environments
  3. Keep verbose logging for interactive development
  4. Add progress indicators for better UX
  5. Document the performance benefits prominently

This gives developers the best of both worlds: fast production builds and debuggable development experience.