Concurrent REST Api

1,043 views
Skip to first unread message

Abhinav Srivastava

unread,
Mar 21, 2016, 4:41:13 PM3/21/16
to golang-nuts
Hi, 

I am new to Golang and trying to create a REST api that can handle concurrent web requests; could not find a good tutorial. What if 1000's of clients hit the api at the same time? How can I handle that? Any example would be helpful. I know server spins a goroutine for every new request but what about making the whole processing parallel.

Thanks
-Abhi

Shawn Milochik

unread,
Mar 21, 2016, 4:44:10 PM3/21/16
to golang-nuts
If you can handle two requests simultaneously -- and properly -- you can handle 1,000 just as well.

Making use channels, and the sync.Mutex or sync.RWMutex to avoid concurrent access of the same variables will help you keep everything humming along.


Tim Shannon

unread,
Mar 22, 2016, 9:19:54 AM3/22/16
to golang-nuts, Sh...@milochik.com
Yep, what Shawn said, and the best way to test this is to build with the -race flag.  It'll throw errors if you have concurrent access improperly guarded.

Tim Hawkins

unread,
Mar 22, 2016, 9:28:57 AM3/22/16
to Abhinav Srivastava, golang-nuts

Check out this tool

https://blog.micro.mu/2016/03/20/micro.html

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leonel Quinteros

unread,
Mar 22, 2016, 9:57:06 AM3/22/16
to golang-nuts
Any net/http example will work for you, as you said, the server will spin a new goroutine for each connection, so you don't have to worry about handling the connections, but to make the parallel processing the right way as Shawn said. 

You can also use any framework, either as a code reference or to implement your API. Check out mine for either purpose: https://github.com/yarf-framework/yarf

The code isn't long and it should be very straightforward to follow. 

Sankar

unread,
Apr 10, 2016, 3:14:07 AM4/10/16
to golang-nuts
I am new to Golang and trying to create a REST api that can handle concurrent web requests; could not find a good tutorial. What if 1000's of clients hit the api at the same time? How can I handle that?

You could put your Golang server on multiple machines and behind a load balancer. Your code needs to be stateless in that case though. Shameless plug for my blogpost: http://psankar.blogspot.in/2016/03/programmers-guide-to-microservicessoa.html 
Reply all
Reply to author
Forward
0 new messages