Q:

Iterating through keys in Redis

I have just started with Redis.  My DB contains about 1 billion records.  Using HKEYS * results in an out of memory error. 
Is there a way to iterate through keys?  Something like HKEYS * but with a limit n? 
Edit:
I am now using a loop which matches a pattern
for c in '1234567890abcedf':
    r.keys(c + '*')

A:

Available since Redis 2.8.0 are the cursor based Redis iteration commands (SCAN, HSCAN etc) that let you iterate efficiently over billions of keys. 
For your specific case, the start using HSCAN instead of HKEYS/HGETALL.  It is efficient, cheap on server resources and scales very well.  You can even add a pattern to HSCAN unlike HKEYS. 
e.g.
127.0.0.1:6379> HMSET hash0 key0 value0 key1 value1 entry0 data0 entry1 data1
OK
127.0.0.1:6379> HSCAN hash0 0 MATCH key* 
1) "0"
2) 1) "key0"
   2) "value0"
   3) "key1"
   4) "value1"
127.0.0.1:6379> HSCAN hash0 0 
1) "0"
2) 1) "key0"
   2) "value0"
   3) "key1"
   4) "value1"
   5) "entry0"
   6) "data0"
   7) "entry1"
   8) "data1"

