Object Pool Pattern You Should Know in Golang(Design Patterns 05)

The Cache That Keeps on Giving: Understanding Golang’s Object Pool Pattern

Contact Author@:Medium,LinkedIn,Twitter

golang golang-object-pool-pattern(from github.com/MariaLetta/free-gophers-pack)|300

Hello, here is Wesley, Today’s article is about object pool pattern in Go. Without further ado, let’s get started.💪

The object pool pattern is a creation-type design pattern that aims to improve performance by reusing objects, reducing memory allocation and deallocation overhead. In high-concurrency application scenarios, the cost of creating and destroying objects can be very high, so using an object pool can significantly boost application efficiency.

In Go (Golang), due to the language’s concurrency features and standard library support, we can implement this pattern in a concise and efficient way.

Background

In high-performance applications, resource allocation and deallocation may become a system bottleneck. For example:

  • Creating and destroying database connections.
  • Frequent allocation of many small objects, leading to increased garbage collection pressure.
  • Repeated establishment and disconnection of network connections.

The object pool pattern avoids unnecessary resource allocation and deallocation by pre-creating a set of objects and storing them in a pool. When these objects are needed, the client can borrow from the pool, use them, and then return them.

Code Implementation

Reference: [Object Pool Pattern - Wikipedia](https://en.wikipedia.org/wiki/Object_pool_pattern
,Using channels to implement a simple pool:

Main function:

Output:

1
2
3
4
5
6
7
8
9
...
2024/12/09 00:25:10 using resource #2 finished work 32 finish
2024/12/09 00:25:10 using resource #2 finished work 25 finish
2024/12/09 00:25:10 get resource time out
2024/12/09 00:25:10 get resource time out
...
2024/12/09 00:25:10 using resource #0 finished work 30 finish
2024/12/09 00:25:10 using resource #4 finished work 34 finish
...

Golang sync.Pool

Golang’s standard library provides sync.Pool, a concurrent-safe object pool for reusing temporary objects. Compared to traditional languages that implement object pools through complex wrapping, Go’s implementation is lighter and more closely matches actual requirements.

Output:

1
2
3
4
Get: &{ID:1 Name:Resource-1}, 0x1400000c030
Get: &{ID:2 Name:Resource-2}, 0x1400000c060
Get: &{ID:0 Name:} (reused: true)
Get: &{ID:0 Name:} (reused: true)

Key Design Points

  1. Resetting Object State
    When returning an object to the pool, we clear its fields to avoid the impact of dirty data.
  2. Concurrency Safety
    sync.Pool is inherently concurrency-safe and suitable for reusing resources in a multi-Goroutine environment.
  3. On-Demand Creation
    The New method of sync.Pool allows on-demand creation of objects, without manual initialization.

Use Cases

  1. High-Frequency Object Reuse
    For example, temporary buffers in HTTP request handling.
  2. Network Connection Pooling
    For managing database connections or WebSocket connections, similar approaches can be used.
  3. Reducing Garbage Collection Pressure
    In scenarios where a large number of objects are created within a short time frame, reusing objects can reduce the overhead of garbage collection.

Notes

1. No Guarantee of Object Survival

The objects in sync.Pool may be cleared by garbage collection (GC) at any time, so it’s not guaranteed that an object obtained from the pool will still be available for use next time. This means that if you don’t persistently retain objects in the pool under high load conditions, you may encounter cases where the object is zero-valued. For example, if you obtain an object from the pool and use it, then put it back into the pool, the next time you get it, you may receive a new instance instead of the previous one.

2. Concurrency Safety

Although sync.Pool is thread-safe and can be safely used in multiple goroutines, users must be careful when modifying the state of an object after obtaining it from the pool, as other goroutines may simultaneously obtain the same object. If multiple goroutines access the same object without proper synchronization mechanisms, data competition and inconsistencies may occur.

3. Not Suitable for Long-Lived Objects

The design intention of sync.Pool is to cache temporary objects, so it’s not suitable for long-lived objects that need to maintain their state over a long period (e.g., database connections or network connections). If an object needs to retain its state for a long time, consider using other data structures (such as maps or custom structures) to manage these objects.

4. Object Lifetime Management

When using sync.Pool, ensure that the objects put into the pool are reusable and do not contain sensitive information before they are taken out of the pool. Users must manually manage the lifetime of objects, ensuring that necessary resetting is performed before calling the Put() method to avoid returning dirty data.

5. Avoid Over-Optimization

While sync.Pool can improve performance, over-reliance on it for performance optimization may introduce additional complexity and maintenance costs. In some cases, the actual performance improvement may not be noticeable, so when deciding whether to use sync.Pool, weigh its complexity against its performance benefits.

Summary

Golang’s sync.Pool is a natural choice for implementing the object pool pattern, offering simplicity, ease of use, and good concurrency performance. In practical applications, an object pool can significantly improve performance, especially in high-concurrency and high-frequency resource allocation scenarios. However, it’s not without risks, and developers should exercise caution when using it in actual applications.

More

Recent Articles:

Random Article:


More Series Articles about You Should Know In Golang:

https://wesley-wei.medium.com/list/you-should-know-in-golang-e9491363cd9a

And I’m Wesley, delighted to share knowledge from the world of programming. 

Don’t forget to follow me for more informative content, or feel free to share this with others who may also find it beneficial. it would be a great help to me.

Give me some free applauds, highlights, or replies, and I’ll pay attention to those reactions, which will determine whether or not I continue to post this type of article.

See you in the next article. 👋

中文文章: https://programmerscareer.com/zh-cn/golang-object-pool-pattern/
Author: Medium,LinkedIn,Twitter
Note: Originally written at https://programmerscareer.com/golang-object-pool-pattern/ at 2024-12-08 20:49.
Copyright: BY-NC-ND 3.0

Chain Of Responsibility Pattern You Should Know in Golang(Design Patterns 06) Prototype Pattern You Should Know in Golang(Design Patterns 04)

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×