Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy for mitigating race conditions when updating cookies?

So the situation is that on a given page there are many HTTP requests and each of these need to update a single shared cookie. There is no possibility to control how many requests are made per page, but realistically there can be 10+.

We've seen issues where only the last http request successfully updates the cookie and I am told this is due to browser dependencies.

Is this a known issue in general and are there any strategies for mitigating the risk?

like image 973
Dan Avatar asked Aug 09 '11 20:08

Dan


People also ask

What are recommended ways to avoid race condition?

Race conditions can be avoided by proper thread synchronization in critical sections. Thread synchronization can be achieved using a synchronized block of Java code. Thread synchronization can also be achieved using other synchronization constructs like locks or atomic variables like java.

How do you correct a race condition?

an easy way to fix "check and act" race conditions is to synchronized keyword and enforce locking which will make this operation atomic and guarantees that block or method will only be executed by one thread and result of the operation will be visible to all threads once synchronized blocks completed or thread exited ...

What is race condition and how it can be overcome?

A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the same time, but because of the nature of the device or system, the operations must be done in the proper sequence to be done correctly.


1 Answers

One strategy is to avoid storing mutable information in the cookie itself, and instead have the cookie be an identifier of some sort that identifies a record in a database of some kind somewhere. Then, your server updates information in the (server-side) database, instead of changing the cookie value.

like image 165
Greg Hewgill Avatar answered Oct 28 '22 11:10

Greg Hewgill