NOTE

Designing Object Pooling

A historical note on pooling, its parameters, capacity sizing, and common applications.

System DesignCreated Updated 2 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is Pooling?

Create objects in advance and store them in a pool. When an object is needed, get it from the pool instead of creating it; after use, return it to the pool instead of destroying it.

2. Why Do We Need Pooling?

  • Frequently creating and destroying objects is expensive.
  • Objects consume resources, so their number must be limited; otherwise resources may be exhausted.

3. Disadvantages of Pooling

Pooling trades space for time, so it consumes memory.

4. How to Design Pooling

Four key parameters: minimum idle objects + maximum idle objects + maximum objects + timeout.

  • Current object count < minimum idle object count
    • Create a new object and put it into the object pool.
  • Current object count > minimum idle object count
    • If there is an idle object, use it.
    • If there is no idle object:
      • If current object count < maximum idle object count, create a new object and put it into the object pool.
      • If current object count > maximum idle object count:
        • If current object count < maximum object count, create a new object but do not put it into the pool; destroy it after use.
        • If current object count > maximum object count, wait for an idle object.
  • For idle objects above the minimum idle count, destroy them after the timeout is exceeded.

4.1. How to Determine the Maximum Object Count

Same as the deployment section of Business System Design Method:

  1. Calculate the QPS a single object can support. See Load Testing.
  2. Maximum object count = estimated business QPS / QPS supported by one object + some headroom.

4.2. How to Determine the Maximum Idle Object Count

If burst traffic exists, set it the same as the maximum object count; otherwise set it smaller.

4.3. How to Determine the Minimum Idle Object Count

4.4. How to Determine the Timeout

4.5. How to Determine

5. Applications of Pooling

5.1. Thread Pool

14.ThreadPool.md

5.2. Goroutine Pool

Goroutine Pool

5.3. TCP Connection Pool

How to Design a TCP Connection Pool

6. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub