No I/O in STM transactions?

73 views
Skip to first unread message

Hugh Winkler

unread,
Apr 5, 2009, 3:11:45 PM4/5/09
to clo...@googlegroups.com
Hi all,

From http://clojure.org/refs : "I/O and other activities with
side-effects should be avoided in transactions, since transactions
will be retried. The io! macro can be used to prevent the use of an
impure function in a transaction."

Isn't it OK to do I/O in a transaction as long as doing the operation
multiple times has the same effect as doing it once (i.e. it's
idempotent)?

Isn't it also OK to read data in a transaction, in many circumstances?
Even if retrying the reads results in a different end state, that's OK
for lots of systems. Example: couldn't I safely read a stock price
from a ticker stream within a transaction, if the purpose was only to
have a cached value from sometime in the last 5 minutes?

(I imagine there are performance considerations. Retrying some I/O
operations could be expensive. Right now, I'm concerned with the
logic, not the performance, but if you know something about the
performance implications, please jump in).

Just hoping someone can straighten me out if I'm wrong... I'd like to
remove some io! macros from my code!

Thanks,
Hugh

Stephen C. Gilardi

unread,
Apr 5, 2009, 3:50:31 PM4/5/09
to Clojure

On Apr 5, 2009, at 3:11 PM, Hugh Winkler wrote:

> From http://clojure.org/refs : "I/O and other activities with
> side-effects should be avoided in transactions, since transactions
> will be retried. The io! macro can be used to prevent the use of an
> impure function in a transaction."

>

> Isn't it OK to do I/O in a transaction as long as doing the operation
> multiple times has the same effect as doing it once (i.e. it's
> idempotent)?

I suspect that Rich used the word "should" there rather than "must" as
a nod to cases like those you described. Any activities with side-
effects whose correctness will not be harmed if they are repeated
(potentially many times) are OK to include in a transaction.

Ultimately program correctness is up to you. The purpose of the
admonition is to put you on notice that the probability of retries for
code in transactions (over time) is approximately 100%. Any side
effects you include in them need to be carefully considered and will
work properly only if they are truly safe if repeated.

--Steve

Dex Wood

unread,
Apr 6, 2009, 1:58:17 AM4/6/09
to Clojure
If you want to do I/O from a transaction, just use an agent to do the
I/O. Since the agent is only sent off at commit, you don't have to
worry about retries. This doesn't answer your questions about
idempotent I/O, but I think it is a strategy that you can use.

On Apr 5, 2:11 pm, Hugh Winkler <hwink...@gmail.com> wrote:
> Hi all,
>
> Fromhttp://clojure.org/refs: "I/O and other activities with

Hugh Winkler

unread,
Apr 6, 2009, 10:23:17 AM4/6/09
to clo...@googlegroups.com
On Mon, Apr 6, 2009 at 12:58 AM, Dex Wood <slas...@gmail.com> wrote:
>
> If you want to do I/O from a transaction, just use an agent to do the
> I/O.  Since the agent is only sent off at commit, you don't have to
> worry about retries.  This doesn't answer your questions about
> idempotent I/O, but I think it is a strategy that you can use.

Yes, thanks very much for this tip! I have to admit I didn't (don't)
understand the agent concept. I've been avoiding the subject I guess.
Going to have a look now...
Reply all
Reply to author
Forward
0 new messages