Finding the one in the crowded Matrix

One funny and yet powerful idea related to one-to-one relations came to my mind recently.

Imagine a simple one-to-one relation whenever a second object always exists. For instance a user and his default virtual folder, whenever a user is created, the user’s default folder should also be created, and it cannot be deleted.

First of all a pure o-t-o relation is usually pretty useless, just add what’s needed to the table schema and skip the foreign relation part. …

Optimizing Redis memory usage for strings, integers, jsons and anything you like

Whats inside:

  • How much +1 symbol to every redis key costs, and does key names optimization worth it?
  • Key/value store abstraction over redis hashes. Getting 10 times less memory for stored integers or getting 5% memory discount for medium previews.
  • To GZip or not to gzip — comparision of gzipping VS other types of memory optimizations on a various range of data.
  • Introducing me-redis gem — all types of redis memory optimization in one set of tools. Tiny code changes for a max result!
  • Memory savings ( starting at 2.5 less up to 12 times less memory ) and performance…

English version: Simple sample for Postgres and ruby.

Что интересного в статье:

  1. Пример быстрого псевдослучайного семплинга массивов в постгресе и руби.
  2. Сравнение различных реализаций сэмплирования массивов в постгресе и руби, с предложенным: Для постгреса на массивах в 1000 элементов в 6.5 раз быстрее комбинации order by random() ( в 15 раз на 10К элементах) и в 4 раз быстрее процентной выборки random() < 0.xx ( в 10 раз на 10К элементах ). Для руби на массивах от 2000 элементов и от 11 элементов в выборке — нарастающая разница в пользу предложенного варианта.
  3. Основной недостаток сэмплирования через TABLESAMPLE SYSTEM и SYSTEM_ROWS…

Русская версия статьи: Simple sample в ruby и Postgres’е. (рус)

Whats inside:

  1. An example of a fast pseudo-random sampling of an array in ruby and Postgres.
  2. Comparison between different sampling implementations with a given one. In short for Postgres it’s much faster then order by random() or where random < 0.xx. For ruby its faster for arrays with size greater than 2000 elements and sampling size greater than 10.
  3. What is the main flaw of the TABLESAMPLE SYSTEM and the TABLESAMPLE SYSTEM_ROWS and how to escape it.
  4. An example of a random STABLE function

Through this paper I will state N as…

English version: From Action To Any

Подкапотные нюансы AnyCable для мигрирующих с ActionCable.

Что будет интересного:

  • Как можно посчитать количество пользователей онлайн в ActionCable и почему это сломается в AnyCable
  • Чем отличается current_user в базовом классе Connection при использовании AnyCable от ActionCable.
  • Почему AnyCable гарантировано ломает devise при использовании Rails сессии и как с этим быть.
  • Чем отличается stop_all_streams в AnyCable от своего аналога в ActionCable
  • Как разорвать соединение извне в ActionCable и как это можно сделать AnyCable
  • Где была и куда делась проверка заголовка Origin в AnyCable.

Это небольшое мемо будет интересно для тех кто хочет копнуть на полштыка…

Русская версия: От Action к Any

Internal aspects you may notice while migrating from ActionCable to AnyCable

What’s inside:

  • How to count online users in ActionCable and why it would break in AnyCable
  • Small differences between current_users defined by identified_by in Connection::Base class.
  • Why AnyCable will break your devise authentication and how to fix it.
  • What’s the difference between stop_all_streams call in AnyCable and ActionCable
  • How to stop connection from the outside with ActionCable, and what to do in AnyCable.
  • What happens with cross-origin security in AnyCable comparing to ActionCable.

This little memo will be useful if you want to…

Postgres derailed indexes. Part 2

How to miss a GIN index in your queries you can read in Yo-ho and the bottle of GIN. This one is example how to miss a b-tree index using varchar_pattern_ops/text_pattern_ops in non-C locale environment.

Autocomplete case

The best reallife case scenario would be an autocomplete implementation. Assume you need a fast autocompletion on some text column. User typing a text and an application delivers ten nearest words/phrases alphabetically sorted, like when you do googling.

Fulltext search problems

This can be done with full text search using tsvector and ts_queries, they can do prefix search. But FTS may be highly inefficient for short piece of…

There seems to be something wrong with our bloody index today!

I will not give a speach about indexes in general or indexes special features in the PostgreSQL. Lets assume you know what indexes are, what is their value in delivering right amount of a needed data within a suitable amount of time. I just want to bring up a couple of interesting examples on how you can easily derail indexed behaviour in the PostgreSQL.

Yo-ho and bottle of GIN

Data structure

Lets start with a table named cards which have two columns a text title and a foreign key of collection_id.

title varchar NOT NULL,
title_tsv tsvector NOT NULL,

A reasonable way of Code-Test-Document triad organization in ruby based application with conceptions and tools overview and comparision, containing code examples.

Back then

When I was doing my first steps as C++ Win32 programmer, I was surprised how much Microsoft valued it’s documentation. MSDN documentation package was couple of times bigger than MS Visual Studio, and tech writers staff at Microsoft, as I heard, was at least as big as programmers or may be even bigger! It was some really shining etalon of how things must be done with a documentation. This was a long ago, now I’m not dealing with COM…

Since the time I discover arrays in PostgreSQL as a data type, I keep on wondering what would happend with a many to many relationship done through arrays instead of a mtm intermediate table. Of course at a scale of 100K it looks like nothing interesting, but this is a wrong assumtion, even at a scale of 100K * 100K with intence linking between objects we can see some interesting trends.

Many to many

In systems analysis, a many-to-many relationship is a type of cardinality that refers to the relationship between two entities[1] A and B in which A may contain a parent


Chief Software Architect / CTO, Ruby and PostgresSQL fan.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store