Praneel PIDIKITI
Praneel PIDIKITI

Reputation: 19517

What is the difference between Caching and Pooling?

What is the difference between Caching and Pooling?

Upvotes: 25

Views: 9345

Answers (6)

xiaozhiliaoo
xiaozhiliaoo

Reputation: 451

Java code Cache interface:

public interface Cache { Resource acquire(Identity id)}  // implement usually Map

Pool interface:

public interface Pool { Resource acquire() }  // implement usually List

They use for resourse(memory,connection,thread etc..) reuse. It means cache must has identity to find,but pool need`t it. so get resource from pool is transparent. the example is a lot of. memory pool, memory cahce, buffer cache pool, connection pool.

Upvotes: 0

stdout
stdout

Reputation: 2651

Both for reusing and reducing memory footprint of the process (also getting rid of the object creation overhead). One notable diff is a pool uses the same objects over and over again during the lifetime of your context where in a cache (at least in LRU) you evict objects to make space for newly created ones.

Another diff would be the answer of the following question. Would you like to be returned a specific object or any object would be fine? The answer make it clear what you need - cache or pool.

Upvotes: 2

Tom
Tom

Reputation: 488

Cache - store frequently used values, typically because the lookup and/or creation is non-trivial. e.g. if a lookup table from a database is frequently used, or values are read from a file on disk, it's more efficient to keep it in memory and refresh it periodically.

A cache only manages object lifetime in the cache, but does not impose semantics on what is held in the cache. A cache also doesn't create the items, but just stores objects.

Pool - term to describe a group of resources that are managed by the pool itself. e.g. (Database) Connection Pool - When a connection is needed it is obtained from the pool, and when finished with is returned to the pool.

The pool itself handles creation and destruction of the pooled objects, and manages how many objects can be created at any one time.

Pools are typically used to reduce overhead and throttle access to resources. You wouldn't want every servlet request opening a new connection to the database. Because then you have a 1:1 relationship between active requests and open connections. The overhead of creating an destroying these connections is wasteful, plus you could easily overwhelm your database. by using a pool, these open connections can be shared. For example 500 active requests might be sharing as little as 5 database connections, depending on how long a typical request needs the connection.

Cache Pool - mostly seems to describe the number of (independent?) cache's that exist. E.g. an asp.net application has 1 cache per Application Domain (cache isn't shared between asp.net applications). Literally a pool of caches, although this term seems to be used rarely.

Upvotes: 32

RQDQ
RQDQ

Reputation: 15569

Caching usually refers to holding onto a static copy of a data for quick retrieval (with the assumption that retrieval or calculation of the value is expensive).

Pooling usually refers to keeping a number of resources around for quick usage (with the assumption that the creation and or disposal of these resources is expensive).

Upvotes: 2

Haakon
Haakon

Reputation: 1741

Both aim for object reuse. The distinction is usually drawn along statefulness; a pool is a collection of stateless objects, a cache is one of stateful objects. See this explanation.

Upvotes: 6

Oded
Oded

Reputation: 499012

Caching is saving a value/object for reuse - normally to save resources.

Wikipedia says:

a cache is a component that transparently stores data so that future requests for that data can be served faster.

Polling is similar, where you have a number of such objects (a pool) - once an object has been taken out of the pool and used, it is returned to the pool for later reuse.

Wikipedia says:

A pool in computer science is a set of initialised resources that are kept ready to use, rather than allocated and destroyed on demand.

Upvotes: 9

Related Questions