Boost Backend Performance with HashMap, LinkedHashMap, TreeMap & ByteBuffer

This article explores four powerful yet understated caching techniques—HashMap with read‑write locks, LinkedHashMap‑based LRU caches, TreeMap for consistent hashing, and ByteBuffer pooling—detailing their implementations in middleware such as RocketMQ, MyBatis, and Cobar to enhance backend performance.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Boost Backend Performance with HashMap, LinkedHashMap, TreeMap & ByteBuffer

In this article the author discusses less‑used but powerful caching mechanisms in business systems, referring to them as the "sweeping monks of the cache world".

1. HashMap / ConcurrentHashMap Configuration Cache

HashMap is a hash‑table based collection that provides fast insert, lookup and delete operations. It is often the first cache developers encounter, but its basic features are insufficient for production needs such as statistics, expiration, and eviction policies.

In middleware like RocketMQ, HashMap becomes essential. RocketMQ stores routing information of topics in several HashMap instances inside the NameServer. Because HashMap is not thread‑safe, RocketMQ protects updates with ReentrantReadWriteLock —write operations acquire the write lock, read operations acquire the read lock. The NameServer also runs a scheduled task to refresh routing data every 30 seconds.

Key points of the NameServer routing model:

HashMap as the storage container

Read‑write lock to control lock granularity

Scheduled task to periodically update the cache

ConcurrentHashMap provides built‑in thread safety. Before JDK 1.7 it used a segmented‑lock mechanism; since JDK 1.8 it uses an array + linked list + red‑black tree structure together with CAS operations. RocketMQ uses different ConcurrentHashMap instances to store consumer groups, consumption progress, and message filter information. The NameServer does not use ConcurrentHashMap because its routing data spans multiple HashMaps and requires coordinated updates, which are more naturally handled with a read‑write lock.

2. LinkedHashMap LRU Cache

LinkedHashMap extends HashMap by maintaining a doubly‑linked list of entries, preserving either insertion order or access order. When configured for access order, the most recently accessed entry moves to the tail, enabling a natural Least‑Recently‑Used (LRU) eviction policy.

Typical usage is an LRU cache; MyBatis’s second‑level cache implements LRU using LinkedHashMap. The cache is built with the decorator pattern (responsibility chain + decorator) and stores up to 1024 keys by default. When the size exceeds the limit, the eldest entry is removed via an overridden removeEldestEntry method. Because LinkedHashMap is not thread‑safe, MyBatis wraps it with SynchronizedCache to ensure safe concurrent access.

3. TreeMap Sorted Object Cache

TreeMap is an ordered map backed by a red‑black tree, allowing keys to be traversed in sorted order. It is especially useful in consistent hashing algorithms, where nodes (or virtual nodes) must be kept ordered on the hash ring.

Consistent hashing minimizes data movement when nodes are added or removed. TreeMap stores the mapping from hash values to node identifiers, enabling fast lookup of the first node whose hash is greater than or equal to a given key. Adding a new node only affects a small range of keys, preserving the algorithm’s scalability.

Typical steps:

Define a TreeMap<Integer, Node> to hold nodes/virtual nodes.

Initialize the map with physical nodes, the number of virtual nodes per physical node, and a hash function.

When locating a key, compute its hash and find the nearest greater entry in the TreeMap.

4. ByteBuffer Network Programming Buffer Pool

ByteBuffer is a byte‑level buffer used heavily in network programming and file I/O. In the Cobar sharding middleware, each NIOProcessor creates a BufferPool that pools ByteBuffer instances, similar to a database connection pool.

The BufferPool’s core responsibilities are allocation and reclamation of buffers. Pooling reduces the overhead of frequent memory allocation in high‑concurrency scenarios, dramatically improving throughput. Modern frameworks such as Netty provide a more advanced pooled buffer implementation called ByteBuf .

5. Conclusion

The article summarizes four strong yet low‑profile caches:

HashMap + read‑write lock + scheduled updates for configuration caches, with ConcurrentHashMap offering built‑in thread safety.

LinkedHashMap for LRU caches, exemplified by MyBatis’s second‑level cache using the decorator pattern.

TreeMap for ordered storage in consistent hashing, preserving node order on the hash ring.

ByteBuffer pooling via BufferPool to boost network‑programming performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentHashMapConcurrentHashMapByteBufferTreeMapLinkedHashMap
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.