stefanwille / crystal-redis

Full featured Redis client for Crystal
MIT License
380 stars 61 forks source link

lpush error when sending concurrent requests #70

Closed parruda closed 5 years ago

parruda commented 5 years ago

Hey guys,

I am using Kemal to receive requests on a specific endpoint and add a JSON to a queue to be consumed by workers using the Reliable Queue Pattern. Basically it does:

redis.lpush(queue_name, value)

Things start to fail when I send concurrent requests.

This works:

$ while true; do curl http://localhost:3000;done

Then I add a second one:

$ while true; do curl http://localhost:3000;done

That's when I start to see errors:

Exception: cast from String to Int64 failed, at /src/lib/redis/src/redis/command_execution/value_oriented.cr:12:9:12 (TypeCastError)
from src/lib/redis/src/redis/command_execution/value_oriented.cr:0:9 in 'integer_command'

What could be causing it?

maiha commented 5 years ago

Hi @parruda !

This library is not thread-safe. So you must handle connections by yourself. The choices are as follows.

Best regards,

kostya commented 5 years ago

use pooled_client: https://github.com/stefanwille/crystal-redis#connection-pooling

stefanwille commented 5 years ago

Redis objects are not thread safe. Please use @kostya's solution: Redis::PooledClient, as in @kostya's link.

parruda commented 5 years ago

Using the pooled client works. Thanks guys!