Creating ansible host_vars and group_vars files dynamically
The most elegant way would be to use dynamic inventory, rather than static inventory and dynamic group variables.

However, if for some reason you don't want to do that, one other option is to use custom facts; these are scripts that live in /etc/ansible/facts.d on the client machines.  Like dynamic inventory, they be executable scripts (and therefore, query redis or whatever else), but in this case they define variables just for that particular host.  This isn't quite what you were looking for, since you'd have to query the database on each individual server instead of once for the entire group, but it can have a similar effect.