I'm considering using redis as a key value store for my api application. The api basically only needs one client connection to the redis. What I'm not sure is that should I keep the connection open forever? Or should I only open the connection when I need to set or get values from redis?
One could think that opening the connection is an expensive operation, so in that sense one should prefer forever connections. On the other hand, keeping the connection always open is not as secure as opening it only when you need it. And also, having long open connections open could result in timeouts. Does redis try to reconnect if the connection fails for some reason? How well does redis handle long open connections? Any help is appreciated!
Redis auto-connection depends on the redis-client that you are using. For example, if you use ioredis, it will automatically try reconnect when the connection to Redis is lost except when the connection is closed manually.