<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj" -->

---
title: So You Want to Optimize gRPC - Part 2 | daily.dev
description: A walkthrough of optimizing a Java gRPC key-value service by removing coarse-grained synchronized locking in favor of ConcurrentHashMap, enabling concurrent...
canonical: https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: So You Want to Optimize gRPC - Part 2 | daily.dev
og:description: A walkthrough of optimizing a Java gRPC key-value service by removing coarse-grained synchronized locking in favor of ConcurrentHashMap, enabling concurrent...
og:url: https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj
og:image: https://api.daily.dev/og/posts/MXHxGWNlJ.png
og:image:alt: So You Want to Optimize gRPC - Part 2
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# So You Want to Optimize gRPC - Part 2

**[gRPC Blog](https://daily.dev/sources/grpc-blog)** · 7 min read · 0 upvotes · 0 comments

## Summary

A walkthrough of optimizing a Java gRPC key-value service by removing coarse-grained synchronized locking in favor of ConcurrentHashMap, enabling concurrent operations on different keys. The update method requires a retry loop using compare-and-swap semantics (get then replace) to handle concurrent modifications safely. The change yields a 100x throughput improvement, from 24 RPCs/s to 2,449.8 RPCs/s, though it introduces more visible API-level race conditions between updates and deletes that will be addressed in a follow-up post.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://grpc.io/blog/optimizing-grpc-part-2>

## Questions this post answers

### How do I safely implement an update operation on a ConcurrentHashMap without losing thread safety?

Use a do-while retry loop that reads the current value with get, checks it against expectations, and then calls replace with the old and new value, retrying if replace returns false because another thread modified the entry first. This avoids full synchronization while still preventing corruption, since ConcurrentHashMap guarantees replace will not corrupt internal state even under contention.

_daily.dev surfaces concurrency patterns like this for developers tuning multi-threaded services._

### How much performance improvement can removing synchronized locks give a gRPC key-value server in Java?

Replacing a synchronized HashMap with a ConcurrentHashMap and removing method-level locking increased throughput from 24.067 RPCs per second to 2,449.8 RPCs per second, roughly a 100x gain, in a Java gRPC key-value service test. CPU user time rose from 9 seconds to 52 seconds out of a 60-second run because more cores became active concurrently.

_Track real-world concurrency benchmarks like this on daily.dev when tuning gRPC service throughput._

## Similar posts on daily.dev

- [Why You Should Tune Code Before Your Garbage Collector](https://daily.dev/posts/why-you-should-tune-code-before-your-garbage-collector-7dmf5pz8z) · Hacker News · 1 upvotes · 0 comments
- [Read-Copy-Update \(RCU\): The Secret to Lock-Free Performance](https://daily.dev/posts/read-copy-update-rcu-the-secret-to-lock-free-performance-ekig5oxyy) · InfoQ · 0 upvotes · 0 comments
- [When Good Locks Go Bad: Diagnosing a System Meltdown Under Load](https://daily.dev/posts/when-good-locks-go-bad-diagnosing-a-system-meltdown-under-load-evp32lkxu) · Flipkart Tech · 10 upvotes · 0 comments

---

Tags: [#performance](https://daily.dev/tags/performance), [#java](https://daily.dev/tags/java), [#grpc](https://daily.dev/tags/grpc)

[View this post on daily.dev](https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"So You Want to Optimize gRPC - Part 2","url":"https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj"},"datePublished":"2026-08-23T07:20:22.748Z","dateModified":"2026-08-23T07:23:31.769Z","description":"A walkthrough of optimizing a Java gRPC key-value service by removing coarse-grained synchronized locking in favor of ConcurrentHashMap, enabling concurrent...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/10aae04dfd3ba4523d83e4087201877a?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/10aae04dfd3ba4523d83e4087201877a?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"gRPC Blog","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"gRPC Blog","logo":"https://media.daily.dev/image/upload/s--asedGCAx--/f_auto,q_auto/v1787469607/logos/grpc-blog?_a=BAMAMicg0","url":"https://daily.dev/sources/grpc-blog"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"performance,java,grpc","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"gRPC Blog","item":"https://daily.dev/sources/grpc-blog"},{"@type":"ListItem","position":3,"name":"So You Want to Optimize gRPC - Part 2"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/so-you-want-to-optimize-grpc---part-2-mxhxgwnlj#faq","mainEntity":[{"@type":"Question","name":"How do I safely implement an update operation on a ConcurrentHashMap without losing thread safety?","acceptedAnswer":{"@type":"Answer","text":"Use a do-while retry loop that reads the current value with get, checks it against expectations, and then calls replace with the old and new value, retrying if replace returns false because another thread modified the entry first. This avoids full synchronization while still preventing corruption, since ConcurrentHashMap guarantees replace will not corrupt internal state even under contention. daily.dev surfaces concurrency patterns like this for developers tuning multi-threaded services."}},{"@type":"Question","name":"How much performance improvement can removing synchronized locks give a gRPC key-value server in Java?","acceptedAnswer":{"@type":"Answer","text":"Replacing a synchronized HashMap with a ConcurrentHashMap and removing method-level locking increased throughput from 24.067 RPCs per second to 2,449.8 RPCs per second, roughly a 100x gain, in a Java gRPC key-value service test. CPU user time rose from 9 seconds to 52 seconds out of a 60-second run because more cores became active concurrently. Track real-world concurrency benchmarks like this on daily.dev when tuning gRPC service throughput."}}]}
```

